Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-27-2012, 03:38 AM
|
#1
|
LQ Newbie
Registered: Mar 2011
Posts: 19
Rep:
|
How can I set an exit option in shell script
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
|
|
|
07-27-2012, 03:53 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,236
|
you need to check the trap settings of the shell,
|
|
|
07-27-2012, 06:10 AM
|
#3
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep: 
|
Quote:
Originally Posted by rockie321
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 06:13 AM.
|
|
|
07-27-2012, 08:30 AM
|
#4
|
LQ Newbie
Registered: Mar 2011
Posts: 19
Original Poster
Rep:
|
Quote:
Originally Posted by divyashree
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
|
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
|
|
|
07-27-2012, 08:24 PM
|
#5
|
Member
Registered: Jul 2009
Posts: 645
Rep:
|
It would be best not to use passwords, if you just want a script to log in and check the disk space or whats mounted.Just use ssh keys.
|
|
|
07-27-2012, 11:33 PM
|
#6
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep: 
|
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 )
|
|
|
07-28-2012, 10:41 AM
|
#7
|
LQ Newbie
Registered: Mar 2011
Posts: 19
Original Poster
Rep:
|
I agree with both of you. But is there a way by which I can interact with a for loop ?
|
|
|
07-28-2012, 08:13 PM
|
#8
|
Senior Member
Registered: Jul 2009
Location: Virginia
Distribution: Debian Stable Testing Sid Slackware CentOS
Posts: 1,055
Rep:
|
A couple of things
You may want to tweak a few things.
The -q is for quiet output to the screen.
Code:
echo $IP Can be connected >>/OUTPUTFILE
will put a header with the IP address of each server from the /temp/a list into the output file.
Like so:
Code:
192.168.0.XXX Can be connected
Then continue on with the info:
Code:
Filesystem Size Used Avail Use% Mounted on
just my 
|
|
|
07-28-2012, 10:34 PM
|
#9
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360
Rep: 
|
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
|
|
|
08-05-2012, 11:04 AM
|
#10
|
LQ Newbie
Registered: Mar 2011
Posts: 19
Original Poster
Rep:
|
Thanks All.
I was able to get what was requested.
Code:
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
Last edited by rockie321; 08-05-2012 at 11:39 PM.
|
|
|
All times are GMT -5. The time now is 08:52 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|