Last active
May 20, 2021 11:58
-
-
Save toonetown/1cac8d0fb8f862781944d6bcf68e87b9 to your computer and use it in GitHub Desktop.
An expect script that will spawn ssh and automatically enter the password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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