[SOLVED] How can I set an exit option in shell script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi
I am trying to collect OS details from multiple servers
Have a file /tmp/a which contains 10 server names
and I process it via for loop to do a df command on all these servers. I want the script to skip a particular server and proceed to the next one if its not able to connect to it or the password provided is wrong. Can I code a line in the script to do that ?
If I press a Ctrl +C, the script will terminate. I don't want that, but to move to the next server name in /tmp/a.
Can this be done ?
for i in `/tmp/a`
do
ssh $i "df -h" >>\output
done
Hi
I am trying to collect OS details from multiple servers
Have a file /tmp/a which contains 10 server names
and I process it via for loop to do a df command on all these servers. I want the script to skip a particular server and proceed to the next one if its not able to connect to it or the password provided is wrong. Can I code a line in the script to do that ?
If I press a Ctrl +C, the script will terminate. I don't want that, but to move to the next server name in /tmp/a.
Can this be done ?
for i in `/tmp/a`
do
ssh $i "df -h" >>\output
done
You can add a condition before doing ssh:
Code:
for IP in $(cat /tmp/a)
do
ping -c 2 $IP
if [ $? -eq 0 ]
then
echo $IP can be connected
ssh $IP "df -h" >>/output
else
echo $IP cant be connected
fi
done
Last edited by divyashree; 07-27-2012 at 07:13 AM.
for IP in $(cat /tmp/a)
do
ping -c 2 $IP
if [ $? -eq 0 ]
then
echo $IP can be connected
ssh $IP "df -h" >>/output
else
echo $IP cant be connected
fi
done
Thanks Divyashree. That would solve the "not able to connect issue". Is there something that I can do for exiting back to the script/for loop when I provide a wrong password
yes, cbtshare is correct. You should not use passwords in script. Instead you can setup ssh for password less login using PKI .
1 - Generate public/private key pair on the local server ( use - ssh-keygen )
2 - Distribute the public key it to all the servers you want to monitor( Use - ssh-copy-id )
An example of interacting with a for loop using trap:-
Code:
#!/bin/bash
trap 'continue' SIGINT
echo 'Press CTRL-C to end each loop immediately.'
for n in 1 2 3 4 5; do
echo "loop $n: sleeping for 10 seconds....."
sleep 10
done
trap 'continue' INT
echo 'Press CTRL-C to end this loop immediately.'
for server in `cat file`
do
ping -qc2 $server
if [ $? -eq 0 ]
then
echo $server is pingable
echo $server >>/output
ssh $server "df -h" >>\output
else
echo $server is not pingable
fi
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.