paramiko ssh库的expect-like扩展

paramiko-expect的Python项目详细描述


https://img.shields.io/pypi/l/paramiko-expect.svghttps://coveralls.io/repos/github/fgimian/paramiko-expect/badge.svg?branch=masterhttps://img.shields.io/travis/fgimian/paramiko-expect.svghttps://img.shields.io/pypi/v/paramiko-expect.svghttps://img.shields.io/pypi/pyversions/paramiko-expect.svgParamiko Expect Logo

艺术作品由Open Clip Art Library

简介

paramiko expect为paramiko ssh库提供类似expect的扩展 它允许脚本通过真正的ssh与主机完全交互 连接。

类是用ssh客户机对象构造的(这可能是 扩展以支持未来的运输以获得更大的灵活性。

快速启动

要安装paramiko expect,只需在提示下运行以下命令:

# from pypi
pip insall paramiko-expect

# from source
pip install git+https://github.com/fgimian/paramiko-expect.git

所以让我们看看它是如何工作的(请看 paramiko_expect-demo.py 完整代码:

# Connect to the hostclient.connect(hostname=hostname,username=username,password=password)# Create a client interaction class which will interact with the hostinteract=SSHClientInteraction(client,timeout=10,display=True)interact.expect(prompt)# Run the first command and capture the cleaned output, if you want the output# without cleaning, simply grab current_output instead.interact.send('uname -a')interact.expect(prompt)cmd_output_uname=interact.current_output_clean# Now let's do the same for the ls command but also set a timeout for this# specific expect (overriding the default timeout)interact.send('ls -l /')interact.expect(prompt,timeout=5)cmd_output_ls=interact.current_output_clean# To expect multiple expressions, just use a list.  You can also selectively# take action based on what was matched.# Method 1: You may use the last_match property to find out what was matchedinteract.send('~/paramiko_expect-demo-helper.py')interact.expect([prompt,'Please enter your name: '])ifinteract.last_match=='Please enter your name: ':interact.send('Fotis Gimian')interact.expect(prompt)# Method 2: You may use the matched index to determine the last match (like pexpect)interact.send('~/paramiko_expect-demo-helper.py')found_index=interact.expect([prompt,'Please enter your name: '])iffound_index==1:interact.send('Fotis Gimian')interact.expect(prompt)# Send the exit command and expect EOF (a closed session)interact.send('exit')interact.expect()# Print the output of each commandprint'-'*79print'Cleaned Command Output'print'-'*79print'uname -a output:'printcmd_output_unameprint'ls -l / output:'printcmd_output_ls

important:运行此脚本之前,请确保 paramiko_expect-demo-helper.py~中。

脚本底部的print语句提供了以下内容 输出:

-------------------------------------------------------------------------------
Cleaned Command Output
-------------------------------------------------------------------------------
uname -a output:
Linux fotsies-ubuntu-testlab 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

ls -l / output:
total 77
drwxr-xr-x  2 root root  4096 May  122:21 bin
drwxr-xr-x  4 root root  1024 May  122:22 boot
drwxr-xr-x 15 root root  4300 Jun 1215:00 dev
drwxr-xr-x 90 root root  4096 Jun 1216:45 etc
drwxr-xr-x  4 root root  4096 May  123:37 home
lrwxrwxrwx  1 root root    33 May  122:18 initrd.img -> /boot/initrd.img-3.2.0-23-generic
drwxr-xr-x 18 root root  4096 May  122:21 lib
drwxr-xr-x  2 root root  4096 May  122:17 lib64
drwx------  2 root root 16384 May  122:17 lost+found
drwxr-xr-x  4 root root  4096 May  122:18 media
drwxr-xr-x  2 root root  4096 Apr 1919:32 mnt
drwxr-xr-x  2 root root  4096 May  122:17 opt
dr-xr-xr-x 84 root root     0 Jun 1215:00 proc
drwx------  3 root root  4096 May 3023:32 root
drwxr-xr-x 15 root root   560 Jun 1217:02 run
drwxr-xr-x  2 root root  4096 Jun  420:59 sbin
drwxr-xr-x  2 root root  4096 Mar  604:54 selinux
drwxr-xr-x  2 root root  4096 May  122:17 srv
drwxr-xr-x 13 root root     0 Jun 1215:00 sys
drwxrwxrwt  2 root root  4096 Jun 1216:17 tmp
drwxr-xr-x 10 root root  4096 May  122:17 usr
drwxr-xr-x 12 root root  4096 Jun 1213:16 var
lrwxrwxrwx  1 root root    29 May  122:18 vmlinuz -> boot/vmlinuz-3.2.0-23-generic

为了与类似tail的脚本进行交互,我们可以使用tail函数(请参见 paramiko_expect-tail-demo.py 完整代码:

# Connect to the hostclient.connect(hostname=hostname,username=username,password=password)# Create a client interaction class which will interact with the hostinteract=SSHClientInteraction(client,timeout=10,display=False)interact.expect(prompt)# Send the tail commandinteract.send('tail -f /var/log/auth.log')# Now let the class tail the file for usinteract.tail(line_prefix=hostname+': ')

当你 查看Multi-SSH 图书馆。有没有想过在多个服务器上跟踪日志?嗯 别再做梦了,我的朋友,它在这里!

测试

尚未完全覆盖,并且假设您已经安装了Docker:

pip install -r requirements-test.txt
docker run -d -p 2222:22 -v `pwd`/examples:/examples -v `pwd`/test/id_rsa.pub:/root/.ssh/authorized_keys  macropin/sshd
pytest -s --cov paramiko_expect --cov-report term-missing

贡献

  • 以色列fruchter(@fruch)-测试/ci/上传到pypi
  • Kiseok Kim(@kiseok7)-流浪的形象

许可证

paramiko expect是在mit许可下发布的。请看 LICENSE 文件以获取更多详细信息。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
安卓在java中加入字符串组以创建复合字符串   java系统甚至不点击“下一步”或“上一步”按钮就将我返回到上一页,而不是进入下一页   java如何在arrayList中获取特定列的不同值   CXF GZIP REST JAVA   Java:使用大量半恒定标志检查优化循环中的循环?   java如何在两个应用程序之间进行会话管理?   java SVG文件使用蜡染(但没有轴线)转换为PNG   使用协议缓冲区和内部数据模型的java   java如何在logtag 安卓中打印和查看字符串的值   javascript如何在NodeJs中应用Java/Spring的分层架构?   java Spring URL在JSP中编码不正确   模式对话框后面的java工具提示   java WSRPC生成日历对象而不是日期   在对象外部无法识别类变量   java将图像从文件读/写到BuffereImage的最快方法?   JavaSpring数据存储库对具有不同Id的子类使用抽象超类   安全在Java 5.0上运行web应用程序服务器有危险吗?