linux远程登录,此脚本可直接进行测试ip、username、password替换正确的即可
#!/usr/bin/expect
# set:设置变量
set timeout 30
set host "172.17.0.5"
set username "root"
set password "test@123"
# spawn:启动新的进程
spawn ssh $username@$host
# expect:从进程接收字符串(捕获字符串:*password*),\r字段,表示换行
expect "*password*" {send "$password\r"}
expect "*connecting*" {send "yes\r"}
# send:用于向进程发送字符串
send "touch test.txt\r\n"
send "touch test1.txt\r\n"
# interact:允许用户交互
interact
# 执行完需要等一会