0

I am having multiple ssh commands to do some tasks for me. For eg:

ssh a-vm "rm -f /home/dir/file1.xlsx"
ssh a-vm "rm -f /home/dir/file2.xml"

scp me@b-vm:/somedir/file1.xlsx .
scp me@b-vm:/somedir/file2.xml .

1) Is there a way to combine 2 ssh commands into 1 and two scp commands into 1? 2) Is there a cost if I do ssh and scp multiple times instead of 1 time?

Any help is appreciated.

1 Answer 1

1

You can just do:

ssh a-vm "rm -f /home/dir/file1.xlsx ; rm -f /home/dir/file2.xml"
scp "me@b-vm:/somedir/{file1.xlsx,file2.xml}" .

Each ssh/scp call will cost you the connection time and some cpu time (could be significant if you do that to hundreds of machines at the same time, otherwise unlikely).

Alternatively you can use a persistent master connection for ssh and tunnel others over it. That will save a couple of network roundtrips - see http://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing

Sign up to request clarification or add additional context in comments.

3 Comments

ssh is executing only the first rm command and not the second one. My code failed at that point. so haven't checked if scp works fine or no.
You don't even need to call rm twice; a single call can remove multiple files.
Double check how you're calling this. Ssh doesn't care how many commands there are and doesn't even parse the string into commands. They're executed on the other side by your shell. This works - just figure out what went wrong. Especially check that both commands are quoted in one string, otherwise you're going to execute the second one locally. Scp part also works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.