Link to home
Start Free TrialLog in
Avatar of Los Angeles1
Los Angeles1

asked on

Linux script to copy files from host to host

I am using RHEL 6.4

Regarding an earlier post where I discovered that I could run a remote script from my host as follows:

putty -t user@host -pw password -m local_commands_file

Open in new window


I want to write a script that will either:

1> Copy a file from the calling machine to the remote machine

2> Copy a file from one remote machine to another

How can I do this ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gilesb2002
gilesb2002

Below is an example of how I do this with and expect script. We have a server that process large amount of files. The script below copies files from several different locations to that server for processing.

The script copies a file passed to it from the command line from a remote server to the local server. You can change the scp command to move the to and from wherever you want.

The file does contain the password in plain text so make sure it only has the necessary read/write permissions. Also you will have to manually ssh into the remote servers from where the script is ran before the script will work. This is so that you can accept the RSA key from the remote server. You could modify the script to send back yes when prompted if you wanted.

#!/usr/bin/expect -f

# Expect script to supply root/admin password for remote ssh server 
# and execute command.

# set Variables
set filetoget [lrange $argv 0 0] 

set username <your user>
set password <your users password>

# remote IP address
set ipaddr <IP address of remote server>
set timeout -1
   
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn scp $username@$ipaddr:/$filetoget /<destination>/
match_max 100000

# Look for passwod prompt
expect "*?assword:*"

# Send password aka $password 
send -- "$password\r"

# send blank line (\r) to make sure we get back to gui
send -- "\r"

expect eof

Open in new window

Avatar of Los Angeles1

ASKER

The scp solved many a problem.  

Thanks