Skip to main content

Write Python scripts to automate SSH and subprocess operations.

Project description

System automation is the process of repeating network and execution operations to achieve desired results. SSHScript is a Python library that makes it easy to create system automation processes. It provides a simple and intuitive interface for executing commands on local and remote hosts, without requiring any knowledge of the subprocess module or Paramiko (SSH).

SSHScript is technically an integration of the subprocess and Paramiko modules, but it presents an unified interface that is more convenient and expressive for system automation tasks. It is also pure Python, which means that SSHScript scripts can be easily integrated with other Python libraries and tools.

Here is an example of a simple script in SSHScript syntax(v2):

## filename: example.spy
## run: sshscript example.spy
$hostname
## get output by $.stdout, $.stderr and $.exitcode
print('localhost name is ', $.stdout.strip())
## get an interactive sudo console of locahost
with $.sudo('password') as sudoconsole:
    sudoconsole.exec_command('whoami')
    assert 'root' in sudoconsole.stdout
    sudoconsole.exec_command('ls -l $HOME')

## connect to remote by ssh
with $.connect('user1@host', 'password):
    $hostname
    ## get output by $.stdout, $.stderr and $.exitcode
    print('remote name is ', $.stdout.strip())
    ## get an interactive sudo console of remote host
    with $.sudo('password') as sudoconsole:
        sudoconsole.exec_command('whoami')
        assert 'root' in sudoconsole.stdout
        sudoconsole.exec_command('ls -l $HOME')
    with $.connect('user2@nestedhost', pkey=$.pkey('/home/user1/.ssh/id_rsa')):
        $hostname
        ## get output by $.stdout, $.stderr and $.exitcode
        print('nested remote name is ', $.stdout.strip())
        ## get an interactive sudo console of remote host
        with $.sudo('password') as sudoconsole:
            sudoconsole.exec_command('whoami')
            assert 'root' in sudoconsole.stdout
            sudoconsole.exec_command('ls -l $HOME')

Here is an example of a simple script with SSHScript module(v2):

## filename: example.py
## run: python3 example.py
import sshscript
from sshscript import SSHScriptSession
session = SSHScriptSession()
# Execute a command on the local host
session.exec_command('df')
for line in session.stdout.split('\n'):
    cols = line.split()
    if len(cols)>5: print(f'ussage of {cols[0]} is {cols[4]}')
## connect to remote by ssh
with session.connect('user1@host', 'password) as remote_session:
    # Execute a command on the remote host
    remote_session.exec_command('df')
    for line in remote_session.stdout.split('\n'):
        cols = line.split()
        if len(cols)>5: print(f'ussage of {cols[0]} is {cols[4]}')
    with remote_session.connect('user2@nestedhost', pkey=remote_session.pkey('/home/user1/.ssh/id_rsa') as nested_remote_session:
        # Execute a command on the remote host
        nested_remote_session.exec_command('df')
        for line in nested_remote_session.stdout.split('\n'):
            cols = line.split()
            if len(cols)>5: print(f'ussage of {cols[0]} is {cols[4]}')

SSHScript can be used to automate a wide variety of system tasks, such as:

  • Deploying and configuring servers

  • Managing backups and restores

  • Monitoring and troubleshooting systems

  • Automating repetitive tasks

SSHScript is a powerful tool for system automation, and it is easy to use, even for those with limited programming experience.

Releases

The new experimental release is 2.0.2 (2023/10/6). There are lots of changes, but documentation is on working.

The last stable version is 1.1.17. (2022/9/22). Relase Notes

More

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sshscript-2.0.2.tar.gz (51.0 kB view details)

Uploaded Source

File details

Details for the file sshscript-2.0.2.tar.gz.

File metadata

  • Download URL: sshscript-2.0.2.tar.gz
  • Upload date:
  • Size: 51.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.18

File hashes

Hashes for sshscript-2.0.2.tar.gz
Algorithm Hash digest
SHA256 1f80afb554f8e1b95ebda93b3577d9d1f1fcb6311e37872c83185587fbeafb06
MD5 1cdd7b35f6cb7543625e082fe83debf2
BLAKE2b-256 f87ecaf418927819d2906ba295cadb2f4248d40f300d26b06064ce1080d07e5a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page