10

I am trying to run a sshpass command inside a bash script but it isn't working.

If I run the same command from the terminal it works fine but running it in a bash script it doesn't.

#! /bin/bash

sshpass -p 'password' ssh user@host command

I am aware of the security issues but its not important now.

Can someone help? Am I missing something.

Thanks

hansaplast
  • 11,007
  • 2
  • 61
  • 75
rcsimoes
  • 103
  • 1
  • 1
  • 6
  • What does it mean "does not work" ? Can you explain it better, please? Can `you echo $?` after sshpass call? – Grzegorz Oct 10 '13 at 17:45
  • 1
    Are you running the script from a cron job? Maybe sshpass isn't in `$PATH` because the cron job doesn't run your `.profile`. – Barmar Oct 10 '13 at 17:46

5 Answers5

19

Try the "-o StrictHostKeyChecking=no" option to ssh("-o" being the flag that tells ssh that your are going to use an option). This accepts any incoming RSA key from your ssh connection, even if the key is not in the "known host" list.

sshpass -p 'password' ssh -o StrictHostKeyChecking=no user@host 'command'
Jotunn
  • 394
  • 3
  • 7
16

Do which sshpass in your command line to get the absolute path to sshpass and replace it in the bash script.

You should also probably do the same with the command you are trying to run.

The problem might be that it is not finding it.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • Ok,now it doesnt work. let me just put the command I want to passa here. I even tried to put all the paths : /usr/bin/sshpass -p 'password' /usr/bin/ssh pi@192.168.2.103 sudo /bin/sh /home/Projecto/AVstart_1.sh – rcsimoes Oct 13 '13 at 20:00
  • @rcsimoes What is the error you get? Run the exact command in a terminal and post the error you get. – Emil Davtyan Oct 14 '13 at 06:17
  • in the terminal the command worked, but it didnn't inside a a bash script, or in python using os.system. I already solve the problem y addind "StrictHostKeyChecking=no" to the command. Thanks for all the help here. I now have onother problem but i guess I should ask a new question, cause it isn't related with this – rcsimoes Oct 14 '13 at 13:05
8

1 - You can script sshpass's ssh command like this:

#!/bin/bash

export SSHPASS=password
sshpass -e ssh -oBatchMode=no user@host

2 - You can script sshpass's sftp commandlike this:

#!/bin/bash

export SSHPASS=password

sshpass -e sftp -oBatchMode=no -b - user@host << !
   put someFile
   get anotherFile
   bye
!
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • What does `sftp` have to do with it? – Barmar Oct 10 '13 at 17:47
  • @Barmar: See both examples. One shows ssh command another shows sftp – anubhava Oct 10 '13 at 17:50
  • So the only significant change you've made is to get the password from an environment variable instead of a command line option. Why would that solve the problem? – Barmar Oct 10 '13 at 17:54
  • @Barmar: I don't post an answer without testing/verifying it. I tested both commands and found them working. There are other differences in my command than just `an environment variable`. – anubhava Oct 10 '13 at 19:06
  • Your other change is `BatchMode=no`, which is the default. But if his problem was with that setting, his command wouldn't work when he runs it interactively, either. – Barmar Oct 10 '13 at 19:13
5

I didn't understand how the accepted answer answers the actual question of how to run any commands on the server after sshpass is given from within the bash script file. For that reason, I'm providing an answer.


After your provided script commands, execute additional commands like below:

sshpass -p 'password' ssh user@host "ls; whois google.com;" #or whichever commands you would like to use, for multiple commands provide a semicolon ; after the command

In your script:

#! /bin/bash

sshpass -p 'password' ssh user@host "ls; whois google.com;"
Secko
  • 7,664
  • 5
  • 31
  • 37
1

This worked for me:

#!/bin/bash

#Variables
FILELOCAL=/var/www/folder/$(date +'%Y%m%d_%H-%M-%S').csv    
SFTPHOSTNAME="myHost.com"
SFTPUSERNAME="myUser"
SFTPPASSWORD="myPass"
FOLDER="myFolderIfNeeded"
FILEREMOTE="fileNameRemote"

#SFTP CONNECTION
sshpass -p $SFTPPASSWORD sftp $SFTPUSERNAME@$SFTPHOSTNAME << !
    cd $FOLDER
    get $FILEREMOTE $FILELOCAL
    ls
   bye
!

Probably you have to install sshpass:

sudo apt-get install sshpass
Bisca
  • 6,380
  • 2
  • 19
  • 32