expect通过堡垒机A:1.1.1.1 登陆B:2.2.2.2
将如下代码保存到文件login.expbash#!/usr/bin/expect -fif {$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 loginnameset login_pass 123456set login_ip 1.1.1.1set login_port 63008set timeout 10set 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_portexpect { "*(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