0

Can we use wget and scp as a pipeline. I want to wget a file from a server and copy to another server. I used below command but it is not working.

wget "$Select_Release_Version_ARTIFACT_URL" | sudo scp test@192.168.94.137:/etc/test/
1

To use wget in a pipe, you must make sure it writes to stdout instead to a file, so use wget -O- ....

AFAIK, you can't use scp to copy from stdin. However, you can use ssh instead, and have it execute a command like cat, which reads from stdin.

So together you get something like

wget -O- "$Select_Release_Version_ARTIFACT_URL" | ssh test@192.168.94.137 'cat > /etc/test/some_file'

Note that will only download and transfer a single file/webpage. Also note that user test on 192.168.94.137 needs rights to create and write to /etc/test/some_file. Using sudo before ssh won't give the remote user test any additional rights. It will allow to access ssh keys of root on the local machine, so if that was the intention, keep it.

While this demonstrates how to use a pipe over ssh, it would have been easier to just execute wget on the remote machine, unless there are reasons why this is not possible.

0

Instead of pipe, you can use &&. This will make sure that when scp runs if wget has exit status of 0 (finished successfully).

wget "$Select_Release_Version_ARTIFACT_URL" && sudo scp "$Select_Release_Version_ARTIFACT_URL" test@192.168.94.137:/etc/test/

This works as following:

wget downloads the file and IF SUCCESSFUL, it will initiate scp.

NOTE: Since you are using sudo, this will ask the password test user every time this command will run.

  • I used passwordless authentication, I think no need of providing password. – Janith Nov 15 '18 at 5:35
  • When I using above command I am getting error. wget is working but scp is not working. – Janith Nov 15 '18 at 5:42
  • Note that if the named file already exists, wget will download into filename.1, filename.2, etc. – Ulrich Schwarz Nov 15 '18 at 5:52
  • Yes can I write a scrip to download and copy to remote machine same time? – Janith Nov 15 '18 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.