SCP files from one host to another

Everyone knows how to copy files around using SCP, but it can be a pain if you have to enter passwords for every copy. If you have an administration host with shared ssh keys to every other host, you can just use a quick little one liner to drag files from hostA, through the admin box, over to hostB:

adminbox # ssh hostA "tar cf - /usr/local/sbin/myscript.sh 2>/dev/null" | ssh hostB "cd / && tar xvf - 1>/dev/null"

Using tar, the file is output to STDOUT and piped over ssh, then read from STDIN. It copies /usr/local/sbin/myscript.sh from hostA to hostB. Because the admin box has ssh keys to both hostA and hostB, the process is automatic and does not require password authentication. This means you can use this method in scripts for batch copies, etc. Also, you won’t have to create a temporary copy on the admin host.

Drop it into a simple shell script and it will be even easier:

#!/bin/bash
# FILE: file_dragger.sh
# AUTHOR: fordodone <fordodone at email.com>
# DATE: 2013/07/11
# NOTES: drags a file from one host to another
#

if [ $# -ne 3 ]
then
echo ""
echo "usage: </full/path/to/file> <src> <dst>"
echo ""
exit
fi

ssh $2 "tar cf - $1 2>/dev/null" | ssh $3 "cd / && tar xvf - 1>/dev/null"

To use it for the original copy example do this:

# file_dragger.sh /usr/local/sbin/myscript.sh hostA hostB
#

extract Debian package

To extract a Debian package and view it’s contents first make a temporary directory to work in, move the .deb file to that location, and extract it:

# mkdir /tmp/rssh
# mv rssh_2.3.2-13squeeze3_i386.deb /tmp/rssh
# cd /tmp/rssh
# ar vx rssh_2.3.2-13squeeze3_i386.deb 
# ls -l
total 128
-rw-r--r-- 1 root root  5094 Jun  5 09:13 control.tar.gz
-rw-r--r-- 1 root root 54142 Jun  5 09:13 data.tar.gz
-rw-r--r-- 1 root root     4 Jun  5 09:13 debian-binary
-rw-r--r-- 1 root root 59428 Nov 27  2012 rssh_2.3.2-13squeeze3_i386.deb

As you can see, the contents have been extracted, and we see the typical 3 files, that are needed for a proper Debian package. The debian-binary file contains information about the version of the package. The control.tar.gz file contains configuration scripts, and tells dpkg about how to install and configure the package. data.tar.gz is the one we are interested in. It contains the actual files that will be set in place at time of installation. The contents of data.tar.gz mirror the typical Linux filesystem tree. Make a directory for the contents of data.tar.gz and extract the tarball into it.


# mkdir debroot
# tar -C debroot -xzf data.tar.gz 
# find debroot/ -ls
   950    4 drwxr-xr-x   4 root     root         4096 Nov 22  2012 debroot/
   956    4 drwxr-xr-x   3 root     root         4096 Nov 22  2012 debroot/etc
   960    4 -rw-r--r--   1 root     root         1867 Nov 22  2012 debroot/etc/rssh.conf
   957    4 drwxr-xr-x   3 root     root         4096 Nov 22  2012 debroot/etc/logcheck
   958    4 drwxr-xr-x   2 root     root         4096 Nov 22  2012 debroot/etc/logcheck/ignore.d.server
   959    4 -rw-r--r--   1 root     root          455 Nov 22  2012 debroot/etc/logcheck/ignore.d.server/rssh
   961    4 drwxr-xr-x   5 root     root         4096 Nov 22  2012 debroot/usr
   965    4 drwxr-xr-x   5 root     root         4096 Nov 22  2012 debroot/usr/share
   971    4 drwxr-xr-x   3 root     root         4096 Nov 22  2012 debroot/usr/share/doc
   972    4 drwxr-xr-x   3 root     root         4096 Nov 22  2012 debroot/usr/share/doc/rssh
   977    8 -rw-r--r--   1 root     root         4162 Nov 27  2005 debroot/usr/share/doc/rssh/SECURITY.gz
   975    4 -rw-r--r--   1 root     root         2207 Nov 22  2012 debroot/usr/share/doc/rssh/README.Debian
   981    4 -rw-r--r--   1 root     root           46 Jul 21  2003 debroot/usr/share/doc/rssh/TODO
   976    8 -rw-r--r--   1 root     root         6297 Nov 22  2012 debroot/usr/share/doc/rssh/changelog.Debian.gz
   983    4 -rw-r--r--   1 root     root          680 Nov 22  2012 debroot/usr/share/doc/rssh/NEWS.Debian.gz
   973    4 -rw-r--r--   1 root     root         2575 Nov 22  2012 debroot/usr/share/doc/rssh/copyright
   979    4 drwxr-xr-x   2 root     root         4096 Nov 22  2012 debroot/usr/share/doc/rssh/examples
   980    8 -rwxr-xr-x   1 root     root         6479 Nov 22  2012 debroot/usr/share/doc/rssh/examples/mkchroot.sh
   974    8 -rw-r--r--   1 root     root         4409 Nov 27  2005 debroot/usr/share/doc/rssh/CHROOT.gz
   982    4 -rw-r--r--   1 root     root         1488 Jan  3  2006 debroot/usr/share/doc/rssh/changelog.gz
   978    4 -rw-r--r--   1 root     root         1636 Dec 29  2004 debroot/usr/share/doc/rssh/README
   984    4 drwxr-xr-x   2 root     root         4096 Nov 22  2012 debroot/usr/share/rssh
   985    4 -rwxr-xr-x   1 root     root         1321 Nov 22  2012 debroot/usr/share/rssh/conf_convert
   966    4 drwxr-xr-x   4 root     root         4096 Nov 22  2012 debroot/usr/share/man
   969    4 drwxr-xr-x   2 root     root         4096 Nov 22  2012 debroot/usr/share/man/man1
   970    8 -rw-r--r--   1 root     root         5771 Nov 22  2012 debroot/usr/share/man/man1/rssh.1.gz
   967    4 drwxr-xr-x   2 root     root         4096 Nov 22  2012 debroot/usr/share/man/man5
   968    4 -rw-r--r--   1 root     root         2277 Nov 22  2012 debroot/usr/share/man/man5/rssh.conf.5.gz
   986    4 drwxr-xr-x   2 root     root         4096 Nov 22  2012 debroot/usr/bin
   987   24 -rwxr-xr-x   1 root     root        22288 Nov 22  2012 debroot/usr/bin/rssh
   962    4 drwxr-xr-x   3 root     root         4096 Nov 22  2012 debroot/usr/lib
   963    4 drwxr-xr-x   2 root     root         4096 Nov 22  2012 debroot/usr/lib/rssh
   964   24 -rwxr-xr-x   1 root     root        21184 Nov 22  2012 debroot/usr/lib/rssh/rssh_chroot_helper

Now we can browse, and take a look at the contents of the files.