LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-27-2012, 04:38 AM   #1
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Rep: Reputation: 1
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
 
Old 07-27-2012, 04:53 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
you need to check the trap settings of the shell,
 
Old 07-27-2012, 07:10 AM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by rockie321 View Post
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.
 
Old 07-27-2012, 09:30 AM   #4
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
Smile

Quote:
Originally Posted by divyashree View Post
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
 
Old 07-27-2012, 09:24 PM   #5
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
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.
 
Old 07-28-2012, 12:33 AM   #6
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,362

Rep: Reputation: 135Reputation: 135
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 )
 
Old 07-28-2012, 11:41 AM   #7
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
I agree with both of you. But is there a way by which I can interact with a for loop ?
 
Old 07-28-2012, 09:13 PM   #8
etech3
Senior Member
 
Registered: Jul 2009
Location: Virginia
Distribution: Debian Stable Testing Sid Slackware CentOS
Posts: 1,055
Blog Entries: 2

Rep: Reputation: 45
Thumbs up A couple of things

You may want to tweak a few things.

Code:
ping -qc2 $IP
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
 
Old 07-28-2012, 11:34 PM   #9
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
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
 
Old 08-05-2012, 12:04 PM   #10
rockie321
LQ Newbie
 
Registered: Mar 2011
Posts: 19

Original Poster
Rep: Reputation: 1
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-06-2012 at 12:39 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script running in another shell script and then exit dreamervlk Linux - General 3 09-16-2011 07:40 AM
exit mail in shell script j-me Linux - General 1 02-15-2010 01:28 PM
shell script fails to exit twrig3846 Linux - Newbie 11 02-10-2010 01:10 AM
how to exit out of the calling Shell Script harimac Linux - Newbie 3 08-19-2009 02:49 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM


All times are GMT -5. The time now is 01:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration