Can I use heredoc to enter a password in bash?

I know about RSA authentication, but for my purposes I want to use heredoc to specify a password. I want something like the following, but I cannot get it to work. Is it possible?

#!/bin/bash echo -n "Enter Password: " read -s password ssh myhost << EOL $password echo "I'm logged onto myhost" EOL echo done 

This is what I get when I try:

 $ ./testssh Enter Password: Pseudo-terminal will not be allocated because stdin is not a terminal. user@myhost password: Warning: No xauth data; using fake authentication data for X11 forwarding. Warning: no access to tty (Bad file descriptor). Thus no job control in this shell. mypassword: Command not found. I'm logged onto myhost done 

EDIT:

Based on bmargulies answer, I reworked my script and came up with the following:

 #!/bin/bash echo -n "Enter the Host: " read HOST echo -n "Enter Username: " read USER echo -n "Enter Password: " read -s PASS VAR=$(expect -c " spawn ssh $USER@$HOST expect \"password:\" send \"$PASS\r\" expect \">\" send \"ls\r\" send \"echo 'I\'m on $HOST'\r\" expect -re \"stuff\" send \"logout\" ") echo -e "\n\n\n========" echo VAR = "$VAR" echo done 
+2
bash heredoc ssh expect
Sep 26 '10 at 2:18
source share
2 answers

Programs that read passwords often specifically open / dev / tty to abort the redirection. In this case, the tool you need is an β€œexpect” that will run one after the pseudo-tty.

+4
Sep 26 '10 at 2:21
source share

If you mix in w / perl, you can do something β€œclean” (from without having to view a quote) as follows:

 #!/bin/bash cmd="ssh myhost << EOL" echo -n "Enter Password: " read -s password # answer password prompt # note we use ctl-A as our quote delimeter around the password so we run # no risk of it escaping quotes script=' use Expect; use ysecure; my $exp = new Expect; $exp->raw_pty(1); $exp->spawn(q|<CMD>|); $exp->expect(30,">"); $exp->send(q^A<PASSWORD>^A . "\n"); $exp->soft_close(); $exp->exitstatus() && die; ' script=${script//<CMD>/$cmd} script=${script//<PASSWORD>/$password} perl -e "$script" 
0
Jan 11 '11 at 18:53
source share



All Articles