20.30 expect脚本传递参数

2022-01-06 14:11:03 浏览数 (2)

expect脚本传递参数

  • 传递参数
代码语言:javascript复制
#!/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 "yesr"}
"password:" { send "$passwdr" }
}
expect "]*"
send "$cmr"
expect "]*"
send "exitr"
  • ./3.expect [第一个参数] [第二个参数] [第三个参数]
代码语言:javascript复制
[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
  • 支持多条参数
代码语言:javascript复制
[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 就没有办法执行,只能手动终止

0 人点赞