LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Make a command timeout if not completed in a certain amount of time (https://www.linuxquestions.org/questions/linux-server-73/make-a-command-timeout-if-not-completed-in-a-certain-amount-of-time-592821/)

helptonewbie 10-18-2007 02:35 PM

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

ilikejam 10-18-2007 04:22 PM

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.

helptonewbie 10-18-2007 06:06 PM

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


All times are GMT -5. The time now is 03:22 PM.