Skip to the content.
Examples

Scenario:

Host-a, Host-b are mutully unaccessible for sake of security. Is there a solution of copying a file from the Host-a to Host-b easily?

Answer:

Having a bridge Host-c which can access both Host-a and Host-b

image

file: example.spy - a file

# duplicating a file
srcfile = '/var/log/message'
dstfile = '/data/hosta/var-log-message'
pwd = 'secret'

# make ssh connection to host-a to download file
$.connect(f'user@host-a',password=pwd)
src,dst = $.download(srcfile)
$.close()

# make ssh connection to host-b to upload file
$.connect(f'user@host-b',password=pwd)
$.upload(dst,dstfile)
$.close()

# remove local copy
$rm dst
# or os.unlink(dst)

file: example.spy - a folder

# duplicating a folder
srcfolder = '/var/log'
dstfolder = '/data/hosta'
pwd = 'secret'

# make ssh connection to host-a to download file
$.connect(f'user@host-a',password=pwd)
$tar -zcf /tmp/varlog.tgz @{srcfolder}
src,dst = $.download('/tmp/varlog.tgz')
$.close()

# make ssh connection to host-b to upload file
$.connect(f'user@host-b',password=pwd)
$.upload(dst,'/tmp/varlog.tgz')
$tar -zxf -C @{dstfolder} /tmp/varlog.tgz
# remove temporary file
$rm /tmp/varlog.tgz
$.close()

# remove local copy
$rm dst
# or os.unlink(dst)

For more about “tar”, please see here.

execute the example.spy on Host-c

$sshscript example.spy
$sshscript example.spy --verbose
$sshscript example.spy --debug