unix server is up or not using ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unix server is up or not using ssh
# 1  
Old 10-05-2012
unix server is up or not using ssh

Hi,
I have three servers like S1,S2 and S3
I want to check the S2 and S3 server status from S1 using ssh method.
Code:
ssh user@S2 date >> test.txt
ssh user@S3 date >> test.txt

Am using the above format to get the details. But if the server is down, The code has been hanged and not coming out of it.
Please suggest on this, how to handle.

Thanks,
Chelladurai Smilie

Last edited by vbe; 10-05-2012 at 12:50 PM.. Reason: code tags
# 2  
Old 10-05-2012
I've especially seen ssh hang when the server is up but barely responding. You can put it in the background and wait up to a maximum amount of time before killing it. Your system will need to have /proc/.

Code:
ssh username@host date </dev/null 2>/dev/null >logfile &
PID=$!

TRIES=0
while [ "$TRIES" -lt 10 ]
do
        [ -d "/proc/$PID" ] || break
        sleep 1
        let TRIES=TRIES+1
done

# If it still exists despite timeout, first kill it properly, and if it refuses to die, kill it mean
[ -d /proc/$PID ] && kill -TERM "$PID" && sleep 1 && [ -d /proc/$PID ] && kill -9 /proc/$PID

wait


Last edited by Corona688; 10-05-2012 at 12:46 PM..
# 3  
Old 10-05-2012
Code:
ssh -o ConnectTimeout=1 user@S2 date >> test.txt
ssh -o ConnectTimeout=1 user@S3 date >> test.txt

# 4  
Old 10-05-2012
Quote:
Originally Posted by in2nix4life
Code:
ssh -o ConnectTimeout=1 user@S2 date >> test.txt
ssh -o ConnectTimeout=1 user@S3 date >> test.txt

This can still hang if the server is slow responding for other reasons, like swapdeath -- you might get in but hang afterwards, ssh won't kill itself when that happens.

Not all ssh complies with -o options either.
# 5  
Old 10-09-2012
Hi Corona,
Could you please the usage of the following varibale in the script.

1) PID=$!
2)[ -d "/proc/$PID" ] || break


How it works? .

Thanks in advance .
# 6  
Old 10-09-2012
PID=$! assigns the process ID of the most recently executed background command to the variable PID and thats the ssh-command.
[ -d "/proc/$PID" ] || break checks if there exists a directory in /proc with the name of your process ID. Each process usually has such a directory on most *nix-like OSes.
Edit: the || break part says leave the loop if there is no such directory anymore.

Btw, with your method you check if the sshd demon of the server is up and running. That demon being down does not neccessarily mean the whole server is down.

Last edited by cero; 10-09-2012 at 07:14 AM..
# 7  
Old 10-09-2012
Thanks for explaining Cero.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switching server in UNIX via shell script (SSH )

Requirement: I am writing a shell script which take some file from say Server1 and I have to execute some command on Server2 then I have to manipulate the data and based on that I have to produce report. Problem:I can schedule the shell script on only one server (Server1 or Server2) So, In the... (3 Replies)
Discussion started by: raks2301
3 Replies

2. AIX

How to ssh from an AIX OS server to a Fabric OS server without password?

Hi I'd like to ssh from an AIX OS server ( v5.3) to a Fabric OS server ( v6.1.2 ) without password. I tried using dsa or rsa keys but it didn't work, the aix server still asked for the password. Somebody help, please :(:(:( (8 Replies)
Discussion started by: bobochacha29
8 Replies

3. Shell Programming and Scripting

Multi server access through remote server using ssh

Team, Presently I have 5 ip address kept in ip_abc1 file, for each of the ip address listed, i need to login on each ipaddress one at a time and login as below for that specific ip address ssh -p 8101 karaf@<ip.address_for the specific ip address as logged in> password features:list... (4 Replies)
Discussion started by: whizkidash
4 Replies

4. Shell Programming and Scripting

Ssh to get files from server A and zip in server B

Hi, I have read & write access in Unix box A and read access in Unix box B. I want a folder to be zipped from box B and sftp-ed in box A. Is there a remote scripting to achieve this? (2 Replies)
Discussion started by: Prasannag87
2 Replies

5. Shell Programming and Scripting

Connect (SSH) to Windows server via Linux server through a script and passing command.. but failing

I am trying to connect to Windows server via Linux server through a script and run two commands " cd and ls " But its giving me error saying " could not start the program" followed by the command name i specify e g : "cd" i am trying in this manner " ssh username@servername "cd... (5 Replies)
Discussion started by: sunil seelam
5 Replies

6. Shell Programming and Scripting

ssh keygen in unix server

I have a central unix server and more than 200 unix server to connect from central server .For this to take place i want to use ssh keygen between & among servers .Do anybody has an idea how to automate this process . The server has same user id and same password (0 Replies)
Discussion started by: gauravsinha
0 Replies

7. Shell Programming and Scripting

Find and delete files and folders which are n days older from one unix server to another unix server

Hi All, Let me know how can i find and delete files from one unix server to another unix server which are 'N' days older. Please note that I need to delete files on remote unix server.So, probably i will need to use sftp, but question is how can i identify files and folders which are 'N'... (2 Replies)
Discussion started by: sachinkl
2 Replies

8. Shell Programming and Scripting

Using ssh to add register key on ssh server

Hi, I want to use ssh to add a register key on remote ssh server. Since there are space characters in my register key string, it always failed. If there is no space characters in the string, it worked fine. The following is what I have tried. It seems that "ssh" command doesn't care about double... (9 Replies)
Discussion started by: leaftree
9 Replies

9. Cybersecurity

What's the difference between an SSH Client and an SSH Server?

Eh... yeah. What the title says. :D (1 Reply)
Discussion started by: PSC
1 Replies
Login or Register to Ask a Question