sshpass with SSH works, but sshpass with SCP doesn't

49,418

You use an alternative port with ssh -p 2122 but not with scp.

Try

sshpass -p 'myPass' scp -P 2122 ~/myDir/testPB.txt [email protected]:/chroot/tomcat/testPB

Please note upper case P.

As per man scp:

-P port

    Specifies the port to connect to on the remote host.  Note that this option is written with a capital ‘P’, because -p is already reserved for preserving the times and modes of the file in rcp(1).
Share:
49,418

Related videos on Youtube

MrGarfield
Author by

MrGarfield

Updated on September 18, 2022

Comments

  • MrGarfield
    MrGarfield over 1 year

    In my case, I have a situation that I'm trying to send a file via SCP using sshpass but I can't. I need to use a script with a password but the easiest way doesn't work

    on the hostName2 I have no possibility to see configuration sshd_config etc. and send ssh-copy-id, I need use 'myPass'

    look at this:

    sshpass -p 'myPass' ssh -p 2122 [email protected]      
    

    ^ OK

    sshpass -p 'myPass' scp ~/myDir/testPB.txt [email protected]:/chroot/tomcat/testPB
    

    ^NOT OK

    It works fine:

    [tomcat@hostName .ssh]$ sshpass -p 'myPass' ssh -p 2122 [email protected]
    Last login: Mon Aug 22 11:41:32 2016 from xxx.xxx.xx.xxx
    #################
    # hostName2 #
    #################
    
    JAVA_HOME=/opt/java
    TOMCAT_HOME = /chroot/tomcat
    LOG = /log/tomcat , /log/apache
    LOG_ARCH = /log/arch/tomcat , /log/arch/apache
    STATS = /log/stats
    

    and there is a problem:

    [tomcat@hostName .ssh]$ sshpass -p 'myPass' scp -vvv ~/myDir/testPB.txt [email protected]:/chroot/tomcat/testPB
    Executing: program /usr/bin/ssh host 195.182.52.175, user tomcat, command scp -v -t /chroot/tomcat/testPB
    OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: Applying options for *
    debug2: ssh_connect: needpriv 0
    debug1: Connecting to xxx.xxx.xx.xxx [xxx.xxx.xx.xxx] port 22.
    debug1: Connection established.
    debug1: identity file /home/tomcat/.ssh/identity type -1
    debug1: identity file /home/tomcat/.ssh/identity-cert type -1
    debug3: Not a RSA1 key file /home/tomcat/.ssh/id_rsa.
    debug2: key_type_from_name: unknown key type '-----BEGIN'
    debug3: key_read: missing keytype
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug3: key_read: missing whitespace
    debug2: key_type_from_name: unknown key type '-----END'
    debug3: key_read: missing keytype
    debug1: identity file /home/tomcat/.ssh/id_rsa type 1
    debug1: identity file /home/tomcat/.ssh/id_rsa-cert type -1
    debug1: identity file /home/tomcat/.ssh/id_dsa type -1
    debug1: identity file /home/tomcat/.ssh/id_dsa-cert type -1
    debug1: identity file /home/tomcat/.ssh/id_ecdsa type -1
    debug1: identity file /home/tomcat/.ssh/id_ecdsa-cert type -1
    ssh_exchange_identification: Connection closed by remote host
    lost connection
    

    what's wrong?

    • Jakuje
      Jakuje almost 8 years
      Does the remote file exists? sshpass -p 'myPass' ssh [email protected] ls /chroot/tomcat/testPB
    • MrGarfield
      MrGarfield almost 8 years
      exists ;) the reason was different ! thanks for Your interest. solved
    • Арсений Черенков
      Арсений Черенков almost 8 years
      I vote against closing, problem is reproductible, (missing alternate port option in scp).
    • Jeff Schaller
      Jeff Schaller almost 8 years
      I cast a VTC under the "...or went away when a typo was fixed" umbrella. Good eyes spotting the difference in ports between the two commands!
  • MrGarfield
    MrGarfield almost 8 years
    It works! Great! i appreciate that, thanks for help