Skip to main content

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

Metadata

Release files for xbot.plugins.ssh 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xbot.plugins.ssh 1.0.0
File Size Uploaded
xbot_plugins_ssh-1.0.0.tar.gz 12.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xbot.plugins.ssh 1.0.0
File Interpreter ABI Platform
xbot_plugins_ssh-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.8 kB

Release files / xbot_plugins_ssh-1.0.0.tar.gz

Download URL xbot_plugins_ssh-1.0.0.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1ad1378436b35a550f8554f1d7802de650be41f718be135e2da859df38eb4111
BLAKE2b-256 checksum
How to use checksums
7f0f650ad035d19aee9548fea057a3e202409b79201206bb6b347c776a5c6766
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.20

Release files / xbot_plugins_ssh-1.0.0-py3-none-any.whl

Download URL xbot_plugins_ssh-1.0.0-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06214338950a24ef5e99e8dc5c3b02e826473332ccced7d7d1a75027a9bdcbac
BLAKE2b-256 checksum
How to use checksums
0744493dc8033d2702fdc6a2f9a1beb1fc61e20f56ec7e95d4c50dd1aeab9e27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.20

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page