1

a little ssh issue here.

I set up some keys in ssh between 2 machines and I can login via ssh without password in terminal.

I am trying to write a shell script that sends commands to the remote server through ssh. When i run the script it always asks for the ssh password before performing the commands. How can I make it so the ssh login in the script is passwordless like it is through terminal?

in the script it's like ssh user@remoteMachine 'do some stuff' the same command in terminal works perfectly fine.

(edit) verbose flag differences

script:

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/remote/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/remote/.ssh/id_dsa
debug1: Trying private key: /home/remote/.ssh/id_ecdsa
debug1: Next authentication method: password
user@192.168.1.4's password: 

terminal

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/remote/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.1.4 ([192.168.1.4]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_GB.UTF-8
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic i686)

the script is just

#/usr/bin/bash

ssh user@192.168.1.4 'touch ~/test'

I am making the script executable and calling it with

./script
  • You're running the script by hand in the same login session? Or you're running it in another session, e.g. with cron? – Barmar May 20 '14 at 5:32
  • Same login session currently – Raj Gupta May 20 '14 at 5:34
  • Try ssh -v to print verbose debugging info. What's the difference? – Barmar May 20 '14 at 5:36
  • 1
    Maybe you can post the script, the command to call the script and the command used in terminal? – Fabian May 20 '14 at 10:52
  • 1
    The first line of the script should be #!/bin/bash (shebang), but this wouldn't cause your problem. Do you have an alias for ssh? What is the output of type ssh? – Gilles May 20 '14 at 23:09
0

You can install sshpass package on you machine and after that For running commands remotely:

#!/bin/bash
SCRIPT='
#Your commands
'
sshpass -p<pass> ssh -o 'StrictHostKeyChecking no' -p <port> user@host "$SCRIPT"

You also have sshpass -f<file> and you can use file of passwords for all of your servers...So you can write a loop for making ssh and doing stuff automatically...

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.