sshpass: command not found error
Asked Answered
P

3

23

I am trying to automate the file transfer or FTP from one server to the other.

#!/bin/bash
### In this model, the same filename is processed on each run.
### A timestamp is added to the result file and data file is copied to the archive or error folder with a timestamp after processing.

# Set current directory
cd `dirname "$0"`

# Set the environment variables
. ./Environment.sh $0

#######################################################################################################
# 
#######################################################################################################


FILE=/hcm/Inbound/file.csv

sshpass -p 'xyz' sftp -oBatchMode=no -b - -oStrictHostKeyChecking=no [email protected] <<_EOF_

cd /upload/

put $FILE

_EOF_

# Exit
exit $?

When I am executing this shell script I am getting the following error in putty :

 -bash: sshpass: command not found

I tried using the ssh passwordless method by ssh-keygen -t dsa and other steps but I cannot access putty of the second server due to which I am not being able to execute the next steps.

Kindly help

Petrifaction answered 30/6, 2016 at 17:34 Comment(2)
"Cannot access putty of the second server"? Pardon? You can use passwordless SSH through multiple hops by way of loading your key into an agent (as an aside, RSA keys are preferred over DSA) and enabling agent forwarding; you don't need to actually have a private key available on your bounce hosts.Flapper
As an aside, exit $? is completely redundant: exit passes through the exit status of the immediately prior command by default. Also, you're missing a fair bit of quoting here -- which shellcheck.net will identify.Flapper
R
52

you will need to install sshpass on the client server you are running your code in which is a tool that is not installed by default on most Linux distro

if you are in Ubuntu use this command

apt-get install sshpass

on centOS/redhat use this install epel

wget https://archives.fedoraproject.org/pub/archive/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm -ivh epel-release-6-8.noarch.rpm

install sshpass

yum --enablerepo=epel -y install sshpass

Thanks

Rellia answered 30/6, 2016 at 17:37 Comment(13)
I edited the answer to include the way to install it in centos/redhat, what os you use ?Rellia
Loaded plugins: refresh-packagekit, security Error getting repository data for epel, repository not foundPetrifaction
What is your Linux distrobution? like ubuntu , centOS redhat etc ? and what version is it ?Rellia
sorry new to linus. do not have an idea on that . is there a command to find it ?Petrifaction
do the commands here to know your distro cyberciti.biz/faq/find-linux-distribution-name-version-number and tell meRellia
Red Hat Enterprise Linux Server release 6.6 (Santiago)Petrifaction
added the section to the answer on on centOS/redhat use this install epelRellia
warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Permission denied)Petrifaction
user is not in the sudoers file. This incident will be reported.Petrifaction
Let us continue this discussion in chat.Rellia
wget download.fedoraproject.org/pub/epel/6/x86_64/… --2021-02-20 14:41:20-- download.fedoraproject.org/pub/epel/6/x86_64/… Resolving download.fedoraproject.org... 209.132.190.2, 67.219.144.68, 38.145.60.21, ... Connecting to download.fedoraproject.org|209.132.190.2|:80... connected. HTTP request sent, awaiting response... 404 Not Found 2021-02-20 14:41:20 ERROR 404: Not Found.Thuja
has this been changed?Thuja
@Thuja I updated the link, old link got moved so i put the new linkRellia
P
9

NO!!!! Don't install sshpass. It is the wrong tool for your job.

It was not written for your use case, and if you do use it, your script will be considerably less secure than it can be. I should know what I'm talking about. I wrote it.

Instead, run your server with debugging info and figure out why you failed to set up key based authentication. It is preferable to using sshpass in every possible way.

Puppis answered 30/6, 2016 at 18:11 Comment(4)
I do not have acces to putty on server 2Petrifaction
Then you google "setting up public key authentication", and you run ssh with -vv to see what authentication methods it tries, and if all else fails, you ask why public key doesn't work (assuming none of the 7,283,093 other times people asked that very question didn't help you). You do not use sshpass where public key auth is possible.Puppis
Thanks ! what Hani suggested is working ! So i think my question is answered :)Petrifaction
Normally I'd agree, but some of us do not have control over the administration of the host we're trying to connect. In my case, I'd love to use the SSH AuthorizedKeys method, but our admin refuses to do that, insisting on passwords.Zoomorphism
S
3

One solution I got for CentOS 7 :

  1. Download sshpass from here

And rpm will be downloaded.

  1. Transfer this rpm to your linux system (you can use filezilla etc.).
  2. Install the Rpm using: yum install <rpm file name>.

DONE

Sullyprudhomme answered 30/12, 2019 at 10:53 Comment(1)
The link to sshpass is broken. Updated LinkChristopherchristopherso

© 2022 - 2024 — McMap. All rights reserved.