expect远程登录节点执行命令

set user "root"
set password "root"
set host "192.168.0.112"
set timeout 10

spawn ssh $user@$host
expect {
    "*(yes/no)?" {
        send "yes\r"
        exp_continue
    }
    "password:" {
        send "$password\r"
    }
}

expect "#" { send "cat a 2> /dev/null;echo $?\r" }
expect "1" { send "touch a;echo $?\r" }
expect "0" { send "exit\r" }
expect eof

posted on 2024-04-02 22:07  王景迁  阅读(14)  评论(0编辑  收藏  举报

导航