Skip to the content.

SSHScript Documents




SSHScript makes Python become a scripting tool for system automation. Functionally, the SSHScript is something like the Ansible. Instead of writing descriptive YML files, just write Python scripts with the simplicity of Python and the power of all Python packages.

The sshscript let you embed shell commands in python script. It is like writing shell-script in python.

Below is an example. It makes ssh connection to the host1, then from the host1 makes connection to the host2. Then It executes “netstat -antu” on the host2.

$netstat -antu

Or, to be explicit,

with $.connect('username1@host1') as _:
    with $.connect('username2@host2') as _:
        $netstat -antu

Put the three lines into a file, say “hello.spy”, then execute it on your console by

sshscript hello.spy

If you did not “ssh-copy-id” to the host1 and host2, then just give the password like this

$.connect('username1@host1', password='secret')

Doing nested-scp is simple too. The script below downloads the /var/log/message from the host2 and uploads config.ini on the localhost to /tmp on the host2.

with $.connect('username1@host1') as _:
    with $.connect('username2@host2') as _:

Your script is full-powered by Python.

# This script would ssh to a remote server.
# Then print out all its IP addresses.

# Below is regular python script
import unicodedata,re
from getpass import getpass
password = getpass()

# Below is python script with content of sshscript syntax
# First: ssh to username@host with password
# Second: execute command "ifconfig | grep inet"
$ifconfig | grep inet
# Third: collect the output
conten = $.stdout
# Close the connection, not required but my boss always hopes me to do.

# Below is regular python script
def remove_control_characters(s):
    global unicodedata,re
    s = re.sub(r'[\x00-\x1f\x7f-\x9f]', '',s)
    s = re.sub(r'\[.*?[a-zA-Z]', '',s)
    return s

myIp = set()
for line in content.split('\n'):
    line = line.strip()
    line = remove_control_characters(line)
    if line.startswith('inet'):
        cols = line.split()
        if cols[0] == 'inet':
            ip = cols[1].split(':')[-1]



The last version is 1.1.12 on 2022/8/8. Release Notes


pip install sshscript