21

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 zys@192.abc.taleo.net <<_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

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
sreekem bose
  • 451
  • 3
  • 12
  • 28
  • "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. – Charles Duffy Jun 30 '16 at 17:41
  • 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 http://shellcheck.net/ will identify. – Charles Duffy Jun 30 '16 at 18:16

3 Answers3

49

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

Hani
  • 1,354
  • 10
  • 20
  • I edited the answer to include the way to install it in centos/redhat, what os you use ? – Hani Jun 30 '16 at 17:42
  • Loaded plugins: refresh-packagekit, security Error getting repository data for epel, repository not found – sreekem bose Jun 30 '16 at 17:44
  • What is your Linux distrobution? like ubuntu , centOS redhat etc ? and what version is it ? – Hani Jun 30 '16 at 17:46
  • sorry new to linus. do not have an idea on that . is there a command to find it ? – sreekem bose Jun 30 '16 at 17:49
  • do the commands here to know your distro http://www.cyberciti.biz/faq/find-linux-distribution-name-version-number/ and tell me – Hani Jun 30 '16 at 17:51
  • Red Hat Enterprise Linux Server release 6.6 (Santiago) – sreekem bose Jun 30 '16 at 17:54
  • added the section to the answer on on centOS/redhat use this install epel – Hani Jun 30 '16 at 18:00
  • 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) – sreekem bose Jun 30 '16 at 18:01
  • user is not in the sudoers file. This incident will be reported. – sreekem bose Jun 30 '16 at 18:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116124/discussion-between-hani-and-sreekem-bose). – Hani Jun 30 '16 at 18:06
  • wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm --2021-02-20 14:41:20-- http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 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. – s.patra Feb 20 '21 at 09:16
  • has this been changed? – s.patra Feb 20 '21 at 09:16
  • @s.patra I updated the link, old link got moved so i put the new link – Hani Feb 21 '21 at 10:17
8

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.

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
  • I do not have acces to putty on server 2 – sreekem bose Jul 01 '16 at 05:09
  • 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. – Shachar Shemesh Jul 01 '16 at 08:41
  • Thanks ! what Hani suggested is working ! So i think my question is answered :) – sreekem bose Jul 01 '16 at 08:49
  • 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. – Woodsman Jun 14 '21 at 05:29
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

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • 2
    The link to `sshpass` is broken. [Updated Link](https://centos.pkgs.org/7/epel-x86_64/sshpass-1.06-1.el7.x86_64.rpm.html) – francis Oct 22 '20 at 06:34