check ssh connection to remote host


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check ssh connection to remote host
# 1  
Old 05-18-2008
check ssh connection to remote host

I am using KSH and I need to check whether the remote host has been configured with ssh public key. Is there any way we can check inside a script?
# 2  
Old 05-18-2008
man test
Code:
host=host
ssh $host "echo 2>&1"
test $? -eq 0 && echo $host OK || echo $host NOK

# 3  
Old 05-18-2008
Of course, that's a Useless Use of Test $?

Code:
ssh $host "echo 2>&1" && echo $host OK || echo $host NOK

Absence of a public key is by no means the only way the ssh command can fail, so this is an approximation at best; but maybe it's sufficient.
# 4  
Old 05-18-2008
Quote:
Originally Posted by era
Of course, that's a Useless Use of Test $?
Smilie This time I use test only as example to show him how to use the exit status.
# 5  
Old 05-19-2008
Great thanks , This is helpfull.
# 6  
Old 05-20-2008
I got a problem here

I removed the public key config on the ssh server and then tried

ssh root@$host "echo 2>&1" && echo "OK" || echo "NOK"

But it came out asking password . which I dont want , what I wanted is even if its asked passwd its should come out with a non-zero exit satus.

--------------------------------------------------------------------
Now let say how I got it implemented , even though a twisted way

if [ -f $HOME/.ssh2/ssh2_config ]
then
mv $HOME/.ssh2/ssh2_config $HOME/.ssh2/ssh2_config.bkp
echo "QuietMode yes\nBatchMode yes\nConnectTimeout 4" > $HOME/.ssh2/ssh2_config
else
echo "QuietMode yes\nBatchMode yes\nConnectTimeout 4" > $HOME/.ssh2/ssh2_config
fi

ssh -l root $R_HOSTNAME "date" > /dev/null
CONN_STATUS=$?

And if the RC is 66 , then it mean it had asked passwd and not got passwd within in the timeout period. Andas usual 0 as success.
# 7  
Old 10-06-2008
Code:
ssh -q -o "BatchMode=yes" user@host "echo 2>&1" && echo "OK" || echo "NOK"

...will solve your problem, by running quietly and in batch mode (no user present to enter a password). See man ssh_config for more details. Obviously the two echo statements can be replaced with any action you like...
This User Gave Thanks to cs03dmj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

2. Shell Programming and Scripting

How can i get the logged in host machine ip in a ssh connection?

Hi All, I have a basic question. In my system i maintained multiple unix server, and only one server has public ip, and all the servers are behind load balancing BIG ip server. To access any server, user need to access server with public IP then from there they can establish ssh connection to... (2 Replies)
Discussion started by: dilipsn
2 Replies

3. Red Hat

Unable to SSH into machine - ssh_exchange_identification: Connection closed by remote host

For a few days now I have been experiencing issues when trying to SSH into 1 of my machine. I get the following output when running 'ssh -vvv': server1:/home/mymadq> ssh -l root -vvv server2 OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003 debug1: Reading configuration data /etc/ssh/ssh_config... (3 Replies)
Discussion started by: jaapar
3 Replies

4. UNIX for Advanced & Expert Users

Help! How to find the local host after few ssh hops to remote host???

I do a ssh to remote host(A1) from local host(L1). I then ssh to another remote(A2) from A1. When I do a who -m from A2, I see the "connected from" as "A1". => who -m userid pts/2 2010-03-27 08:47 (A1) I want to identify who is the local host who initiated the connection to... (3 Replies)
Discussion started by: gomes1333
3 Replies

5. UNIX for Advanced & Expert Users

"Connection closed by remote host" while doing ssh to a solaris box

Hi All, When i try to do ssh from a linux to solaris box its throughing "Connection closed by remote host". Please not that this error is not occuring every time i do ssh, it occurs at random timing( Mostly ssh is successful) - unfortunately my script which is doing ssh is falling at this time... (4 Replies)
Discussion started by: pkumar7
4 Replies

6. UNIX for Advanced & Expert Users

connecting through master ssh connection on intermediate host

Suppose host B does not allow public/private key authentication - only secureID authentication. I already have a master ssh connection from host A to host B. Host A does allow public/private key authentication. Is there any way to connect from host C to host B by way of the master ssh connection... (2 Replies)
Discussion started by: cpp6f
2 Replies

7. Linux

ssh_ exchange-identification: Connection closed by remote host

Dear All, Recently our server has been giving the error: "ssh_ exchange-identification: Connection closed by remote host" The error causes the server to become in accessible via ssh and the services are stopped/hung. The server has to be restarted to make it working normal again. The... (3 Replies)
Discussion started by: vguleria
3 Replies

8. Shell Programming and Scripting

ssh_exchange_identification: Connection closed by remote host

hi i am trying to connect the frontend server using ssh i got the following error ssh_exchange_identification: Connection closed by remote host can anyone help please (1 Reply)
Discussion started by: Satyak
1 Replies

9. UNIX for Dummies Questions & Answers

Bash Script to check Remote Host Connection

Hi all, Can anyone tell/guide me how to check remote host is up/running using bash script? Thanks. Zulfiqar (5 Replies)
Discussion started by: zulfikarmd
5 Replies

10. IP Networking

FTP - Connection Closed By Remote Host

Hi, I am having a problem with our AIX 4.3.3 Server accessing FTP. The error is " Connection Closed By Remote Host". Scenario: Since i put a default gateway on the server FTP connection is having a problem but when i remove the default gateway it will works fine.. Is there any way not... (1 Reply)
Discussion started by: mouglybean
1 Replies
Login or Register to Ask a Question