#!/usr/local/bin/expect -f # runremote.exp - Kevin P. Inscoe (2/20/2002) kevin@inscoe.org # # Telnet's to remote host, ftps script to run from current login # host and then executes command on telnet host sending output # to stdout. Script is them removed from telnet host. exp_version -exit 5.0 if {$argc!=3} { send_user "usage: runrootremote.exp host file username\n" exit } # Variables set prompt "*runrootremote*" set rprompt "root*runrootremote*" set pwd [pwd] # Note: If the remote host does not know this host by "hostname" # then change this line to your IP address set myhost [exec hostname] # local host username (for ftp) set luser "kpinsco" puts "pwd='$pwd'" puts "myhost='$myhost'" puts "luser='$luser'" # Grab cmd line arguments set host [lindex $argv 0] set file [lindex $argv 1] set uname [lindex $argv 2] # Get user and root password # set no echo #system stty -echo #stty -raw echo #stty raw -echo # this does not work under AIX 4.3.3 for some reason # stty: impossible in this context #are you disconnected or in a batch, at, or cron script?stty: ioctl(user): bad file number # # while executing #"stty -echo" # (file "./runremote" line 22) # #stty -echo # I hate doing this but can't figure out the prompting problem. # hardcode passwords here for specific hosts but recommend blanking # these variables out when not using the script # remote low level password set upass "aaaaaaa" # local low level password (for ftp) set lpass "bbbbbb" # remote root (or su -) password set rpass "cccccc" # put a switch in here for non-standard root passwords switch $host { "hosta" { set rpass "dddddd" } "hostb" { set rpass "eeeeee" } } # Prompt for password #send_user "password?\ " #expect_user -re "(.*)\n" #send_user "password (for $user) on $host: " # expect_user -re "(.*)\n" # send_user "\n" # send "$expect_out(1,string)\r" # set echo back on #system stty echo #stty echo # sign on to remote host as low-level account and pull the file from this host set timeout 60 spawn telnet $host expect "ogin:" send "$uname\r" expect "word:" send "$upass\r" exec sleep 6 send "sh\r" # set a known prompt so we can expect it send "export PS1=$prompt\r" send "ftp $myhost\r" expect "Name*:" send "$luser\r" expect "Password:" send "$lpass\r" expect "ftp>" send "ascii\r" expect "ftp>" send "hash\r" expect "ftp>" send "lcd /tmp\r" expect "ftp>" send "cd $pwd\r" expect "ftp>" send "get $file\r" expect "ftp>" send "quit\r" expect "$prompt" send "chmod +x /tmp/$file\r" expect "$prompt" send "su -\r" expect "word:" send "$rpass\r" exec sleep 6 send "sh\r" send "export PS1=$rprompt\r" expect "$rprompt" send_user "\n+++ Beginning Command as root: $file +++\n" send "/tmp/$file\r" expect "$rprompt" send_user "\n+++ Ended Command as root: $file +++\n" send "rm -f /tmp/$file\r" expect "$rprompt" send "exit\rexit\r" expect "$prompt" send "exit\r" #exec sleep 2 #send "exit\r" close wait send_user "\nrunrootremote.exp: Complete.\n" exit