The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Checking availability of Web pages through Unix script srivsn Shell Programming and Scripting 6 05-30-2007 08:57 AM
Transfer file from local unix server to remote server indira Shell Programming and Scripting 2 05-03-2007 06:35 AM
Transfer file from local unix server to remote server indira HP-UX 2 05-02-2007 05:15 PM
Checking for availability of remote server dylanmilks IP Networking 1 02-22-2007 07:48 PM
FTP multiple files from remote server to local server berlin_germany Shell Programming and Scripting 2 12-20-2006 03:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-22-2007
dylanmilks dylanmilks is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 4
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 (permalink)  
Old 02-23-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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 (permalink)  
Old 02-23-2007
dylanmilks dylanmilks is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 4
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 (permalink)  
Old 02-26-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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 (permalink)  
Old 03-01-2007
dylanmilks dylanmilks is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 4
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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0