views:

48

answers:

1

I know about RSA authentication, but for my purposes I want to use a heredoc to specify the password. I want something like the following, but I can't get it to work. Is this even 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 it:

$ ./testssh 
Enter Password: 
Pseudo-terminal will not be allocated because stdin is not a terminal.
user@myhost's 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  A: 

Programs that read passwords often specifically open /dev/tty to defeat redirection. In which case, the tool you need is 'expect', which will run one behind a pseudo-tty.

bmargulies
@bmargulies Can you take a look at the code in my edit? Thanks.
B Johnson
@B Johnson it has been, literally, years since I used expect. I see that Dennis helped you with the details.
bmargulies
Keep in mind that Expect is a general-purpose language which can do everything bash does, and arguably more easily. Those of us who chose to write Expect scripts, as opposed to Expect-embedded-uncomfortably-in-bash, avoid a lot of the "quoting hell" evident in the example above.
Cameron Laird