Crawlicious

tools for web business

strange ssh tricks

| 0 comments

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.

Leave a Reply