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 Commented Jan 3, 2017 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
    Commented Jan 3, 2017 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. Commented Jan 3, 2017 at 13:58
  • @Zumo de Vidrio, is this a correct syntax? #!/bin/bash ssh $1 sshpass -p _pass_ ssh user@$1 Commented Jan 3, 2017 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. Commented Jan 3, 2017 at 14:07

1 Answer 1


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)

# 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"? Commented Jan 3, 2017 at 18:58
  • thank you. How should I run this script on mybox? How to pass to the script address of target host? Commented Jan 4, 2017 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
    Commented Jan 4, 2017 at 7:27
  • can we continue in chat? Since I'm completely confused... Commented Jan 4, 2017 at 7:34
  • Yes, I don't have enough reputation to open chat, so if you can make it - I will be very thankful :) Commented Jan 4, 2017 at 8:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .