Skip to content

Instantly share code, notes, and snippets.

@toonetown
Last active May 20, 2021 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toonetown/1cac8d0fb8f862781944d6bcf68e87b9 to your computer and use it in GitHub Desktop.
Save toonetown/1cac8d0fb8f862781944d6bcf68e87b9 to your computer and use it in GitHub Desktop.
An expect script that will spawn ssh and automatically enter the password
#!/usr/bin/expect
#
# A script to handle automatic entering of ssh passwords. It is intended that this script
# mainly be used from within a docker image in conjunction with docker_pipe to avoid
# placing private keys and passwords in your image.
#
# This script can be used as a drop-in replacement for ssh, as all parameters are passed
# to the ssh command.
#
# To use (reading the password from the command line):
# $ read -s EXPECT_SSH_PW
# $ EXPECT_SSH_PW="${EXPECT_SSH_PW}" ssh_expect USER@HOST
#
# Or, in conjunction with docker_pipe:
# EXPECT_SSH_PW="$(docker_pipe read)" ssh_expect USER@HOST
eval spawn -noecho /usr/bin/ssh [lrange $argv 0 end]
set timeout -1
if { [info exists ::env(EXPECT_SSH_PW)] } {
expect {
"Are you sure you want to continue connecting (yes/no)?" { send "yes\n"; exp_continue }
"Password:" { send "$::env(EXPECT_SSH_PW)\n"; exp_continue }
"login:" { interact }
eof
}
} else {
puts "No EXPECT_SSH_PW set - dropping to interactive\n"
interact
}
catch wait result
exit [lindex $result 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment