Skip to main content

Interact with shell locally or over different connection types (telnet, ssh, serial, adb)

Project description

citizenshell is 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.

Installation

citizenshell can simply installed using pip install citizenshell

Obtaining a shell

First you need a shell. For that you have several options:

  1. use the built-in LocalShell for quick access:

    from citizenshell import sh
  2. you can instanciate your own LocalShell:

    from citizenshell import LocalShell
    
    shell = LocalShell()
  3. you can instanciate the TelnetShell for shell over telnet:

    from citizenshell import TelnetShell
    
    shell = TelnetShell(hostname="acme.org", username="john",
                        password="secretpassword")
  4. you can instanciate the SecureShell for shell over SSH:

    from citizenshell import SecureShell
    
    shell = SecureShell(hostname="acme.org", username="john",
                        password="secretpassword")
  5. you can instanciate the AdbShell for shell over ADB:

    from citizenshell import AdbShell
    
    shell = AdbShell(hostname="acme.org", username="john",
                     password="secretpassword")
  6. 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)
  7. 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 also mix and match betweens providing arguments in the URI or via kwargs:

    telnetshell = Shell("telnet://john@acme.org", password="secretpassword", port=1234)
    serialshell = Shell("serial://john:secretpassword@/dev/ttyUSB3", baudrate=115200)

Using a shell

Once you have shell, any shell, you can call it directly and get the standart output:

assert shell("echo Hello World") == "Hello World"

or you can also iterate over the standard output:

result = [int(x) for x in shell("""
    for i in 1 2 3 4; do
        echo $i;
    done
""")]
assert result == [1, 2, 3, 4]

you don’t have to wait for the command to finish to recieve the lines:

for line in shell("for i in 1 2 3 4; do echo -n 'It is '; date +%H:%M:%S; sleep 1; done", wait=False)
    print ">>>", line + "!"

would produce something like:

>>> It is 14:24:52!
>>> It is 14:24:53!
>>> It is 14:24:54!
>>> It is 14:24:55!

you can extract stdout, stderr and exit code seperately:

result = shell(">&2 echo error && echo output && exit 13")
assert result.stdout() == ["output"]
assert result.stderr() == ["error"]
assert result.exit_code() == 13

you can inject environment variable to the shell

assert shell("echo $VAR", VAR="bar") == "bar"

or have the shell raise an exception if the exit code is non-zero:

assert shell("exit 13").exit_code() == 13 # will not raise any exception
try:
    shell("exit 13", check_xc=True) # will raise an exception
    assert False, "will not be reached"
except ShellError as e:
    assert True, "will be reached"

the shell can also raise an exception if something is printed on the standard error:

shell("echo DANGER >&2").stderr() == "DANGER" # will not raise any exception
try:
    shell("echo DANGER >&2", check_err=True) # will raise an exception
    assert False, "will not be reached"
except ShellError as e:
    assert True, "will be reached"

you can pull file from the remote host (for LocalShell it’s just doing a copy):

shell("echo -n test > remote_file.txt")
shell.pull("local_file.txt", "remote_file.txt")
assert open("local_file.txt", "r").read() == "test"

or push file to the remote host (again, for LocalShell it’s just doing a copy):

open("local_file.txt", "w").write("test")
shell.push("local_file.txt", "remote_file.txt")
assert str(shell("cat remote_file.txt")) == "test"

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

citizenshell-2.0.3.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

citizenshell-2.0.3-py2.py3-none-any.whl (20.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file citizenshell-2.0.3.tar.gz.

File metadata

  • Download URL: citizenshell-2.0.3.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for citizenshell-2.0.3.tar.gz
Algorithm Hash digest
SHA256 8e4f04cccc2cfa34e146706402895c623005a335d4dde3714d9835899059072c
MD5 93365ca80a2448b1d8cfccdbb0c28d73
BLAKE2b-256 69570a25cf1a0f095eaf8e326559d5cdad9239a2dfadc0edecdceafc94b48eb1

See more details on using hashes here.

File details

Details for the file citizenshell-2.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for citizenshell-2.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 94f703abd24eb4da1c5e0663221a57d3248852b3f26d5e193b1886c4abc7d107
MD5 a0b8f7f4f4894ad38db620c4e278a6de
BLAKE2b-256 f4c364714afb412babb0d47e96f8977ec7dac1ff439436a4435e04d5957acddf

See more details on using hashes here.

Supported by

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