[HOWTO]Doing a script for copying files automagically:D *nux

Had your computer crash on you, or a website shows wrong, or your printer went dead on you? Come on in...

Moderator: Crew

Post Reply
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS
Contact:

[HOWTO]Doing a script for copying files automagically:D *nux

Post by Maz »

Problem: How to get files from remote computer to home via ssh (Linux)
automatically?
One way to solve the problem:

Basically we just take ssh connection to the remote computer, and make that
computer to execute scp command to our own IP address. The way I used requires
installation of package called expect to your home machine. Well, it's nice program
though, and allows many other things too :) What it basically does is to allow a script
to interact with a process.
If you use fedora, or some other clever distribution, you can smile happily while using
yum or apt­get or something else... Other vice you need to search for the rpm(s)
required to install expect and to meet the dependencies.


Here's a example script I used to copy the sensordata from my work computer to
home computer:

Code: Select all

#!/usr/bin/expect ­f  <where the expect is installed>
set timeout 4       <how long expect waits for thing expected>
spawn /usr/bin/ssh [email protected]   <launch 'interactive' process (connect to one server>
expect "password:$"     <what to expect before proceeding>
send "passwd\r\n"        <send text to the process>
expect "paju:(~)"
send "ssh username@workserver\r\n"    
expect "password:$"
send "passwd2\r\n"
expect "user@linux:~>"
send "scp Envir_measure/cardsoft/softa/log_press/sensor_data.txt
[email protected]­ip.info:humidity_19_03/.\r\n"  <I used DNS name instead IP>
expect "password:$"
send "passwd\r\n"
set timeout 400      <since datafiles were big, transfer took a while...>
expect "matti@linux:~>"
send "exit\r\n"
set timeout 4
expect "paju:(~)(53)%"
send "exit\r\n"
expect "\[Matti@localhost humidity_19_03\]$"
interact
I have added comments for this How To at the end of lines. If you copy the script,
remove <explanation> comments

Now either create a program which executes the script when needed, or put it in
crontab to be executed hourly/daily/weekly/monthly
Then grant execute permission for the script with command
chmod a+x /etc/cron.daily/scriptname
Post Reply