Checking for availability of remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking for availability of remote server
# 1  
Old 02-22-2007
Checking for availability of remote server

Hi. In my KSH script I'm running a command on a remote server. I want to know how I can test to see if the remote server is available (accessible) prior to running the command. Any ideas?

My command looks like:

`ssh $USER@$TARGET_SERVER_DNS ls -l $REMOTE_FOLDER `

This check should be generic enough to handle the following cases:

- server not available (network problem)
- server DNS name not found
- invalid user name, user doesn't exist on the remote server
- remote folder path doesn't exist
- etc

I just need to the SSH command to return a true/false indicating success or failure, that's it.

One inportant point: PING is disabled in my environment.
# 2  
Old 02-23-2007
For cases where
>server is not up and running
>DNS could not resolve the server name to IP


ssh would come back with a error status and a message of unknown service or name

for case
>invalid user name
if you are using passwordless ssh, number of attempts would be exhausted and returned with an error status
if you are using ssh prompted by password, then for an invalid user then you get permission denied each time

for case
>invalid remote dir
clearly ssh would return error status, which could be checked from $?
# 3  
Old 02-23-2007
Thanks. The answer came from a reply found in the IP Networking for Dummies newsgroup.

The solution I found was to first run the SSH command, without the `` around it. Then get the return value. For some reason, if I put `` around the SSH command, I can't get the return code. For example:

ssh $USER@$SERVER ls -l $FOLDER
RESULT=$?

If the value of $RESULT is anything other than 0, the command failed. However, the following will NOT work:

RESULT=`ssh $USER@$SERVER ls -l $FOLDER`

I don't understand why the second example won't work, but it doesn't.
# 4  
Old 02-26-2007
Quote:
RESULT=`ssh $USER@$SERVER ls -l $FOLDER`

I don't understand why the second example won't work, but it doesn't.

With the above, only the result returned by executing ls -l $FOLDER would be stored in RESULT var and not the exit status of ssh,

need to use echo $? only after ssh executes and returns
# 5  
Old 03-01-2007
The above example does what I need it to do. If the DNS name is invalid, it returns 255. If the username is invalid, it returns some other number. If the folder path is wrong, it returns yet another different number. If everything is correct, it returns 0. That's all I need it to do. If the return code is 0, then I proceed with running the "real" command that returns me file count in that folder. So, everything is working as I need it to. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Checking integrity copied from remote server using UNIX command

I am getting a file (abc.txt) using scp from remote server. scp <connect string> -i $HOME/.ssh/id_dsa rem_id@hostname:/var/prod/abc.txt /var/prod/abc.out Before loading I need to check if the file is indeed same , for that I thought of checking the line count but when I used ssh command it was... (3 Replies)
Discussion started by: lalitpct
3 Replies

3. Shell Programming and Scripting

Checking SQL DB Availability

Hello everybody. I have a little question. Im creating a script for checking the status of different servers, and everything is running fine except for one thing. I have to check if certain DBs are connectables, I dont care anything else, nor running querys nor updating tables, the only thing I... (4 Replies)
Discussion started by: SeaSoul
4 Replies

4. UNIX for Dummies Questions & Answers

Checking files in remote server and decide to copy file or not

Hi there, I have a problem in my script, I need to check whether file exists in remote server or not, if the file exists, then stop copy else copy the file to the server.. my code is something like this while read $server do if ssh $server "cd $directory_name; if ; then echo "Error:... (2 Replies)
Discussion started by: beezy
2 Replies

5. Shell Programming and Scripting

Script should keep checking for availability of files.

Hi, I want my script to check for availability of few files contineously for some time. Lets say from 10:00 AM to 10:30 AM script should keep checking on a particular location for each file one by one. If all the files are available then only it should complete the next processing part, else... (2 Replies)
Discussion started by: amit.mathur08
2 Replies

6. Shell Programming and Scripting

Checking availability of service

HI CAN ANY ONE HELP ME FOR BELOW QUESTION CHECKING AVAILABILITY OF SERVICE IN LINUX (2 Replies)
Discussion started by: satheeshkr_cse
2 Replies

7. Shell Programming and Scripting

Checking the file in remote server using SFTP

Hello, I would appreciate if any one can help me on this. The below script start running at 12:30AM. Every 5 min, i go and check the remote site. If i see filewatch.txt over there, then i need to call another shell script and load the data into database. If i don't see that file, then i have to... (2 Replies)
Discussion started by: govindts
2 Replies

8. Solaris

Checking a file on a remote windows server

Hi Everyone! This is what I need to do... I am deploying some directories over to a windows server 2000/3 box from my solaris10 box and I need to make sure the directories exist. The only form of connection I have to work with is SSH Currently, my script deploys the directories over... (13 Replies)
Discussion started by: Keepcase
13 Replies

9. Shell Programming and Scripting

Checking availability of Web pages through Unix script

Hi Guru's, I need to check the availability of a web page for every one hour through a script. Following is the requirement. 1. Go to http://vsn.srivsn.com 2. If any error encountered in openeing the home page, it should trigger an email with the appropriate error message. 3. If page opens... (6 Replies)
Discussion started by: srivsn
6 Replies

10. IP Networking

Checking for availability of remote server

Hi. In my KSH script I'm running a command on a remote server. I want to know how I can test to see if the remote server is available (accessible) prior to running the command. Any ideas? My command looks like: `ssh $USER@$TARGET_SERVER_DNS ls -l $REMOTE_FOLDER ` This check should be... (1 Reply)
Discussion started by: dylanmilks
1 Replies
Login or Register to Ask a Question