Script to login to ssh twice

Written by:

I need to login through ssh twice with a script and can’t get it to work. This is what I have in my script file:

#!/usr/bin/expect

set login "user"
set addr "address1"
set addr2 "address2"
set pw "password"

spawn ssh $login@$addr
expect "$login@$addr's password:"
send "$pwr"
expect "$login@host:"
spawn ssh $addr2
expect "$login@$addr's password:"
send "$pwr"
interact

but this fails with the error:

user@host:~$ ssh: Could not resolve hostname address2: Name or service not known
send: spawn id exp7 not open
    while executing
"send "$pwr""

If I change the line spawn ssh $addr2 with exec ssh $addr2 it fails with the error:

user@host:~$ ssh: Could not resolve hostname address2: Name or service not known
    while executing
"exec ssh $addr2"

What do I need to change in order to make this work?

ssh is unable to resolve the hostname you are giving it, and so it errors out. You are then telling expect to send the password, but there is nothing to send it to since ssh threw the error and quit. So you need to check the hostname you are giving it, since it isn’t resolving. Or you can switch to an IP address. But it is probably better to find out why it can’t figure out who “address2” is.

kurtm

Lookup ssl key generation. You generate a pair of keys (public and private), you keep the private key(s) you generate on your system, and place the public keys on systems you need to access through ssh. Then ssh does not prompt you for password, and you don’t need expect.

Here is a script, “sskkeygen.sh”, I use to generate keys, so I can generate different keypairs for different systems,

#!/bin/bash
SYSTEM=${1:-boogie}
COMMENT=${2:-"Key for $SYSTEM work"}
ssh-keygen -t dsa -f ~/.ssh/id_dsa.$SYSTEM -C "$COMMENT"
chmod 600 ~/.ssh/id_dsa.$SYSTEM
chmod 600 ~/.ssh/id_dsa.$SYSTEM.pub

Copy the ~/.ssh/id_dsa.$SYSTEM.pub key to the remote system. Be Careful – the instructions all say copy the keyfile, but you want to append it!

Then you need a file ~/.ssh/config which tells which keys to use for which hosts.

#key: prod
Host 1.2.3.4
  IdentityFile ~/.ssh/id_dsa.prod
  User meself

#key: dev/test
Host 2.3.1.4
  IdentityFile ~/.ssh/id_dsa.dev
  User meself

#key: ftp dropbox
Host dropbox.company.com
  IdentityFile ~/.ssh/id_dsa.dropbox
  User virtualuser

#key: ftp thing
Host aaa.bbb.thing.com
  IdentityFile ~/.ssh/id_dsa.think
  User neato

#key: work
Host *.work.com
  IdentityFile ~/.ssh/id_dsa.work
  User workid

Posting my own answer since I found a way around this issue. Here’s the script that actually works:

#!/usr/bin/expect

set login "user"
set addr "address1"
set addr2 "address2"
set pw "password"

spawn ssh $login@$addr
expect "$login@$addr's password:"
send "$pwr"
expect "$login@host:"
send "ssh $addr2r"
expect "$login@$addr's password:"
send "$pwr"
interact

So basically I replaced the line spawn ssh $addr2 with send "ssh $addr2r". I was trying to spawn another ssh from where I started instead of starting another ssh on the host I first ssh’d to.

kurtm

Script to login to ssh twice
0 votes, 0.00 avg. rating (0% score)

Leave a Reply