Expect 简明例子

Expect 取参数:

set path [lindex $argv 1]

Expect 赋值:

set SUFFIX ".bak_[exec date +%Y%m%d_%H%M%S]" 

一个简单的例子:

#!/usr/bin/expect --
set timeout  120
set exec_password   [lindex $argv 0]
set exec_portNum    [lindex $argv 1]
set exec_localPath  [lindex $argv 2]
set exec_remoteIP   [lindex $argv 3]
set exec_remotePath [lindex $argv 4]

set SUFFIX ".bak_[exec date +%Y%m%d_%H%M%S]"
set BAKDIR "/data/backup/"

spawn rsync -av  $exec_localPath  pub@$exec_remoteIP#$exec_portNum:$exec_remotePath
expect  {
        "*conn*" {
                send yes/n;
                expect {
                         "*assword" {
                                send $exec_password;
                                sleep 120;
                                send /n;
                                expect  "*assword" {send $exec_password; sleep 25; send /n; wait }
                        }
                        "error*" { exit 1 }
                        "warn*" { exit 1 }
                        "ssh*" { exit 1 }
                        "scp*" { exit 1 }
                }
        }
        "*assword" {
                send $exec_password;
                sleep 1;
                send /n;
                 expect  "*assword" {send $exec_password; sleep 25; send /n; wait }
        }
        "error*" { exit 1 }
        "warn*" { exit 1 }
        "ssh*" { exit 1 }
        "scp*" { exit 1 }
}

你可能感兴趣的:(date,ssh,path)