1

I'm connecting from one remote machine to many remote machines within one network. Username and password for all remote machines is the same. I want to write a script that will use my username and pass to login to machine. I can't install any tool on host machine (expect or sshpass). Copying public key to target remote machine doesn't make a sense since I rarely connect more than once to the same machine. Everyday - new machines.

Is it possible to set up ssh on host machine to be able to connect to remote machines without need to set something on target machines?

Local machine that I'm using to connect to the rest of the network - SUSE Linux Enterprise Server 11 SP3

  • 2
    Using keys is the proper way to do it, but you can do it in a very insecure and non-recommended way by writing directly the password into the script using sshpass: $ sshpass -p <password> ssh user@hostname – Zumo de Vidrio Jan 3 '17 at 13:37
  • Can you install software on your own machine, the one which connects to the "one" remote machine that connects to the "many"? – icarus Jan 3 '17 at 13:44
  • @icarus, yes I can install software on my local machine that I'm using to connect to intermediate remote machine to reach target hosts. – Vitalii Plagov Jan 3 '17 at 13:58
  • @Zumo de Vidrio, is this a correct syntax? #!/bin/bash ssh $1 sshpass -p _pass_ ssh user@$1 – Vitalii Plagov Jan 3 '17 at 14:02
  • @V.Plagov I just tried with: sshpass -p '<password>' ssh user@hostname and it works, remember to put the password between ' ' characters, I missed that in my previous comment. – Zumo de Vidrio Jan 3 '17 at 14:07
0

Given the requirements I would install expect on my local machine and use it to add in effect a new command. Let us call the remote hosts one and many1, many2 and the local machine mybox. Something like this (untested)

#!/usr/bin/expect
# connect to "one"
spawn ssh username@one
# next lines shouldn't be needed, should have shared keys for "one"
expect "assword: "
# provide password for "one", ending with "\r"
send -- "the_password_for_username@one\r"
# mainloop
interact "~p" {
    send "ssh manyuser@[lindex $argv 0]\r"
    expect "yes/no)? " { send "yes\r" exp_continue } "assword: "
    send "the_password_for_many\r"
    }

Run this on mybox as ./script many1, it will connect you to one

Then when you want to connect to many1 just type ~p. It will run the ssh command, answer the "do you want to connect" question if asked, and provide the password.

Of course the standard comments about the insecurity of storing passwords in scripts apply. The other slight downside is that you need to dedicate a key sequence to this. Here I am using ~p but you could use a function key.

  • Thank you for the answer. But sorry, I'm not sure I have understand it. Where should I run that script? On mybox or on one? And what should I write in here "seek the one true way\r" and here "go out to the many\r"? – Vitalii Plagov Jan 3 '17 at 18:58
  • thank you. How should I run this script on mybox? How to pass to the script address of target host? – Vitalii Plagov Jan 4 '17 at 6:37
  • Install expect on mybox, and add the script. As written the script expects that you were always running ssh username@one, edit the third line accordingly. either make the script executable or run expect -f scriptname. If the one host varies or the username varies then we need more code to pass them in. – icarus Jan 4 '17 at 7:27
  • can we continue in chat? Since I'm completely confused... – Vitalii Plagov Jan 4 '17 at 7:34
  • Yes, I don't have enough reputation to open chat, so if you can make it - I will be very thankful :) – Vitalii Plagov Jan 4 '17 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.