ssh connection script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ssh connection script
# 1  
Old 09-20-2006
ssh connection script

Hi all,

I'm writing a script that chooses the best computer available in an open lab. The script works great except every now and then there is a dead computer in the lab that begins the ssh handshaking, but freezes after the following:

debug1: Offering public key:
When the script happens accross such a computer, it hangs up waiting for the ssh command to finish.

I'm trying to come up with a way to implement a timeout criteria that stops the local attempt to wait for the ssh command after a set time interval. Not having found an option in ssh itself that does this, I'm trying to achomplish the task by putting the ssh in the background, like this (I'm using bash):
ssh lab4-5 'uptime' > test_load &
If I enter the above line in a terminal, it runs in the background with an expected Pid and "Done" message. however, if it is part of a script file, there are no such messages printed, nor does a "jobs" command indicate what the Pid might be.

The clossest I've come to the script working is the following:
#!/bin/bash

timeout=2
tstart=$( date | sed -r -e 's/.*:.*:0?//' -e 's/ .*//' )

[ -e test1 ] && rm test1

{
ssh lab4-5 'uptime' >| test2
mv test2 test1
} &


while [ ! -e test1 ]; do

tnow=$( date | sed -r -e 's/.*:.*:0?//' -e 's/ .*//' )
lapse=$(( tnow - tstart )) && [ "$lapse" -lt "0" ] && lapse=$(( 60 + lapse ))
echo "lapse: $lapse"
[ "$lapse" -gt "$timeout" ] && kill %+ && echo "ssh killed"
[ "$lapse" -gt "1" ] && jobs

done

cat test1

One question I have is what outputs & should be sending its messages to. Can these be captured by the script? Any suggestions on how to get this to work are greatly appreciated. Also, if there are any ideas about a better way to go about this, I'm open to suggestions.

Thanks
# 2  
Old 09-20-2006
My retrieval script uses a timeout similar to yours. The process id is stored in $! though:

Code:
    if [ $SERVER = $JUMP ]
    then
      scp -q $SERVER:$SERVER/$CONTENT $SVRPATH & > /dev/null 2>&1
      PID=$!
    else
      scp -q $JUMP:$SERVER/$CONTENT $SVRPATH & > /dev/null 2>&1
      PID=$!
    fi

# wait for the specified number of seconds for scp to complete
# if the timeout is exceeded, kill the process and move on to the next box

    while [[ $TIMEOUT -gt 0 ]]
    do
      /usr/bin/ps -p $PID > /dev/null 2>&1
      if [[ $? -ne 0 ]]
      then
        break
      fi
      TIMEOUT=$(($TIMEOUT - 1))
      sleep 1
    done

# if the timeout reaches 0, then the process was killed. Report something.

    if [[ $TIMEOUT -le 0 ]]
    then
      echo "ERROR: Unable to connect to server ($PROGNAME)" >> $SVRPATH/config.status
      chmod 664 $SVRPATH/config.status
      kill -KILL $PID
    fi

Carl
# 3  
Old 09-20-2006
Thanks!

$! was just what I was looking for, and I've got it working now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

2. UNIX for Dummies Questions & Answers

Script that tries to connect via SSH till connection is up

Hey, I need a script that tries to connect via SSH to a remote server and that remote server might not be up yet, so retry until succeed the error message I get if the server is not up yet is: ssh: connect to host 127.0.0.1 port 40001: Connection refused any idea of a good way to do it ? ... (5 Replies)
Discussion started by: OdedOvdat
5 Replies

3. Shell Programming and Scripting

ssh connection through shell script with no interruption

Hi all, I need ssh in shell script. My requirement is: - Take user_name and password from user (in PHP) - Pass this to shell script which will: - connect via ssh - Run sql query - Pass the results back in PHP - Put the results to website. I know PHP has libraries for ssh and ftp, but... (3 Replies)
Discussion started by: shekhar2010us
3 Replies

4. Shell Programming and Scripting

How to automate SSH remote connection with a shell script

Hi Guys! I am trying to write a shell script for automated ssh. vairable user and passwd have initialized correctly, but when I use the following it still prompting me for the password. #!/usr/bin/bash user='root@10.14.76.225' passwd='admin' ssh $user $passwd uptime exit I... (3 Replies)
Discussion started by: pinpe
3 Replies

5. Shell Programming and Scripting

Passing password in script for ssh connection - no except

Used the script posted on forum - unix.com/shell-programming-scripting/21597-script-change-passwords-same-user-multiple-servers.html but the last question posted on this seems to be still unanswered, tried different things with no success, can someone help giving an way to pass the password via... (5 Replies)
Discussion started by: sapadmin
5 Replies

6. Shell Programming and Scripting

Mysql command after a SSH connection (Script)

Hi all, Im new at scripting and i need to run a few commands at work every hours so i decide to make a script but on 1 of the steps i have a the follwoing problem: The command i do is this: #!/bin/bash ssh root@asdasd001 'mysql -h A-db-1 -uroot -password --execute "show slave status"'... (3 Replies)
Discussion started by: Aparicio
3 Replies

7. Shell Programming and Scripting

SSH Connection drops - but does my script keep running?

Hello there. I'm fairly new to Linux, but I am connecting via SSH and PuTTY to a remote server, and I am running a fairly heavy MySQL script in a PHP page. Our connection here is dodgy to say the least and I get continuous disconnections. My question is, when I get disconnected, does my... (4 Replies)
Discussion started by: christatedavies
4 Replies

8. Shell Programming and Scripting

how to terminate ssh connection without terminating script

Hi all, I connect with SSH connection to remote machine in the script and ı want to logout at half of the script then continue to script. If ı write exit in the script it terminates script not SSH connection. How can i do that please help me (1 Reply)
Discussion started by: fozay
1 Replies

9. Shell Programming and Scripting

Remote SSH Connection Using Script

Hi, I am new to Shell Scripting. Can anybody help me in writing a Script Which Could Login from a Unix box to a Remote Unix box which accepts the user credentials automatically and display the result for checking the Disk Space Utilisation (Without running any SSH agent). (1 Reply)
Discussion started by: ajith_tg
1 Replies

10. Shell Programming and Scripting

Testing ssh connection from KSH script

Hi. I have a kornshell script that runs on a daily basis as a cron job. Part of what the script does is copy the folder contents from another server to the current server (server where KSH script is running). I have a scp command, as follows: scp $REMOTE_HOST:$REMOTE_FILE_DIR/* $TMP_DIR ... (8 Replies)
Discussion started by: dmilks
8 Replies
Login or Register to Ask a Question