在本地或通过不同连接类型(telnet、ssh、serial、adb)与shell交互
citizenshell的Python项目详细描述
#公民壳[![构建状态](https://travis-ci.org/meuter/citizenshell.svg?branch=master)(https://travis ci.org/meuter/citizenshell)
citizenshell是一个python库,允许使用简单一致的api在多个协议(telnet、ssh、serial或adb)上本地或远程执行shell命令。这个库与python 2(2.7)和3(>;=3.4)以及[pypy]都兼容(https://pypy.org/)。目前,它主要关注posix平台,如linux和macos,但将来可能会扩展到基于windows的平台。它是在
[mit]许可证(https://opensource.org/licenses/mit)下发布的。
为此,您有几个选项:
1。使用内置的"localshell"进行快速访问:
``python
from citizenshell import sh
```
2。您可以实例化自己的"localshell":
`` python
来自citizenshell import localshell
```
>3。您可以通过telnet为shell实例化"telnetshell":
``python
来自citizenshell import telnetshell
password="secretpassword")
```
4。您可以通过ssh为shell实例化"secureshell":
`` python
来自citizenshell import secureshell
shell=secureshell(hostname="acme.org",username="john",
password="secretpassword")
```
5。您可以通过adb实例化shell的"adbshell":
``python
来自citizenshell import adbshell
password="secretpassword")
```
6。您可以通过串行线实例化shell的"serialshell":
`` python
从serial import eightbits,奇偶校验
从citizenshell import serialshell
shell=serialshell(port="/dev/ttyusb3",username="john",
password="secret密码",
波特率=115200,奇偶校验=奇偶校验,字节大小=八位)
````
7。还可以使用"shell"函数通过uri获取shell对象:
``python
from citizenshell import shell
telnetshell=shell("telnet://john:secretpassword@acme.org:1234")
secureshell=shell("ssh://john:secretpassword@acme"。org:1234"
adb shell=shell("adb://myandroiddevice:5555")
serialshell=shell("serial://jogn:secretpassword@/dev/ttyusb3?波特率=115200"
```
@/dev/ttyusb3,波特率=115200)
```
用法:
`` python
result=[in t(x)for x in shell("
for i in 1 2 3 4;do
echo$i;
done
"")]
assert result==[1,2,3,4]
```
对于shell中的line("对于1 2 3 4中的i;do echo-n'it i s';日期+%h:%m:%s;睡眠1;完成",wait=false)
打印">;>>",line+"!"
```
将生成如下内容:
```文本
>;>;它是14:24:52!
;>;gt;现在是14:24:53!
>>>现在是14:24:54!
>>gt;现在是14:24:55!
```
/>```
您可以将环境变量注入shell
``python
assert shell("echo$var",var="bar")="bar"
```
实现这一点的方法是:
``python
shell=localshell()
os.chdir(第一个自定义路径)
shell('first_command')
os.chdir(第二个自定义路径)
shell('second_command')
```
但它是丑陋的!两个抽象级别是混合的。
这更好:
`` python
shell=localshell()
shell('first_command',cwd=first_custom_path)
shell('second_command',cwd=second_custom_path)
```
如果退出代码非零,shell可以引发异常:
`` python
assert shell("exit 13").exit_code()==13不会引发任何异常/>如果在标准错误上打印了某些内容,shell也会引发异常:
``python
shell("echo danger>&;2").stderr()=["danger"]\t false,"将无法到达"
除了shellerror a s e:
assert true,"将无法到达"
````
mote_file.txt"
assert open("local_file.txt","r").read()="test"
````
>或将文件推送到远程主机(同样,对于"local shell",它只是做一个复制):
``python
open("local_file.txt","w").write("test")
shell.push("local_file.txt","remote_file.txt")
assertstr(shell("cat remote廑file.txt"))="test"
````
\logs
每个shell对象都有一组记录器:stdin、stderr和stdout,以及带外日志消息。
默认情况下,它们都设置为"logging.critical",不记录任何内容。但是,这个日志级别
可以使用shell构造器中的"log level=`关键字参数:
``python
from citizenshell import localshell
from logging import info
```
l()`方法:
``python
from citizenshell import sh
from logging import info
sh.设置日志级别(info)
````
配置为"logging.info":
-所有命令都登录在stdout上,前缀为"$",并用青色显示为"termcolor"
-所有字符都会生成stdout上的uced记录到stdout
-stderr上生成的所有字符都记录到stderr,并用红色"termcolor"标记
-所有带外消息都记录到stdout,前缀为">;",并用黄色"termcolor"标记
,例如:citiz的
``python
enshell import localshell
from logging import info
shell=localshell(logu level=info)
shell("echo hello>;&;2 echo error&;echo output&;exit 13")
shell("echo hello>;/tmp/from.txt")
shell.push("/tmp/from.txt"、"/tmp/to.txt")
`````
以下日志(省略颜色):
````
$>;&;2回显错误&;回显output&;exit 13
输出
错误
$echo hello>;/tmp/from.txt
>;'/tmp/from.txt'->;'/tmp/to.txt'
$command-v chmod
/bin/chmod
$chmod 664'/tmp/to.txt'
```
更多日志消息,可以使用"logging.debug"。
BR/>
citizenshell是一个python库,允许使用简单一致的api在多个协议(telnet、ssh、serial或adb)上本地或远程执行shell命令。这个库与python 2(2.7)和3(>;=3.4)以及[pypy]都兼容(https://pypy.org/)。目前,它主要关注posix平台,如linux和macos,但将来可能会扩展到基于windows的平台。它是在
[mit]许可证(https://opensource.org/licenses/mit)下发布的。
为此,您有几个选项:
1。使用内置的"localshell"进行快速访问:
``python
from citizenshell import sh
```
2。您可以实例化自己的"localshell":
`` python
来自citizenshell import localshell
>3。您可以通过telnet为shell实例化"telnetshell":
``python
来自citizenshell import telnetshell
```
4。您可以通过ssh为shell实例化"secureshell":
`` python
来自citizenshell import secureshell
shell=secureshell(hostname="acme.org",username="john",
password="secretpassword")
```
5。您可以通过adb实例化shell的"adbshell":
``python
来自citizenshell import adbshell
```
6。您可以通过串行线实例化shell的"serialshell":
`` python
从serial import eightbits,奇偶校验
从citizenshell import serialshell
shell=serialshell(port="/dev/ttyusb3",username="john",
password="secret密码",
波特率=115200,奇偶校验=奇偶校验,字节大小=八位)
````
7。还可以使用"shell"函数通过uri获取shell对象:
``python
from citizenshell import shell
secureshell=shell("ssh://john:secretpassword@acme"。org:1234"
adb shell=shell("adb://myandroiddevice:5555")
serialshell=shell("serial://jogn:secretpassword@/dev/ttyusb3?波特率=115200"
```
@/dev/ttyusb3,波特率=115200)
```
用法:
`` python
result=[in t(x)for x in shell("
for i in 1 2 3 4;do
echo$i;
done
"")]
assert result==[1,2,3,4]
```
对于shell中的line("对于1 2 3 4中的i;do echo-n'it i s';日期+%h:%m:%s;睡眠1;完成",wait=false)
打印">;>>",line+"!"
```
将生成如下内容:
```文本
>;>;它是14:24:52!
;>;gt;现在是14:24:53!
>>>现在是14:24:54!
>>gt;现在是14:24:55!
```
/>```
您可以将环境变量注入shell
``python
assert shell("echo$var",var="bar")="bar"
```
实现这一点的方法是:
``python
shell=localshell()
os.chdir(第一个自定义路径)
shell('first_command')
os.chdir(第二个自定义路径)
shell('second_command')
```
但它是丑陋的!两个抽象级别是混合的。
这更好:
`` python
shell=localshell()
shell('first_command',cwd=first_custom_path)
shell('second_command',cwd=second_custom_path)
```
如果退出代码非零,shell可以引发异常:
`` python
assert shell("exit 13").exit_code()==13不会引发任何异常/>如果在标准错误上打印了某些内容,shell也会引发异常:
``python
shell("echo danger>&;2").stderr()=["danger"]\t false,"将无法到达"
除了shellerror a s e:
assert true,"将无法到达"
````
mote_file.txt"
assert open("local_file.txt","r").read()="test"
````
>或将文件推送到远程主机(同样,对于"local shell",它只是做一个复制):
``python
open("local_file.txt","w").write("test")
shell.push("local_file.txt","remote_file.txt")
assertstr(shell("cat remote廑file.txt"))="test"
````
\logs
每个shell对象都有一组记录器:stdin、stderr和stdout,以及带外日志消息。
默认情况下,它们都设置为"logging.critical",不记录任何内容。但是,这个日志级别
可以使用shell构造器中的"log level=`关键字参数:
``python
from citizenshell import localshell
from logging import info
l()`方法:
``python
from citizenshell import sh
from logging import info
sh.设置日志级别(info)
````
配置为"logging.info":
-所有命令都登录在stdout上,前缀为"$",并用青色显示为"termcolor"
-所有字符都会生成stdout上的uced记录到stdout
-stderr上生成的所有字符都记录到stderr,并用红色"termcolor"标记
-所有带外消息都记录到stdout,前缀为">;",并用黄色"termcolor"标记
,例如:citiz的
``python
enshell import localshell
from logging import info
shell=localshell(logu level=info)
shell("echo hello>;&;2 echo error&;echo output&;exit 13")
shell("echo hello>;/tmp/from.txt")
shell.push("/tmp/from.txt"、"/tmp/to.txt")
`````
以下日志(省略颜色):
````
$>;&;2回显错误&;回显output&;exit 13
输出
错误
$echo hello>;/tmp/from.txt
>;'/tmp/from.txt'->;'/tmp/to.txt'
$command-v chmod
/bin/chmod
$chmod 664'/tmp/to.txt'
```
更多日志消息,可以使用"logging.debug"。
BR/>