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 Apache Flink外部Jar   创建和强制转换对象数组时发生java错误   Java,添加数组   具有相同包结构和类的java JAR   java Jenkins未能构建Maven项目   java为什么一个forloop比另一个更快,尽管它们做的“一样”?   servlets在将“/”站点迁移到Java EE包时处理contextpath引用   无法解析java MavReplugin:2.21或其某个依赖项   泛型如何编写比较器来泛化Java中的两种类型的对象?   java Android Emulator未在netbeans上加载   多线程Java使用线程对数组中的数字求和:在同步块中使用新变量作为锁:差异   java如何在JSP/servlet中设置<input>标记的值?