Register
It is currently Fri Aug 01, 2014 5:50 am

Telnet & SSH Questions in bash scripts


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Apr 26, 2009 9:28 pm   

Joined: Sun Apr 26, 2009 10:11 am
Posts: 5
Hi All,

I'm currently new to scripting and would want to ask for your assistance/suggestions. I have created a script that would ping a certain IP Address or Hostname that is entered by the User. After the ping finishes the script would prompt the user if they want to login to the node.

Here's were my problem comes in. The IP Address or Hostname could be a Cisco Router or a Linux/Solaris box. I want the script to try and Telnet to the IP or Host and it fails try to ssh on it. I'm guessing that the script would base it decision on the error messages it receives when trying to perform a telnet or ssh. I don't know how to do this and any assistance would be greatly appreciated.

Error Message:
-bash-3.00$ ssh 10.10.30.11
ssh: connect to host 10.10.30.11 port 22: Connection refused

-bash-3.00$ telnet 10.10.80.184
Trying 10.10.80.184...
telnet: Unable to connect to remote host: Connection refused
-bash-3.00$

Here is script.

#!/bin/bash
clear
echo "Date Started `date`"
echo Enter IP
read IP
ssh user@10.10.82.55 ping -sn $IP 56 10
sleep 1
echo "Date Finished `date`"
echo " The End! "
while :
do
echo "Login to Host?"
echo "1. To Login to Node"
echo "2. To go back to Menu"
echo -n "Enter your choice [1 or 2]"
read userchoice
case $userchoice in
1) telnet $IP
exit
read ;;
2) echo "Going back to Menu"
exit
read ;;
*) echo "Not a valid choice"
exit
esac
done


Thanks in advance.[code][/code]


Top
 Profile  
 PostPosted: Mon Apr 27, 2009 12:15 am   

Joined: Sun Apr 26, 2009 10:11 am
Posts: 5
Found out a way to accomplish this. I created a loop within a loop. But it seems that i'm encountering problems as i go along.

Here's the update working script.

Code:
#!/bin/bash
clear
echo "Date Started `date`"
        echo ""
        echo "        Enter IP or Hostname        "
        echo ""
        read IP
        echo ""
        ssh noc@10.10.82.55 /usr/sbin/ping -sn $IP 56 10
        echo ""
        echo "Date Finished `date`"
        echo ""
        echo "        The End!"
while :
do
echo ""
echo "     Login to Host?"
echo "     1. To Login to Node"
echo "     2. To go back to EMS Utility"
echo ""
echo -n "Enter your choice [1 or 2]"
echo ""
read userchoice
case $userchoice in
        1) while :
           do
           echo ""
           echo "1. Cisco Router"
           echo "2. Linux/Solaris Box"
           echo ""
           echo -n "Enter your choice [1 or 2]"
           read userinput
           case $userinput in
                1) telnet $IP
                exit
                read ;;
                2) ssh noc@$IP
                exit
                read ;;
                *) echo "INVALID CHOICE!"
                exit
          esac
          done ;;
        2) echo ""
        echo "Going back to EMS Utility"
        exit
        read ;;
        *) echo "Not a valid choice"
        exit
esac
done



Since the script is based on loops, how can i exit the loop if the initial ping fails? With this script if the ping fails it continues to the first loop. Any advice / suggestions?

Thanks.


Top
 Profile  
 PostPosted: Mon Apr 27, 2009 1:02 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
If you know the IP is a "correct" one then you can use ping.

Code:
ping -c1 <host>

This will ping the host 1 time and then exit. If the reply was not found it returns a errorcode ($? is greater then 0).

Code:
#!/bin/bash
list_of_ip=filename.txt
for i in $(cat $list_of_ip); do
   ping -c1 $i > /dev/null
   if [ $? -gt 0 ]; then
      echo "Failed to ping host $i ... "
   else
      echo "Successfully pinged host $i ... "
   fi
done


If you wish to store the output just redirect the script output (./script.sh > output.txt).

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP