expect免交互式命令---坑!!!

时间:2023-02-11 16:57:21

#!/usr/bin/expect
#设置变量
set host 192.168.126.137
set user root
set password ​​​123.com​

#开启新进程式交互
spawn ssh expect免交互式命令---坑!!!host
#从上面开启的进程中识别对应的信息然后发送字符串
expect {
"*yes/no" {send "yes\r";exp_continue}
"*password:" {send "$password\r"}
}
#链接远程主机后识别终端前 ]#
expect "]#"
#总结,shell中在expect中执行命令不能使用双引号,且命令与交互式分开,不然命令无法执行且报错
send {​​​ps -elf | grep -v grep | grep '/usr/lib/systemd/systemd-journald' | awk -F ' ' '{print $4}' >> /data/1.log​​​ & }
send "\r"
expect "]#" {send "exit\r" }

​expect相关命令见链接

【1】​​ Bluehitomi的Linux操作系统expect命令远程获取执行后的返回值​