How to put sshpass command inside bash script?

I am trying to run the sshpass command inside a bash script, but it does not work.

If I run the same command from the terminal, it works fine, but it does not run in a bash script.

#! /bin/bash

sshpass -p 'password' ssh user@host command

I know the security issues, but now it doesn’t matter.

Can anyone help? Did I miss something.

thanks

+4
source share
5 answers

Do which sshpasson the command line to get the absolute path to sshpassand replace it in a bash script.

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

The problem may be that she does not find her.

+10
source

-o StrictHostKeyChecking = no ssh ( "- o" ), ssh, ). RSA- ssh-, " ".

sshpass -p 'password' ssh -o StrictHostKeyChecking=no user@host 'command'
+9

1 - script sshpass ssh :

#!/bin/bash

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

2 - script sshpass sftp commandlike this:

#!/bin/bash

export SSHPASS=password

sshpass -e sftp -oBatchMode=no -b - user@host << !
   put someFile
   get anotherFile
   bye
!
+7

, , , sshpass bash script. .


script :

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

script:

#! /bin/bash

sshpass -p 'password' ssh user@host "ls; whois google.com;"
+2
source

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
!

Maybe you need to install sshpass:

sudo apt-get install sshpass
+1
source

All Articles