20.30 expect script to pass parameters

expect script to pass parameters

  • pass parameters
#!/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 [first argument] [second argument] [third argument]
[root@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 ls
spawn ssh [email protected]
[email protected]'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
  • Support multiple parameters
[root@aminglinux-02 sbin]# ./3.expect root 192.168.133.130 "ls;whoami;vmstat 1"
spawn ssh [email protected]
[email protected]'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
  • Because vmstat 1 is running continuously. So there is no way to execute the last exit of the script, it can only be terminated manually

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325065725&siteId=291194637