SSHScript Documents
v1.1.17 supports streaming-style commands (tcpdump). Released on 2022/9/22, Release Notes
en
- Tutorial
- Learning SSHScript Chapter 1
- Learning SSHScript Chapter 2
- Syntax, Variables and Functions
- sshscript CLI
- sshscript Module
- Ways to get a root’s console
- Do logging
- Troubleshooting of installation
zh-TW
- 導覽(Tutorial)
- 使用SSHScript教學一(Learning SSHScript Chapter 1)
- 使用SSHScript教學二(Learning SSHScript Chapter 2)
- 用 Python 作系統自動化
- 安裝有問題
Introduction
System automation is the process of using computer software to automatically perform routine or repetitive tasks. SSHScript is a tool that makes it easy to use Python for system automation by providing a simple syntax for executing commands on local or remote hosts. With SSHScript, you can embed commands and networking in your Python scripts, and SSHScript will handle the execution and output, so you don’t need to have expertise in the subprocess module or Paramiko (ssh) to use it.
Below is an example. It makes an ssh connection to the host1, then from the host1 makes a connection to the host2. Then It executes “netstat -antu” on the host2.
# file: hello.spy
# 1. connect to host1
with $.connect('username1@host1', password='secret') as _:
# 2. connect to nested host2
with $.connect('username2@host2', password='secret') as _:
# 3. execute a command
$netstat -antu
# 4. handle outputs
with open('netstat.log','w') as fd:
fd.write($.stdout)
If you did “ssh-copy-id” to remote hosts in advance, you don’t even need to give the password.
# login by a ssh key
$.connect('username1@host1')
# login by a ssh key in a path
$.connect('username1@host2',pkey=$.pkey('/path/to/keyfile'))
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 _:
$.download('/var/log/message')
$.upload('config.ini','/tmp')
Below is a longer example, it makes an ssh connection to a remote host, then prints out all its IP addresses.
# regular python script
import unicodedata,re
from getpass import getpass
password = getpass()
#### start of SSHScript's block
# 1. ssh to the remote host
with $.connect('username@host',password=password) as _:
# 2. execute command
$ifconfig | grep inet
# 3. collect the output
content = $.stdout
#### end of SSHScript's block
# handle the outputs in 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]
myIp.add(ip)
print(myIp)
Suppose that the file is named “hello.spy”, then execute it on console by
sshscript hello.spy
the SSHScript’s CLI “sshscript” would transfer hello.spy into a regular python script, then execute the script. In fact, “hello.spy” can contain any python statements.
You can also use SSHScript as a regular python package by “import sshscript”. The documents page has examples for your reference.
Install
pip install sshscript
Upgrade
pip install sshscript --upgrade
or
pip install sshscript==1.1.17
Examples
Why and Features
The idea behind SSHScript is to provide a simple and intuitive way to automate common tasks that involve running commands and dealing with outputs on local and remote hosts. SSHScript makes it easy to write scripts that can connect to remote hosts via ssh and execute commands, and it allows you to process the resulting data directly in Python. This enables you to build complex data structures and processing flows using an object-oriented approach.
Some key benefits of using SSHScript include:
-
Easy to use. If you know what commands to run and which host to connect to via ssh, you can write your script without having to learn any extra concepts.
-
Embedding shell commands in Python scripts is intuitive and self-explanatory, making it easy to collaborate and maintain your scripts.
-
Handling execution outputs and exceptions is easier in Python than it is in shell scripts.
-
Your scripts can leverage the vast ecosystem of Python packages.
-
SSHScript supports parallel execution of commands using threads.