scp is used to perform secure copy between different hosts.
By default, scp doesn’t let you pass password as a parameter. For instance, for mysql, I can do something like: mysql -uusername -ppassword and login without having to give username and password input separately. That is very helpful while scripting or automating things because that’s one less case to handle.
So how do you pass password as parameter in scp? You cannot. At least not using scp on its own. The best method I could find is by using expect.
expect is a scripting language used to deal with scripts/programs that require user interaction. Here we’re going to use expect to start scp (with spawn) and send password as our response without any interaction from our end — all in a single line.
expect -c 'spawn scp ./path/to/source/filename user@server.com:~/path/to/destination;expect password;send "serverpass\r";interact'
That’s it. Obviously, ./path/to/source/filename.csv is the path to your file that you are trying to copy (source file), /path/to/destination is the path where you are trying to copy it to on a remote host (destination directory), destination remote host here is user@server.com, and serverpass is the password of the destination host.
You may need to install expect if its not already installed:
- apt-get install expect
- yum install expect
No related posts.
[redacted]
February 18, 2021 @ 12:51 am
By the way, on some systems you will want \n instead of \r. This is the case on MacOS.