Scenario:
ssh to remote hosts and run commands in parallel by threads more complex example.

Note: this example requires sshscript version >= 1.1.12
file: example.spy
# If the localhost can connect host-b, host-c directly,
# We don't need to connect bridge host, just comment-out the next line.
$.connect('user@host-a')
$ hostname
print('connected to',$.stdout)
def job(accounts,ret,pkey):
# this is required
global job
account = accounts.pop(0)
$.connect(account,pkey=pkey)
$ hostname
print('connected to',$.stdout)
hostname = $.stdout.strip()
# knowing which ip makes the connection
# and ensures that "exeution in shell" works
$$'''
echo $SSH_CLIENT
cd /tmp
pwd
'''
# ensures that "single command execution" works
twoDollarsResult = $.stdout
$ date
# collect data
oneDollarsResult = $.stdout
ret[hostname] = '\n'.join([f'@{oneDollarsResult}',twoDollarsResult])
# recursively ssh to next hosts
if len(accounts):
# load ssh key again from current connected host
pkey = $.pkey('/home/user/.ssh/id_rsa')
job(accounts,ret,pkey)
else:
$.close()
pkey = $.pkey('/home/user/.ssh/id_rsa')
# ssh to host-b1 to do the task, then ssh to host-b2 from host-b1 to do the task
accounts1 = ['user@host-b1','user@host-b2']
ret1 = {}
t1 = $.thread(target=job,args=(accounts1,ret1,pkey))
# ssh to host-c1 to do the task, then ssh to host-c2 from host-c1 to do the task
accounts2 = ['user@host-c1','user@host-c2']
ret2 = {}
t2 = $.thread(target=job,args=(accounts2,ret2,pkey))
t1.start()
t2.start()
t1.join()
t2.join()
from pprint import PrettyPrinter
pp = PrettyPrinter(indent=4)
pp.pprint(ret1)
pp.pprint(ret2)
execute the example.spy
$sshscript example.spy
$sshscript example.spy --verbose
$sshscript example.spy --debug
$sshscript example.spy --debug --verbose
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 作系統自動化