Skip to main content

No project description provided

Project description

LOGO Version Python Versions License Downloads

gqylpy-ssh

本源代码使用了 paramiko 库,这个库是基于 LGPL 协议发布的。

在远程服务器执行命令并得到命令结果,它是对 paramiko 库的二次封装。通过 Command 对象,你可以得到这条命令的执行结果,状态等信息。

pip3 install gqylpy_ssh

首先初始化 GqylpySSH 实例:

import gqylpy_ssh as gssh

gssh.__init__(
    hostname='192.168.1.7',
    username='gqylpy',
    password=...
)

通过 cmd 函数在远程服务器执行命令,它返回一个 Command 对象:

c = gssh.cmd('hostname')

若命令执行失败,调用此方法将抛出异常:

c.raise_if_error()

获得命令状态或输出:

status: bool = c.status
output: str = c.output
status, output = c.status_output

获得命令输出,若命令执行错误,将抛出异常

output: str = c.output_else_raise

获得命令输出,若命令执行错误,将返回 "define value":

output: str = c.output_else_define('define value')

检查命令输出是否包含某个字符串,得到一个布尔值:

x: bool = c.contain_string('string')

获得命令输出,如果输出包含某个字符串,否则抛出异常:

output: str = c.output_if_contain_string_else_raise('string')

这个方法可将带有标题的输出转为字典:

it = c.output2dict('kubectl get nodes')

如果你需要创建多个 GqylpySSH 实例:

from gqylpy_ssh import GqylpySSH, Command

ssh = GqylpySSH('192.168.1.7', **params)
c: Command = ssh.cmd('hostname')
output: str = c.output_else_raise
import gqylpy_ssh as gssh

gssh.__init__('192.168.1.7', **params, gname='1.7')
gssh.__init__('192.168.1.9', **params, gname='1.9')

output: str = gssh.cmd('hostname', gname='1.7').output_else_raise
output: str = gssh.cmd('hostname', gname='1.9').output_else_raise

gssh.cmd('hostname').output == gssh.cmd('hostname', gname='1.7').output

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

gqylpy_ssh-1.0.1-py3-none-any.whl (18.7 kB view hashes)

Uploaded Python 3

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