Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to automate ssh login WITH passwd (not passphraseless ssh)

Regardless of security issues, I want to automate ssh login by putting password into a script file (in form of plaintext). For example, I tried following, but without success...

echo "mypassword" | ssh -X root@remote_node_address

it still prompt with password inputs...

Edit: I am aware of setting up passphraseless ssh (and actually have done this). What my question really is is how to automate process of setting up passphraseless ssh...

like image 266
Richard Avatar asked Dec 21 '22 16:12

Richard


2 Answers

Automate with Expect

You can use Expect to drive password authentication with SSH. For example:

#!/usr/bin/expect -f
set timeout -1
spawn ssh -o PubkeyAuthentication=no host.example.com
expect -exact "Password: "
send -- "secret\r"
expect {\$\s*} { interact }

This script is a very basic example, and not especially robust in the face of failure or when running under a non-standard remote TERM like GNU screen, but it works for the common case. You can also use /usr/bin/autoexpect from the expect-dev package to generate your own custom scripts based on a manual session.

like image 83
Todd A. Jacobs Avatar answered Dec 23 '22 05:12

Todd A. Jacobs


you will need to use public key authentication, see

http://www.ece.uci.edu/~chou/ssh-key.html

in order to add new keys for existing hosts, you will need to automate updating of public keys in ~/.ssh/authorized_keys on remote machine

it is easy to do with

ssh-keygen -t rsa -b 1024 -f ~/.ssh/new-key -P ""
cat ~/.ssh/new-key.pub | ssh root@target-host 'cat >> ~/.ssh/authorized_keys'

then you can use new key to access host with

ssh -i ~/.ssh/new-key root@remote-host
like image 21
jdevelop Avatar answered Dec 23 '22 06:12

jdevelop