自动化交互工具 - expect

expect.sh版
#!/usr/bin/expect
set user "root"
set ip "192.168.30.90"
set passwd "123123"

spawn ssh $user@$ip
expect {
"yes/no" {send "yes\n";exp_continue} #加上exp_continue程序不会退出该条expect
"password" {send "$passwd\n"}
}
expect "#"
send "ls\n"
expect "#"
send "exit\n"
expect eof

shell.sh版
#!/bin/bash

user="root"
ip="192.168.30.90"
passwd="123123"

expect << EOF
spawn ssh $user@$ip
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "$passwd\r"}
}
expect "#"
send "ls\r"
expect "#"
send "exit\r"
expect eof
EOF

``
posted @ 2026-08-06 13:52  Dog17  阅读(7)  评论(0)    收藏  举报