expect实现自动通过堡垒机登陆

expect通过堡垒机A:1.1.1.1 登陆B:2.2.2.2
将如下代码保存到文件login.exp

bash#!/usr/bin/expect -f
if {$argc < 3} {
    puts stdout "$argv0 err params\n"
    exit 1
}

proc do_console_login {login pass ip} {
    puts $login;
    puts $pass;
    puts $ip
}

# puts [llength $argv]

set login loginname
set login_pass 123456
set login_ip 1.1.1.1
set login_port 63008

set timeout 10
set user [lindex $argv 0]
set password [lindex $argv 1]
set ip [lindex $argv 2]
do_console_login $user $password $ip

# spawn ls -l

# expect "login_52.exp"

# expect eof

#登陆堡垒机
spawn ssh $login@$login_ip -p $login_port
expect {
    "*(yes/no) " {send "yes\n";exp_continue}
    "*assword:" {send "$login_pass\n"}
}   
expect "]$ "
send "ssh $user@$ip \n"
expect {
    "(yes/no)" {send "yes\n";exp_continue}
    "*assword:" {send "$password\n"}
}   

interact

运行如下命令:

bash./login.exp user password 1.1.1.1
    原文作者:MrRightLeft_秦磊
    原文地址: https://segmentfault.com/a/1190000002680406
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞