Check if remote destination is available before running scp command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if remote destination is available before running scp command
# 1  
Old 04-06-2018
Check if remote destination is available before running scp command

I have a script on a Linux box which scp the files to windows server without any issues. but there are time frames where the windows server will not be available due to maintenance. hence I need to check if the remote location is available before running the scp command.

Code:
scp /path/to/file/filename user@host:/destination/path/

I tried with ssh command to check if remote directory exists but does not work and get the error message below

Code:
ssh user@host test -d /destination/path/ && echo exists

Code:
exec request failed on channel 0


Last edited by RudiC; 04-07-2018 at 04:38 AM..
# 2  
Old 04-07-2018
You could try a simple ping host before running the scp, but there is always the chance that your Windows server will go down between the time that you run the ping and the time that you run the scp.

It is frequently easier to just check whether or not the scp command succeeds...
Code:
if scp "$sourcepath" "$destpath"
then	echo "scp of $sourcepath to $destpath succeeded"
else	# Add "scp $sourcepath $destpath" to a list of copy commands that
	# need to be rerun later.
	echo "scp \"$sourcepath\" \"$destpath\"" >> /path/to/todo_list
	# Note that the diagnostic from scp will serve as your visual
	# indication that the scp failed this time, but you can also add
	# your own diagnostic here if you want to.
fi

or even just:
Code:
scp "$sourcepath" "$destpath" &&
    echo "scp \"$sourcepath\" \"$destpath\" succeeded" ||
    echo "scp \"$sourcepath\" \"$destpath\"" >> /path/to/todo_list

# 3  
Old 04-07-2018
Use ssh-option connecttimeout to not have to wait so long for the error to occur:
Code:
scp -o ConnectTimeout=5 file user@host:/dir/

In my ruby scripts for ssh i use the following steps(layed out in bash here) to set up an ssh connection, which wastes as little time as possible:

Code:
#!/bin/bash

LC_ALL=C
SSH_PORT=22
SSH_OPTS="-o ConnectTimeout=5 -o PasswordAuthentication=No"

export LC_ALL SSH_PORT SSH_OPTS

ping_test()      { ping -w 3 -i 0.2 -c3 "$1" &>/dev/null ; }
port_test()      { nmap -p $SSH_PORT -P0 "$1" | grep -qE "$SSH_PORT/tcp.*open" ; }
sshd_test()      { ssh -o Port=$SSH_PORT $SSH_OPTS -v "$1" date 2>&1 | grep -qE "Remote protocol version [0-9]+" ; }
ssh_login_test() { ssh -o Port=$SSH_PORT $SSH_OPTS "$1" echo ssh connect success 2>&1 | grep -qE "ssh connect success" ; }

check_ssh() {
  
   local target="$1"
   ping_test       "$target"    || return 1
   port_test       "$target"    || return 2
   sshd_test       "$target"    || return 3
   ssh_login_test  "$target"    || return 4   # this can be done as the same step with the step before
   return 0

}

# -- start of main program  

if check_ssh my_hostname; then
  if scp ... 
     ...
  fi
fi


Last edited by stomp; 04-07-2018 at 05:22 PM..
This User Gave Thanks to stomp For This Post:
# 4  
Old 04-09-2018
Just a word of caution about nmap in that it can be badly used too easily and excessive or injudicious use may be seen as a hacking attempt by either the target host(s) or networks and automatic defences may be causing you to get blocked or may trigger blocking future connections if you are too aggressive in your probing.


Is it the very long timeout that you are worried about? I agree that you should try the option -o ConnectTimeout=5 where 5 is a configurable number of seconds to wait for the connection to open. Don't make it too short or you may always fail to connect.



I hope that this helps,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check processes running on remote server

Hello Guys, I need some help to find out if processes are running on remote server or not. I could do 'ssh' to do that but due to some security reasons, I need to avoid the ssh & get result from remote server. Could you please suggest some that can be done without ssh or similar sort of... (8 Replies)
Discussion started by: UnknownGuy
8 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

Running nohup command on remote server

I am having an issue in running a nohup command in a remote linux box from a linux box. Here are the details. Linux Machine 1: I have a script which starts a services and dumps the output into a log file. nohup sh weblogic.sh >> /home/log.out & I have placed the entire command in a... (2 Replies)
Discussion started by: accessbalaji
2 Replies

4. Shell Programming and Scripting

scp without password for destination server

I am using the below command to scp the file to destination server ($server). The ssh keys have been created between the source and destination servers. Still I am getting password authentication after executing the below scp command: scp $Local_dir/$file_pattern* $username@$server:/$ftp_dir/... (1 Reply)
Discussion started by: Ganesh L
1 Replies

5. Shell Programming and Scripting

Command to check only Autosys running jobs with autorep like command

Hi, Is there any specific command to use to check only say Running jobs via autorep or similar command for Autosys? (0 Replies)
Discussion started by: sidnow
0 Replies

6. Shell Programming and Scripting

ssh - running remote command not exiting

Hi, i have a shellscript, where in i need to connect to different server start these three jobs and exit from the server and start the same three jobs on local server. ssh user@remotehost 'bash -s' << EOF ${GETT_HOME}/bin/start1 & sleep 10 ${GETT_HOME}/bin/start2 & sleep 10... (1 Reply)
Discussion started by: swapnabhargav
1 Replies

7. Shell Programming and Scripting

check web server running on local and on remote machine

Hi , How to check whether web server is running from remote machine How to check whether web server is running on web server itself Can any one help me soon (1 Reply)
Discussion started by: satheeshkr_cse
1 Replies

8. UNIX for Advanced & Expert Users

Using SCP command in IBM AIX to download file from remote to local system

Hi, When i run the code in solaris unix machine, the file from remote server is getting downloaded. but when i use the same code in IBM AIX remote machine, it is not running. It is saying "Erro during scp transfer." Below is the code. Please give some resolution. SCPClient client = new... (1 Reply)
Discussion started by: gravi2020
1 Replies

9. Solaris

Running command on Remote server

Hi, I have username/password for a remote server. I would like to have list of filenames and their size in a particular directory of remote server. Now Problem is - I can not use rsh command as I can not modify rhost file of remote server. Thanks in advance. Sanjay (1 Reply)
Discussion started by: sanjay1979
1 Replies

10. Shell Programming and Scripting

Script to check running processes on remote server.

Hi, I am trying to write a script, which queries a db to get the names of processes, stores it in a file and then checks if that process is running on a remote server. However I am not getting it right, could anyone help me out. #!/bin/sh echo "select Address from Device where Cust =... (5 Replies)
Discussion started by: amitsayshii
5 Replies
Login or Register to Ask a Question