[lug] sh and expect

dan radom dradom at redback.com
Tue Aug 21 09:22:43 MDT 2001


I've written a few scripts for a customer that run from cron and backup Redback Networks SMS product configuration files to a tftp server up to 5 times a day, and I'm not really happy with the they run.  I'm looking for suggestions, and my ultimate goal is for both sh and expect commands to live in one single file.  Here's what I've got so far...

...
cat sms-hosts
xxx.xxx.xxx.001
xxx.xxx.xxx.002
and so on


...
cat redback-multihost-tftp.backup 
#!/bin/sh
TARGET=/tftpboot/backup-config
for x in `cat /SCRIPTS-REDBACK/sms-hosts`
do
FILE=$TARGET/sms-$x-`date '+%Y-%m-%d'`

if [ -f $FILE ]; then
    mv $FILE.4 $FILE.5
    mv $FILE.3 $FILE.4
    mv $FILE.2 $FILE.3
    mv $FILE.1 $FILE.2
    mv $FILE.0 $FILE.1
    mv $FILE $FILE.0
fi

touch $FILE
chmod 666 $FILE
done
for x in `cat /SCRIPTS-REDBACK/sms-hosts`
do
exec redback-multihost-tftp.exp $x `date '+%Y-%m-%d'` &
done


...
cat redback-multihost-tftp.exp
#!/usr/local/bin/expect
#
spawn telnet [lindex $argv 0]
expect "Username:"
send "user\@local\r"
sleep 1
expect "assword:"
send "passhere\r"
sleep 1
expect ">"
send "enable\r"
sleep 1
expect "assword:"
send "passhere\r"
sleep 1
expect ">"
send "save conf tftp://xxx.xxx.xxx.xxx/backup-config/sms-[lindex $argv 0]-[lindex $argv 1]\r"
expect ">"
sleep 1
send "exit\r"



The scripts function as expected, but I would rather not have to use 3 files to accomplish this.  The sms-hosts file can be easily done away with, but I'm not sure on how to run the expect commands from a #!/bin/sh script.  Any thoughts or suggestions?


Dan



More information about the LUG mailing list