The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
Article Feedback - scp tricks
 New Topic  |  Go to Top  |  Go to Topic  |  Search  |  Log In   Newer Topic  |  Older Topic 
 ssh tricks
Author: James Flemer 
Date:   08-03-03 03:52

First off, anyone who uses ssh or scp with lots of different hosts should take the time to read the ssh_config(5) man page. It is very useful if you have accounts with different usernames, see the 'User' parameter.

Ok, now, since this article was about scp, let me talk about some nice things you can do with ssh. You all know that copying a whole directory tree sometimes causes problems, and that is why it is common to use tar to ensure permissions, ownership and symlinks are handled correctly. The example in tar(1) is:

tar -cf - -C srcdir . | tar xpf - -C destdir

Now, consider this when the destination is a remote host. Ssh can be used to make this very simple, just say:

tar -cf - -C srcdir . | ssh remote_host tar xpf - -C destdir

where 'srcdir' is on the local host, and destdir is on the remote.
Similarly, to copy from a remote host:

ssh remote_host tar -cf -C srcdir . | tar xpf - -C destdir

Another way I use ssh is to redirect output of a command on one host to a file on another host. For example, if I was compiling something and for some reason wanted the make log on another computer, I would run (using a Bourne shell):

make buildworld 2>&1 | ssh remote_host cat \> /tmp/make.log

The output of buildworld is sent over ssh to the cat process on the remote host, which just writes it to it's stdout. In this case it's stdout is redirected to /tmp/make.log. Make sure you escape the redirect (>) on the command line so it is evaluated by the remote not the local host.

Have fun.

Reply To This Message
 Re: ssh tricks
Author: James Flemer 
Date:   08-03-03 03:55

Oops there is a missing - in the second ssh+tar example. It should read:

ssh remote_host tar -cf - -C srcdir . | tar xpf - -C destdir


ssh remote_host tar -cf -C srcdir . | tar xpf - -C destdir


Reply To This Message
 Re: ssh tricks
Author: pierre-yves verdon 
Date:   08-03-03 11:00

i use quite the same trick but i finally gzip the transfered directory.
tar -zcvf - DIRECTORY/ | /usr/bin/ssh -l LOGIN HOST 'cat - > file.tgz'

it's also possible to use ssh with rsync:
rsync -rltgpvz --exclude=log/ -e ssh LOGIN@HOST:/remote_dir .


Reply To This Message
 Re: ssh tricks
Author: Andy Harrison 
Date:   13-03-03 21:25

I keep this little script on my machines. Sucks files into the current directory.

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
printf "\n\n\tExample:\n\n\t\tsshtar username hostname_or_ip /dir/to/copy\n\n"
exit 0
/usr/local/bin/ssh $1@$2 "cd $3 ; tar cf - ." | tar xvfBp -

Reply To This Message
 Re: ssh tricks
Author: Ben 
Date:   14-03-03 10:40

When using stdio over ssh, it's sometimes a good idea to use "ssh -e none" to make it have no escape character, as there is a non-zero chance your data may contain the relevant magic to make ssh exit (:

Also you can do fun things like:

tar cf - /home | ssh -e none user@host \( cd /home \; tar xvf - \)

to change dir when untarring stuff over ssh.

Reply To This Message
 Re: ssh tricks
Author: frank 
Date:   24-03-04 23:30

useless use of cat award?

Reply To This Message
 Re: ssh tricks
Author: Dan 
Date:   24-03-04 23:53

frank wrote:

> useless use of cat award?

Comments such as the above fall into the "I'm smarter than you!" category. Without explanation, they are worthless.

Frank, perhaps if you could explain why you think it is useless people might learn.


Reply To This Message
 Forum List  |  Threaded View   Newer Topic  |  Older Topic 

 User Login
 User Name:
 Remember my login:
 Forgot Your Password?
Enter your email address or user name below and a new password will be sent to the email address associated with your profile.
How to get the most out of the forum