expect腳本傳遞參數
#!/usr/bin/expect
set user [lindex $argv 0] //第一個參數
set host [lindex $argv 1] //第二個參數
set passwd "123123a"
set cm [lindex $argv 2] //第三參數
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
- ./3.expect [第一個參數] [第二個參數] [第三個參數]
[root@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 ls
spawn ssh root@192.168.133.130
root@192.168.133.130's password:
Last failed login: Thu Sep 21 01:57:01 CST 2017 from 192.168.133.131 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Sep 21 01:51:46 2017 from 192.168.133.1
[root@aminglinux-01 ~]# ls
anaconda-ks.cfg
[root@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 "ls;whoami;vmstat 1"
spawn ssh root@192.168.133.130
root@192.168.133.130's password:
Last login: Thu Sep 21 01:57:21 2017 from 192.168.133.131
[root@aminglinux-01 ~]# ls;whoami;vmstat 1
anaconda-ks.cfg
root
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 1470532 876 189616 0 0 12 2 42 93 0 0 100 0 0
0 0 0 1470532 876 189616 0 0 0 0 41 91 0 1 99 0 0
0 0 0 1470532 876 189616 0 0 0 0 33 74 0 0 100 0 0
0 0 0 1470532 876 189616 0 0 0 0 37 83 0 0 100 0 0
0 0 0 1470532 876 189616 0 0 0 0 35 77 0 0 100 0 0
^C
- 由於vmstat 1 是持續運行的。因此腳本最後的exit 就沒有辦法執行,只能手動終止