Scenario:
Ssh to a remote host, su as root and download root’s files
Solution:

file: example.spy
file = '/root/file.tgz'
tmpfile = '/tmp/file.tgz'
pwd = 'secret'
user = 'user'
host = 'host'
# make ssh connection
$.connect(f'{user}@{host}',password=pwd)
# sudo as root to modify file's mode
with $sudo -S su as console:
console.expect('password')
console.sendline(pwd)
# copy as a temporary file
console.sendline(f'cp {file} {tmpfile}')
# set owner of the temporary file to "user"
console.sendline(f'chown {user} {tmpfile}')
# download the temporary file with "user"
src, dst = $.download(tmpfile)
print('downloaded as ', dst)
# remove the temporary file
$rm @{tmpfile}
# close the connnection
$.close()
# ls folder on localhost
$ls -l
execution examples
$sshscript example.spy
$sshscript example.spy --verbose
$sshscript example.spy --debug
Origin: https://www.facebook.com/groups/gpython/posts/10157054268769579/
Last Updated: 2026-07-25 16:59:40
Table of contents
- SSHScript v3.1 Documentation
- Accounts (legacy URL)
- Checking the syncing status of two MySQL servers
- Getting Started with SSHScript
- Page not found
- Q: What can it do that Netmiko doesn’t do? (origin)
- SSHScript Documentation
- SSHScript Documents
- SSHScript v2.0 CLI(Draft)
- SSHScript v2.0 Commands Execution and Console
- SSHScript v2.0 Console of sudo and su
- SSHScript v2.0 Getting Started
- SSHScript v2.0 Interactive Commands and Foreground Commands
- SSHScript v2.0 Make Connections
- SSHScript v2.0 Recipes
- SSHScript v2.0 Reference Guide
- SSHScript v2.0 Stdout, Stderr and Exitcode
- SSHScript v2.0 Threading Support
- SSHScript v2.0 Upload and Download Files
- SSHScript v2.0.2 Release Notes
- SSHScript v3.0 Interactive and Foreground Commands
- SSHScript v3.0 Reference Guide
- SSHScript v3.0.0 Release Notes
- SSHScript v3.1
- SSHScript vs. Ansible
- 用 Python 作系統自動化