Bash Script to check Remote Host Connection


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash Script to check Remote Host Connection
# 1  
Old 04-16-2008
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
# 2  
Old 04-16-2008
Define "up and running".

Code:
ping -c 1 remotehost || { echo "does not respond to ping" >&2 ; exit ; }
rup remotehost || { echo "no rup" >&2; exit; }
# if you have netcat, and ssh on port 22 on remotehost
nc -z remotehost 22 || { echo "nobody home on port 22" >&2; exit; }
ssh remotehost true || { echo "does not let me in" >&2; exit; }

If you want to check port 80 instead, try to fetch a web page, etc.

This obviously also depends on firewall blocks and stuff like that.
# 3  
Old 04-16-2008
Thanks Era. By "Up and Running" I mean i should be able to scp necessary file from that machine to other machine. Hope this helps.

Thanks again for your help.

Regards.
Zulfiqar
# 4  
Old 04-16-2008
By that definition, if your scp fails, it's not.

Code:
if scp remotemachine:path/to/file here/
then
  # it's up and running! whee
else
  echo "could not scp: $?" >&2
  exit $?
fi

You might still want to check if it's responding to ping and accepting connections on port 22 before attempting the actual copy, but in practice, often all you really care about anyway is whether the copy succeeded or not.
# 5  
Old 04-16-2008
Thanks Era. But this script would just check scp is successful or not. But What I want is to see if remote host is able to accept connection before doing scp.

Thanks.
Zulfiqar
# 6  
Old 04-16-2008
If you can ssh, you can scp.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SFTP file check through a remote host

Hi all, posting my first time, hope not breaking posting rules with it, if yes, let me know. I'm trying to build a script to check a file in an sftp server through a remote server. The intention is to check the file in a sftp host, and if the file is found or not, it should send an email.... (4 Replies)
Discussion started by: MrShinyPants
4 Replies

2. 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

3. Shell Programming and Scripting

Telnet Bash Script (Connection closed by foreign host.)

Hello Everyone, My following script is giving me problems, when the SIP trunk goes down and the telnet session is started and just when the command is about to complete the connection is closed then script restarts. I have noticed that as soon the script types in "sys re" or "sys rebo" or... (6 Replies)
Discussion started by: jeetz
6 Replies

4. 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

5. Shell Programming and Scripting

command to check whether the remote host is up or not

Hi, My script needs to check whether the remote host is up or not. If it is up i need to start few servers in that host or else just a notification should be sent that the remote host is down? Could someone help me out in this? Regards Arun (4 Replies)
Discussion started by: arunkumarmc
4 Replies

6. UNIX for Dummies Questions & Answers

ssh_exchange_identification: Connection closed by remote host Connection closed

Hi Everyone, Good day. Scenario: 2 unix servers -- A (SunOS) and B (AIX) I have an ftp script to sftp 30 files from A to B which happen almost instantaneously i.e 30 sftp's happen at the same time. Some of these sftp's fail with the following error: ssh_exchange_identification: Connection... (1 Reply)
Discussion started by: jeevan_fimare
1 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

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? (6 Replies)
Discussion started by: praveenbvarrier
6 Replies

9. 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

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