TAG | ssh
Suppose that you need to transfer a file from hostA to hostC. Between these hosts is a hostB that you use to hop through. Maybe between hostA and hostB is a firewall or maybe you are traversing networks or something. Here are some ways that you can transfer that file.
cat myfile.txt | ssh hostB "( ssh hostC '( cat > /tmp/myfile.txt )' )"
This basically copies myfile.txt from hostA to hostC, but you would need to have the ability to ssh from hostB to hostC
Here is another way to do it using a tunnel. This is useful if you cannot ssh from hostB to hostC due to key restrictions.
Make a script like this…
#!/bin/bash ssh -nNT -L 2222:hostC:22 hostB & tunnel_id=$! sleep 2 cat myfile.txt | ssh localhost -p 2222 '(cat > /tmp/myfile.txt)' kill $tunnel_id
Note that you can also use scp in the 2nd example to transfer the file.
How can you copy a file from host1 through host2, and end up on host3? Here is my quick hack solution for a scp / ssh copy.
cat file.txt | ssh -A user1@host2 ‘cat | ssh -A user@host3 “cat > file.txt”‘
or, you can just set up a tunnel from host1 to host3 like this
ssh -L 9999:host3:22 host2
this will open up the local port 9999 which gets tunnelled through host2 and ends up at host3. Now you can do commands like this.
scp -P 9999 file.txt user2@localhost:file.txt
and there you have it!

