24

Using scp and interactively entering the password the file copy progress is sent to the console but there is no console output when using sshpass in a script to scp files.

$ sshpass -p [password] scp [file] root@[ip]:/[dir]

It seems sshpass is suppressing or hiding the console output of scp. Is there a way to enable the sshpass scp output to console?

jacknad
  • 13,483
  • 40
  • 124
  • 194
  • I believe the best option is NOT to use `sshpass`. If you can, use pubkey authentication. If you need more freedom in control what is going on, you will have to write it as `expect` script, I guess. – Jakuje May 05 '16 at 12:46
  • 2
    This is an embedded target. sshpass is what is available for now. – jacknad May 05 '16 at 12:53
  • Try out this link so that you may find [http://unix.stackexchange.com/questions/188291/how-to-view-progress-with-sshpass-and-scp-in-linux] – Ajith P May 20 '16 at 04:06

3 Answers3

8

After

sudo apt-get install expect

the file send-files.exp works as desired:

#!/usr/bin/expect -f

spawn scp -r $FILES $DEST
match_max 100000
expect "*?assword:*"
send -- "12345\r"
expect eof
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
3

Not exactly what was desired, but better than silence:

SSHPASS="12345" sshpass -e  scp -v -r $FILES $DEST 2>&1 | grep -v debug1

Note that -e is considered a bit safer than -p.

Output:

Executing: program /usr/bin/ssh host servername, user username, command scp -v -t /src/path/dst_file.txt
OpenSSH_6.6.1, OpenSSL 1.0.1i-fips 6 Aug 2014
Authenticated to servername ([10.11.12.13]:22).
Sending file modes: C0600 590493 src_file.txt
Sink: C0600 590493 src_file.txt
Transferred: sent 594696, received 2600 bytes, in 0.1 seconds
Bytes per second: sent 8920671.8, received 39001.0
mgutt
  • 5,867
  • 2
  • 50
  • 77
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • why `-e` is considered a bit safer than `-p`? – FarK Nov 20 '19 at 09:51
  • 5
    @FarK `SSHPASS="12345" sshpass -e ps aux | grep sshpass` shows `sshpass -e ps aux` while `sshpass -p 12345 ps aux | grep sshpass` shows `sshpass -p zzzzz ps aux` and there is a short period of time that ps may show `-p 12345`. In addition, there is `history | grep shpass`, but commands preceded by a space are not saved in history... – 18446744073709551615 Nov 20 '19 at 10:10
0

In this way:

output=$(sshpass -p $PASSWD scp -v $filename root@192.168.8.1:/root 2>&1)

echo "Output = $output"

you redirect the console output in variable output.

Or, if you only want to see the console output of scp command, you should add only -v command in your ssh pass cmd:

sshpass -p $PASSWD scp -v $filename root@192.168.8.1:/root

Community
  • 1
  • 1
Fabio_MO
  • 713
  • 1
  • 13
  • 26
  • 5
    `scp -v` just prints debugging output, it doesn't give the same progress output that a non-`sshpass`ed scp would. Redirecting sshpass output to a variable doesn't provide the scp progress output either. This answer isn't. – studog Mar 22 '18 at 13:49
  • I think this is clearly not the answer the questioner was looking for – vipcxj May 23 '23 at 03:29