Interact with shell locally or over different connection types (telnet, ssh, serial, adb)
Project description
citizenshell is (or rather will be) a python library allowing to execute shell commands either locally or remotely over several protocols (telnet, ssh, serial or adb) using a simple and consistent API. This library is compatible with both python 2 (2.7) and 3 (>=3.4) as well as with PyPy. For now, it focuses on POSIX platforms like Linux and MacOS, but may be extended to work to Windows based platform in the future. It is distributed under MIT license.
Installing
citizenshell can simply installed using pip install citizenshell
Examples
LocalShell
you can use the built-in sh command for simple commands:
from citizenshell import sh
assert sh("echo Hello World") == "Hello World"
you can instanciate a LocalShell for more complex cases:
from citizenshell import LocalShell
shell = LocalShell(GREET="Hello")
assert shell("echo $GREET $WHO", WHO="Citizen") == "Hello Citizen"
you can also iterate over stdout:
from citizenshell import LocalShell
shell = LocalShell()
result = [int(x) for x in shell("""
for i in 1 2 3 4; do
echo $i;
done
""")]
assert result == [1, 2, 3, 4]
or you can extract stdout, stderr and exit code seperately:
from citizenshell import LocalShell
shell = LocalShell()
result = shell(">&2 echo error && echo output && exit 13")
assert result.stdout() == ["output"]
assert result.stderr() == ["error"]
assert result.exit_code() == 13
TelnetShell
you can instanciate the TelnetShell for shell over telnet:
from citizenshell import TelnetShell
shell = TelnetShell(hostname="acme.org", username="john", password="secretpassword")
assert shell("echo Hello World") == "Hello World"
you can then do eveything you can do with a LocalShell.
SecureShell
you can instanciate the SecureShell for shell over SSH:
from citizenshell import SecureShell
shell = SecureShell(hostname="acme.org", username="john", password="secretpassword")
assert shell("echo Hello World") == "Hello World"
you can then do eveything you can do with a LocalShell.
AdbShell
you can instanciate the AdbShell for shell over ADB:
from citizenshell import AdbShell
shell = AdbShell(hostname="acme.org", username="john", password="secretpassword")
assert shell("echo Hello World") == "Hello World"
you can then do eveything you can do with a LocalShell.
SerialShell
you can instanciate the SerialShell for shell over serial line:
from serial import EIGHTBITS, PARITY_NONE
from citizenshell import SerialShell
shell = SerialShell(port="/dev/ttyUSB3", username="john", password="secretpassword", baudrate=115200, parity=PARITY_NONE, bytesize=EIGHTBITS)
assert shell("echo Hello World") == "Hello World"
you can then do eveything you can do with a LocalShell.
Shell
you can also obtain shell objects by URI using the Shell function:
from citizenshell import Shell
localshell = Shell()
telnetshell = Shell("telnet://john:secretpassword@acme.org:1234")
secureshell = Shell("ssh://john:secretpassword@acme.org:1234")
adbshell = Shell("adb://myandroiddevice:5555")
serialshell = Shell("serial://jogn:secretpassword@/dev/ttyUSB3?baudrate=115200")
you can mix and match betweens providing arguments in the URI or via kwargs:
from citizenshell import Shell
localshell = Shell()
telnetshell = Shell("telnet://john@acme.org", password="secretpassword", port=1234)
serialshell = Shell("serial://john:secretpassword@/dev/ttyUSB3", baudrate=115200)
you can then use the shell objects as you would any other.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for citizenshell-2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc3c5815380b2e8ddc676e564876e767b21fef85714ae7730c724ce4beb88ce3 |
|
MD5 | e4e41ec95e0e7b9847926afde938d5b4 |
|
BLAKE2b-256 | 9a5289f9960533ae62c5516c7acb91fde4ad18d902d32267af1cf7a20f16d15e |