ssh gateway script
spawn
: launch a new process
send
: send string to process(similar with typing in the terminal)
expect
: accecpt string from the process
exp_continue
: accecpt string from the process, and continue expect
interact
let user interact with terminal
#!/usr/bin/expect
set salt [lindex $argv 0];
set gateway1Username [Your gateway1 host];
set gateway1Pass [Your gateway1 password];
set gateway2Username [Your gateway2 host];
set gateway2Pass [your gateway2 password];
set server [Your target server host];
set serverPass [Your target server password];
set timeout -1
# connect gateway1
spawn ssh $gateway1Username
expect {
"*yes/no*" {
send "yes\n";
exp_continue;
}
"password:" {
send "$gateway1Pass\n";
}
}
# connect gateway2
expect {
"*something your should write*" {
send "ssh $gateway2Username\r";
exp_continue;
}
"*password:" {
send "$gateway2Pass\r";
}
}
# connect target
expect {
"*something your should write*" {
send "ssh $server\r";
exp_continue;
}
"*yes/no*" {
send "yes\n";
exp_continue;
}
"*password:" {
send "$serverPass\r";
}
}
interact