amazon web services - Running expect script on EC2 hangs, but runs successfully when manually invoked -
i'm writing expect script start ssh tunnel.
it gets run on ec2 when instance starts, part of deployment creates script .ebextensions config file.
when script run, gets stuck @ point:
enter passphrase key '/home/ec2-user/id_data_app_rsa':
if run same script manually on server succeeds , can see tunnel process running.
ps aux | grep ssh
root 19046 0.0 0.0 73660 1068 ? ss 16:58 0:00 ssh -i /home/ec2-user/id_data_app_rsa -p222 -vfn -l 3306:x.x.x.x:3306 root@x.x.x.x
i can verify script reading ssh_passphrase correctly printing console.
set password $::env(ssh_passphrase) send_user "retrieved env variable : $password " this debug output ec2 logs:
enter passphrase key '/home/ec2-user/id_data_app_rsa': interact: received eof spawn_id exp0 i'm baffled why it's getting no further here when ec2 deployer runs, continues when run manually.
this script in .ebextensions, script starts @ #!/usr/bin/expect:
files: "/scripts/createtunnel.sh" : mode: "000755" owner: root group: root content: | #!/usr/bin/expect exp_internal 1 set timeout 60 # set variables set password $::env(ssh_passphrase) send_user "retrieved env variable : $password " spawn -ignore hup ssh -i /home/ec2-user/id_data_app_rsa -p222 -vfn -l 3306:x.x.x.x:3306 root@x.x.x.x expect { "(yes/no)?" { send "yes\n" } -re "(.*)assphrase" { sleep 1; send -- "$password\n" } -re "(.*)data_app_rsa" { sleep 1; send -- "$password\n" } -re "(.*)assword:" { sleep 1; send -- "$password\n" } timeout { send_user "un-able login: timeout\n"; return } "denied" { send_user "\nfatal error: denied \n"} eof { send_user "closed\n" ; return } } interact
your problem here:
set password $::env(ssh_passphrase) and way shell works environment variables. when script invoked, assume environment variables set. depending on how script invoked, $::env(ssh_passphrase) may not set, resulting in variable null/blank. when init scripts (or cloud-init) run, not run environment of login shell. should not assume .profile or /etc/profile environment variables set, rather source or set them explicitly.
a possible solution may
. ~ec2-user/.profile /path/to/above.script
Comments
Post a Comment