Skip to main content

The cross-platform tool to execute bash commands remotely.

Project description

PyPI version Build Status

Plinux

Cross-platform tool to work with remote Linux OS.

Plinux based on paramiko project. It can establish ssh connection to a remote server, execute command as user or with sudo rights. Plinux returns object with exit code, sent command, stdout/sdtderr response.

Installation

For most users, the recommended method to install is via pip:

pip install plinux

Import

from plinux import Plinux

Usage

The most recommended usage way:

from plinux import Plinux

client = Plinux(host="172.16.0.124", username="bobby", password="qawsedrf")
response = client.run_cmd("hostname")
print(response.stdout)  # WebServer
print(response.ok)  # True
from plinux import Plinux

client = Plinux()
response = client.run_cmd_local("hostname")
print(response.stdout)  # WebServer
print(response.ok)  # True

Command using sudo:

from plinux import Plinux

client = Plinux(host="172.16.0.124", username="bobby", password="qawsedrf", logger_enabled=True)
response = client.run_cmd("systemctl stop myservicename.service", sudo=True)

print(response)  # SSHResponse(response=(0, None, None, "sudo -S -p '' -- sh -c 'systemctl stop myservicename.service'"))
print(response.command)  # sudo -S -p '' -- sh -c 'systemctl stop myservicename.service'
print(response.exited)  # 0

SFTP usage:

from plinux import Plinux

tool = Plinux(host="ftp.test.local", username="bobby", password="qawsedrf")
sftp = tool.sftp
print(sftp.listdir())

SQLite3 usage:

from plinux import Plinux

client = Plinux(host="cdn1.test.local", username="bobby", password="qawsedrf")

db_path = '/opt/ProductName/nxdb/StorageConfig.db'
sql = 'select Data from DtoDataContainer'
db = client.sqlite3(db_path, sql).json()
print(db)  # {"Settings1": 1, "Settings2": 2...,"Settings10": 10}
print(db['Setting1'])  # {"Settings1": 1}

Aliases

Some methods have "human commands" and aliases:

  • client.run_cmd("ls /home/bobby")
  • client.list_dir("/home/bobby")
  • client.ls("/home/bobby")

Changelog

UNRELEASED
2.0.2 (06.10.2022)
  • get_json() removed
  • cat_file() extended with pprint param
  • get_dir_size() fixed
2.0.1 (06.10.2022)
  • stdout type hint extended
  • get_ntp_servers() fixed to process different ways of data storing in /etc/systemd/timesyncd.conf
2.0.0 (05.10.2022)

Breaking changes:

  • response with expected exit code != 0 will be warned in log
  • ResponseParser changed and renamed to the SSHResponse
  • log level added
  • log format changed to match another apps
  • check_exists renamed to the .exists()
  • get_prelogin() new method added to read MOTD
  • stat_file() extended. "executable" and time of last access/birth/modification/satus
  • get_directory_size refactored and renamed to get_dir_size()
  • get_current_os_name() deprecated

Changed type of returned value: str:

  • get_service,
  • get_service_status,
  • get_service_journal,
  • get_ssl_md5,
  • get_ssl_certificate,

bool:

  • stop_service/kill_service/start_service/restart_service,
  • enable/disable,
  • get_netstat_info,
  • create_file,
  • clear_file,
  • remove,
  • extract_files,
  • copy_file,
  • reboot,
  • shutdown,
  • create_directory,
  • kill_user_session,

list:

  • get_ntp_servers
1.3.8 (22.06.2022)
  • get_ufw_status() new method added
  • change_hostname() obsolete
  • get_file_permissions() changes: renamed to stat_file() and return dict now
  • get_file_size() deprecated. use .stat_file()
  • __sudo_cmd removed
1.3.7 (21.06.2022)
  • get_package_info(): new method added
  • get_md5() returns md5 text only
  • list_dir() returns list of files now
  • get_free_space() returns string with free space
  • get_disk_usage() returns dict with disk usage
  • get_last_file() returns str with last file in directory
  • cat_file() returns str with file content
  • get_package_version() returns str with package version
  • get_hostname() returns str with hostname
  • get_os_version() returns dict with OS info
  • get_ip() returns str with IP address
1.3.6 (07.06.2022)
  • get_ip_addresses_show() fixed to return info for specific interface
1.3.5 (06.06.2022)

python-dateutil added as dependency.

New features:

  • get_ntp_servers()
  • get_time_date()
  • to_known_type() auxiliary method to convert string to python known data type
1.3.4 (03.06.2022)
  • get_ip_addresses_show() new method added
1.3.3 (17.04.2022)
  • refactored to manage logger state
1.3.2 (4.04.2022)
  • exceptions moved to separate "exceptions" module. e.g.
from plinux.exceptions import PackageNotFoundError

raise PackageNotFoundError
1.3.1 (4.04.2022)
  • logger replaced with the plogger package
1.3.0.post1 (22.03.2022)
  • code cleanup
1.2.9 (22.03.2022)

New method added:

  • .get_user_id()
1.2.8 (28.02.2022)
  • .sqlite3() extended with the timeout=1000 param
1.2.7 (22.02.2022)
  • PackageNotFoundError added
1.2.6 (18.02.2022)

New methods added:

  • .is_security_update_available()
  • .get_disk_size()
  • .get_directory_size()
  • .get_port_listeners_process_id()
  • .get_process_cmdline()
1.2.5 (07.02.2022)

.get_json() extended to decode BOM

1.2.4 (21.01.2022)

Created instance with local execution by default (host=127.0.0.1). Python minimum version now is 3.9 New method added:

  • get_installed_packages_versions: Get all installed packages and their versions. Returns dict. Minor: added some methods description
1.2.3 (7.12.2021)

New method added:

  • get_ssl_fingerprint
  • get_ssl_serial
  • get_pid fixed to process list of pids
1.2.2 (7.12.2021)

get_free_space fixed

  • Now it executes command towards "/" and with -h param by default.
  • method can accept different number of positional parameters as additional command argument ()
  • Specify empty mount_point (the first param as '') to get all disk info
1.2.1 (22.11.2021)

Added:

  • get_package_version
  • is_package_upgradable
  • clear_file
  • get_ssl_certificate
  • validate_ssl_key
  • get_ssl_md5
1.2.0 (14.10.2021)
  • logger added as attr for inheritance
1.1.9 (12.05.2021)
  • create_file, get_file_size, grep_line_in_file, change_line_in_file, delete_line_from_file, get_last_file, remove, extract_files, copy_file, get_md5, create_directory, list_dir to accept "sudo" param
1.1.8 (13.02.2021)
  • run_cmd_session method added. Can execute several commands one-by-one
  • ResponseParser updated to work with run_cmd_session (NOTE: exit code always be 0 for the "run_cmd_session")
1.1.7 (21.12.2020)

logger extended to catch destination host

1.1.6 (29.11.2020)

sqlite3 method updated to accept external parameters like "-line -header"

1.1.5 (07.11.2020)

sqlite3 method added

1.1.4 (06.11.2020)
  • added 'sftp' property explicitly
  • 'cat', 'check_exists', 'get_json' now support sudo usage
1.1.3 (08.08.2020)

get_pid method added

1.1.2 (25.04.2020)
  • send_cmd deprecated
  • fix password prompt in stderr
1.1.1 (29.03.2020)

get_md5 method added

1.1.0 (19.03.2020)

ResponseParser extended with json()

1.0.9 (28.02.2020)

Log filehandler writes in utf8 from now

1.0.8 (06.02.2020)

get_file_permission extended:

  • added faq
  • added "human=False" param returns access rights in human readable form otherwise in in octal
  • added alias "stat"
1.0.7 (30.01.2020)
  • ResponseParser methods notation changed.
    • stdout -> str
    • stderr -> str
    • exited -> int
    • ok -> bool
    • command -> str
1.0.6 (29.01.2020)
  • kill_user_session method added
1.0.5 (26.01.2020)
  • logging refactored to avoid multiple log entries

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

plinux-2.0.2.tar.gz (30.9 kB view details)

Uploaded Source

Built Distribution

plinux-2.0.2-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for plinux-2.0.2.tar.gz
Algorithm Hash digest
SHA256 8c1ab061a8bb5c76c7871d8db44daf1cd2fe01a5396e9e5eb5c2a060c4c9866d
MD5 0f8e271b64395f6a4d73c7caba6787d7
BLAKE2b-256 38a37635a0c7505f4fc1ef22d23336ef120ccccc31a881aff3671efc4ec50890

See more details on using hashes here.

File details

Details for the file plinux-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: plinux-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for plinux-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c35b238cbd048e18216a730926d8faa7b069bb086b5e288d10ea57f8bfc495a1
MD5 f72c13d22d1aef1c392277a99edd6607
BLAKE2b-256 4a6e35b936a3b198124b15c9ded9e5622163cad30c660432f112b5f51ad941d3

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