Skip to main content

SSH library for xbot.framework

Project description

Introduction

xbot.plugins.ssh provides ssh and sftp support for xbot.framework

Installation

Install via pip:

pip install xbot.plugins.ssh

Getting Started

import os
import shutil

from xbot.plugins.ssh.ssh import SSHConnection
from xbot.plugins.ssh.sftp import SFTPConnection

# Modify the following information to your own before trying to run.
host = '192.168.8.8'
user = 'xbot'
pwd = 'xbot'

# Create ssh connection.
sshconn = SSHConnection()
sshconn.connect(host, user, pwd)

# Expect the return code of command is 0.
sshconn.exec('ls -l', expect=0)

# Expect the return code of command is 2.
sshconn.exec('ls /errpath', expect=2)

# Expect the output of command contains `username`.
sshconn.exec('whoami', expect=user)

# Don't check the result of command.
sshconn.exec('ls -l', expect=None)
sshconn.exec('ls /errpath', expect=None)

# Execute command with sudo.
sshconn.sudo('whoami', expect='root')

# Interactive command.
sshconn.exec("read -p 'input: '", prompts={'input:': 'hello'})

# Attributes and methods of SSHCommandResult.
cmd = 'echo -e "jack 20\ntom 30"'
result = sshconn.exec(cmd)
assert str(result) == 'jack 20\ntom 30'
assert result.rc == 0
assert result.cmd == cmd
assert result.getfield('tom', 2) == '30'
assert result.getcol(2) == ['20', '30']

# Create sftp connection.
sftpconn = SFTPConnection()
sftpconn.connect(host, user, pwd)

# Preparing for sftp testing.
lhome = os.environ.get('HOME') or os.environ['HOMEPATH']
lputdir = os.path.join(lhome, 'lputdir')
lgetdir = os.path.join(lhome, 'lgetdir')
rputdir = '/tmp/rputgetdir'
rgetdir = rputdir
if os.path.exists(lputdir):
    shutil.rmtree(lputdir)
if sftpconn.exists(rputdir):
    sshconn.exec(f'rm -rf {rputdir}')
sftpconn.makedirs(rputdir)
os.makedirs(os.path.join(lputdir, 'dir', 'subdir'))
os.system('whoami > %s' % os.path.join(lputdir, 'file'))
os.system('whoami > %s' % os.path.join(lputdir, 'dir', 'subfile'))

# Put directory.
l = os.path.join(lputdir, 'dir')
sftpconn.putdir(l, rputdir)
p1 = sftpconn.join(rputdir, 'dir', 'subdir')
assert sftpconn.exists(p1) == True
p2 = sftpconn.join(rputdir, 'dir', 'subfile')
assert sftpconn.exists(p2) == True

# Put file.
l = os.path.join(lputdir, 'file')
sftpconn.putfile(l, rputdir)
p1 = sftpconn.join(rputdir, 'file')
assert sftpconn.exists(p1) == True
sftpconn.putfile(l, rputdir, 'newfile')  # rename
p2 = sftpconn.join(rputdir, 'newfile')
assert sftpconn.exists(p2) == True

# Get directory.
r = sftpconn.join(rgetdir, 'dir')
sftpconn.getdir(r, lgetdir)
p1 = os.path.join(lgetdir, 'dir', 'subdir')
assert os.path.exists(p1) == True
p2 = os.path.join(lgetdir, 'dir', 'subfile')
assert os.path.exists(p2) == True

# Get file.
r = sftpconn.join(rgetdir, 'file')
sftpconn.getfile(r, lgetdir)
p1 = os.path.join(lgetdir, 'file')
assert os.path.exists(p1) == True
sftpconn.getfile(r, lgetdir, 'newfile')  # rename
p2 = os.path.join(lgetdir, 'newfile')
assert os.path.exists(p2) == True

# Open file.
p = sftpconn.join(rputdir, 'file')
with sftpconn.open(p, 'w') as fp:
    fp.write('xbot')
with sftpconn.open(p, 'r') as fp:
    assert fp.read().decode('utf-8') == 'xbot'

Demo Project

Here is a demo project showing how to use xbot.plugins.ssh in a test project.


简介

xbot.plugins.ssh 是为 xbot.framework 提供 ssh 和 sftp 支持的插件。

安装

使用 pip 安装:

pip install xbot.plugins.ssh

入门

import os
import shutil

from xbot.plugins.ssh.ssh import SSHConnection
from xbot.plugins.ssh.sftp import SFTPConnection

# Modify the following information to your own before trying to run.
host = '192.168.8.8'
user = 'xbot'
pwd = 'xbot'

# Create ssh connection.
sshconn = SSHConnection()
sshconn.connect(host, user, pwd)

# Expect the return code of command is 0.
sshconn.exec('ls -l', expect=0)

# Expect the return code of command is 2.
sshconn.exec('ls /errpath', expect=2)

# Expect the output of command contains `username`.
sshconn.exec('whoami', expect=user)

# Don't check the result of command.
sshconn.exec('ls -l', expect=None)
sshconn.exec('ls /errpath', expect=None)

# Execute command with sudo.
sshconn.sudo('whoami', expect='root')

# Interactive command.
sshconn.exec("read -p 'input: '", prompts={'input:': 'hello'})

# Attributes and methods of SSHCommandResult.
cmd = 'echo -e "jack 20\ntom 30"'
result = sshconn.exec(cmd)
assert str(result) == 'jack 20\ntom 30'
assert result.rc == 0
assert result.cmd == cmd
assert result.getfield('tom', 2) == '30'
assert result.getcol(2) == ['20', '30']

# Create sftp connection.
sftpconn = SFTPConnection()
sftpconn.connect(host, user, pwd)

# Preparing for sftp testing.
lhome = os.environ.get('HOME') or os.environ['HOMEPATH']
lputdir = os.path.join(lhome, 'lputdir')
lgetdir = os.path.join(lhome, 'lgetdir')
rputdir = '/tmp/rputgetdir'
rgetdir = rputdir
if os.path.exists(lputdir):
    shutil.rmtree(lputdir)
if sftpconn.exists(rputdir):
    sshconn.exec(f'rm -rf {rputdir}')
sftpconn.makedirs(rputdir)
os.makedirs(os.path.join(lputdir, 'dir', 'subdir'))
os.system('whoami > %s' % os.path.join(lputdir, 'file'))
os.system('whoami > %s' % os.path.join(lputdir, 'dir', 'subfile'))

# Put directory.
l = os.path.join(lputdir, 'dir')
sftpconn.putdir(l, rputdir)
p1 = sftpconn.join(rputdir, 'dir', 'subdir')
assert sftpconn.exists(p1) == True
p2 = sftpconn.join(rputdir, 'dir', 'subfile')
assert sftpconn.exists(p2) == True

# Put file.
l = os.path.join(lputdir, 'file')
sftpconn.putfile(l, rputdir)
p1 = sftpconn.join(rputdir, 'file')
assert sftpconn.exists(p1) == True
sftpconn.putfile(l, rputdir, 'newfile')  # rename
p2 = sftpconn.join(rputdir, 'newfile')
assert sftpconn.exists(p2) == True

# Get directory.
r = sftpconn.join(rgetdir, 'dir')
sftpconn.getdir(r, lgetdir)
p1 = os.path.join(lgetdir, 'dir', 'subdir')
assert os.path.exists(p1) == True
p2 = os.path.join(lgetdir, 'dir', 'subfile')
assert os.path.exists(p2) == True

# Get file.
r = sftpconn.join(rgetdir, 'file')
sftpconn.getfile(r, lgetdir)
p1 = os.path.join(lgetdir, 'file')
assert os.path.exists(p1) == True
sftpconn.getfile(r, lgetdir, 'newfile')  # rename
p2 = os.path.join(lgetdir, 'newfile')
assert os.path.exists(p2) == True

# Open file.
p = sftpconn.join(rputdir, 'file')
with sftpconn.open(p, 'w') as fp:
    fp.write('xbot')
with sftpconn.open(p, 'r') as fp:
    assert fp.read().decode('utf-8') == 'xbot'

示例项目

展示如何在一个测试项目中使用本插件的示例:demo

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

xbot_plugins_ssh-0.2.3.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

xbot_plugins_ssh-0.2.3-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file xbot_plugins_ssh-0.2.3.tar.gz.

File metadata

  • Download URL: xbot_plugins_ssh-0.2.3.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for xbot_plugins_ssh-0.2.3.tar.gz
Algorithm Hash digest
SHA256 c680a12598af48fc521f75f4b50b05816bb645c4d6ac47d9a6b37969299e7b7a
MD5 7e4d6a1be9402d78af3a117d54320849
BLAKE2b-256 9153e5a06a83ec70c396f93b2f6aaac82ad5c41c629ce09375745a7a7fb2a0f9

See more details on using hashes here.

File details

Details for the file xbot_plugins_ssh-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for xbot_plugins_ssh-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1a8e198d767a422480624c199a89921fa5fc73e58e26459caafcbdb53a8a23db
MD5 0d7e993694cd72dbd0f5c6347f6644f4
BLAKE2b-256 ea5600d65112efd41aa83f9d08b5208fd488fff62446939295dbc991e2b36de4

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