Register
It is currently Tue Jul 07, 2015 12:12 pm

Wait for ssh tunnel to properly come up then launch program.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Nov 27, 2012 4:57 am   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 26
Location: Try to guess!
I've been using this old script for some time and almost forgot about it until the other day i became aware of its limitations. Because of the net slowing down to a crawl and the ssh handshake requiring almost 40 seconds to complete, i found out that it could not tell wether the ssh tunnel was ready or not. It could just simply detect (thanks to the ssh return value) wether ssh failed or not.
Code:
machine=mach001

if [[ $1 != "" ]]; then
   labmachine=$1
fi

echo "Connecting to: $labmachine"

ssh -2N $USER@$machine.my.domain.tld -L 1190:news.my.domain.tld:119 & sshpid=$!
retval=$?
if [[ $retval != 0 ]]; then
   notify-send -t 5 -i $iconfolder/messagebox_warning.png "SSH FAILED" "Exit code $retval"
   exit 1
fi
sleep 3
thunderbird
kill $sshpid
retval=$?
if [[ $retval != 0 ]]; then
   notify-send -t 5 "Kill failed."
   exit 1
fi

What's the proper way to make the script wait for the tunnel to be correctly established?
Using -v and grepping for "Entering interactive session"?? =))


Top
 Profile  
 PostPosted: Sat Jun 27, 2015 9:40 am   

Joined: Fri Jun 26, 2015 4:43 pm
Posts: 7
Wait or sleep command maybe? After the command to ssh. I'm not an expert but I have had to use the wait and sleep command for renewing a lease for an Ip. Had to make it sleep 60 in a few places so everything had time to shake hands properly. Seems like the same could be done here.

Code:
ssh -2N $USER@$machine.my.domain.tld -L 1190:news.my.domain.tld:119 & sshpid=$!
sleep 60


Sometime ssh takes longer just adjust the sleep vaule to whatever needed. Although, I have not tried it so you'll have to play around with it to get it perfect for your needs.


Top
 Profile  
 PostPosted: Sun Jun 28, 2015 7:42 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 167
Using wait is wrong. It waits until given process exits.
Just sleeping does not give proper control.

A solution could be to start tunnel in background and then read with nc until something can be read from tunnel. As nc got a -verbosity flag you can use this to write something like:
Code:
# start tunnel first
# then try to read from tunnel in a loop
while [[  "${#nc_output" == 0 ]]; do
    read -r nc_output < <(nc -v -d -u localhost port 2>&1)
    sleep 0.5  # sleeps half a second or omit entire line
done
# procede with your script
Note: no data pass the tunnel. Just the -v flag outputs something.


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: No registered users and 1 guest


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:  
cron


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