[frgeek-michiana] Automated ssh and rsync Expect script

  • From: Tom Brown <tbrown@xxxxxxxxxxxx>
  • To: FreeGeek Michiana <frgeek-michiana@xxxxxxxxxxxxx>
  • Date: Wed, 18 Oct 2006 10:33:16 -0500

#!/usr/bin/expect
#
#       rsyncFiles
#
# Purpose:      Copy files from one remote server to another using a cron job,
#               no user intervention required.
# Requires:     TCL and Expect extension
# Author:       Tom Brown, tbrown@xxxxxxxxxxxxxxxxx
# Date:         10/17/2006
#
# Comment:      If this is started manually by a regular user, rather than from
#               a root cron job, there is no reason to use the root password. 
Change
#               the related lines to reflect the regular user name and password.
#               
#               There is no error catching anywhere -- maybe later. What if the 
remote
#               server is offline? What if a command fails? Until I add error 
catching,
#               the cron job is logging to /var/log/cron.
#
#
# Initiate the ssh session.
# Substitute your server name for remoteServer.
spawn ssh remoteServer

# Wait for remote server to return text pattern "password: "
expect "password: "

# Send correct user password for login to remote server.
# If this is running from cron, you will probably use root's password. Some
# may object, I know.
# The password is followed immediately by \r, which means <return>.
send "rootPassword\r"

# Wait for remote server to return a command line prompt.
# You might want to modify the pattern if you have a custom prompt.
# This text pattern works with Slackware.
expect "root@remoteServer:~$"

# Send the rsync command to the remote server. Source directory is
# on the remote server. Destination directory is on the local server.
send "rsync /path/remote/directory root@localServer:/path/local/directory\r"

# Wait for remote server to prompt for a password to run rsync.
expect "password: "

# Send root's password on remote server.
send "rootPassword\r"

# Wait for rsync to finish.
expect "root@localServer:~$"

# The rest is pure TCL.
# In my application, the files must be readable by anybody.
# So I change to one level higher than the directory containing
# the just copied files and change the ownership of the directory
# to an unprivileged owner and group. The directory is also public readable.
cd /path/local
exec chown -R nobody.nogroup directory/

# Done.
exit

** This list is PUBLICLY archived. **
PLEASE don't post personal or sensitive information unless you wish for it to 
be in the public domain.

To post to the list send email to <frgeek-michiana@xxxxxxxxxxxxxx>

You may unsubscribe or change your list settings by going to the list website at 
<//www.freelists.org/webpage/frgeek-michiana>

Other related posts:

  • » [frgeek-michiana] Automated ssh and rsync Expect script