Skip to content

Instantly share code, notes, and snippets.

@wutiger555
Created July 22, 2021 06:44
Show Gist options
  • Save wutiger555/7f2246d7420afbb294d9191631215975 to your computer and use it in GitHub Desktop.
Save wutiger555/7f2246d7420afbb294d9191631215975 to your computer and use it in GitHub Desktop.
#!/usr/bin/env expect
set ERR_UNEXPECTED_OUTPUT 1
set SMALL_TIMEOUT 30
set MEDIUM_TIMEOUT 300
set LONG_TIMEOUT 1800
set LOGIN_RETRY_NUMBER 3
set timeout $SMALL_TIMEOUT
set hostname [lindex $argv 0];
set username [lindex $argv 1];
set password [lindex $argv 2];
# ssh connect
spawn ssh -o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
$username@$hostname
expect_after {
timeout {
send_user "\n\n*** ERROR: unexpected output\n"
exit $ERR_UNEXPECTED_OUTPUT
}
}
set i 1
# login
expect {
"login:" {
send "$username\n"
exp_continue
}
"password" {
send "$password\n";
sleep 1
incr i
if { $i > $LOGIN_RETRY_NUMBER } {
send_user "\n\n*** ERROR: retry login $LOGIN_RETRY_NUMBER fail\n"
exit $ERR_UNEXPECTED_OUTPUT
}
exp_continue
}
eof {
send_user "\n\n*** ERROR: server has closed the connection\n"
exit $ERR_UNEXPECTED_OUTPUT
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment