0

Is it possible to make a script that ssh to remote host, enters the passwords to log-in and then executes at least 1 command on the remote machine?

4
  • Is there a reason you can't use key-based login?
    – muru
    Sep 4, 2014 at 12:17
  • If you use ssh keys you can use a feature called forced commands. See binblog.info/2008/10/20/…
    – Panther
    Sep 4, 2014 at 12:17
  • you cannot provide a password to ssh unless typing it. If you want keyboard less run, you have to use key. ssh was design so that you don't have to leave clear text password in shell.
    – Archemar
    Sep 4, 2014 at 12:43
  • @Archemar there's a way using expect, i think.
    – muru
    Sep 4, 2014 at 13:03

1 Answer 1

4

To run a command, for example ls, at a remote host via a ssh, you can use:

ssh username@remote_hostname 'echo | ls'

Before you can do it without password you need to do the following:

  1. Generate public and private keys using:

    ssh-keygen
    
  2. Copy public key to remote host:

    ssh-copy-id -i ~/.ssh/id_rsa.pub remote_hostname
    

(The directory .ssh needs to exist on the remote host otherwise you need to create it.)

2
  • Of course, when generating the key pair, you must use an empty password or you will be prompted to enter it each time you use the key. If you have private keys with password, you can load them first into ssh-agent and you won't have to enter the key password anymore as long as ssh-agent is running.
    – Benoit
    Sep 4, 2014 at 13:03
  • You don't need an empty password. gnome-keyring will store what ever password you enter the first time you use ssh and then it is transparent.
    – Harris
    Sep 4, 2014 at 13:11

You must log in to answer this question.

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