LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-18-2007, 02:35 PM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Make a command timeout if not completed in a certain amount of time


Hello,
I'm trying to bascailly find out how i can basically have a script that can properly test for ssh connectivity.

basically one way i can see is:
ssh ipaddress exit
echo $?

if 0 then ok if not then failure etc etc

but of course this is to test passwordless ssh connectivity is running ok, therefor if it isn't and it gets asked for a password this is no good, and i don't want to test with telnet ipaddress 22 because that doesn't really tell you much.

So either a way to make the command timeout if it asks for a password without changing the timeout on the config file, like an escape character being sent to the command after so much time, or i thought possibly read stdin and if it says about entering a password then again we know its failed and can exit and know the ssh didn't work passwordlessly.

Anyideas??

Cheers
 
Old 10-18-2007, 04:22 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

Code:
ISHELLO=`ssh ipaddress "echo hello"` &
SSHPID=$!
sleep 3
if [ x$ISHELLO != "xhello" ] || [ ps -ef | grep 'ssh ipaddress "echo hello"' | grep $SSHPID ]
then
    echo failure
fi
should do it.

$! == the PID of the last command, to see if the ssh connection is still running, and the 'hello' should end up in $ISHELLO if the ssh connection is successful.

Last edited by ilikejam; 10-18-2007 at 04:37 PM.
 
Old 10-18-2007, 06:06 PM   #3
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
Smile

thanks but that didn't work, and i sorted out my own script now so it all works, unles you can see a reason my method would fail for some reason i've missed

Code:
#!/bin/bash
#IP Address you want to test ssh on
ipaddress=PUT ip ADDRESS in this SPACE
#send ssh command if connect then exit but get the PID regardless
ssh $ipaddress exit & getPidOf=$!
#sleep give chance to connect
sleep 5
#Check if connect is still running, might not have found a connection,
#in which case ssh PID would have exited, might be stuck on the password,
#in which case ssh PID is still open, or might have connected then
#exited, so PID is gone
ps -ef | grep "ssh $ipaddress exit"
#test if SSH command is still running
success=$?
#Is it still running Yes/No
if [ `echo 'x'$success'x'` = 'x0x' ]; then
#Was still running might be stuck on password or not timeout yet
echo "PID STILL ALIVE"
#Kill off SSH PID
kill -9 $getPidOf
echo "SSH ERROR"
else
#SSH PID not running exited either no host or success
echo "PID DEAD"
#test ssh again
ssh $ipaddress exit
#if success then exit code=0 otherwise something else e.g 255
Success=`echo $?`
#was it success?
if [ `echo 'x'$Success'x'` = 'x0x' ]; then
#yes exit code= 0
echo SSH WORKING FINE 
SSHWORKING=yes
else
#no ok must be no connection to host
echo ERROR
fi
fi
#echo $getPidOf #Used for testing

if [ $SSHWORKING = yes ];then
commands enter here
fi
exit
This works but might be over complicated who knows, i no longer need it anyway thought up a better way to do what i want but still if anyone wants it its here for ya
Regards
Mark
 
  


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
Make most amount of Linux users in least amount of time studpenguin General 24 02-02-2007 03:42 PM
Killing a process after x amount of time... virid Linux - Newbie 3 03-17-2006 06:13 AM
What's the shortest amount of time to expect to pass the RHCE/T MasterC Linux - Certification 10 01-30-2006 12:58 PM
Ping times grow HUGE after X amount of time Shade General 2 05-11-2005 11:03 PM
c program that executes during a certain amount of time jagman Programming 6 04-13-2005 09:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:26 AM.

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
Open Source Consulting | Domain Registration