- Mastering Python Networking
- Eric Chou
- 188字
- 2021-07-02 21:42:36
Pexpect and SSH
If you try to use the previous telnet example and just plug into a ssh session, you might find yourself pretty frustrated. You always have to include the username in the session, ssh new key question, and more such. There are many ways to make ssh sessions work, but luckily, Pexpect has a subclass called pxssh, which specializes setting up SSH connections. The class adds methods for login, logout, and various tricky things to handle many situation in the ssh login process. The procedures are mostly the same with the exception of login() and logout():
>>> from pexpect import pxssh
>>> child = pxssh.pxssh()
>>> child.login('172.16.1.20', 'cisco', 'cisco', auto_prompt_reset=False)
True
>>> child.sendline('show version | i V')
19
>>> child.expect('iosv-1#')
0
>>> child.before
b'show version | i VrnCisco IOS Software, IOSv Software (VIOS-ADVENTERPRISEK9-M), Version 15.6(2)T, RELEASE SOFTWARE (fc2)rnProcessor board ID 9MM4BI7B0DSWK40KV1IIRrn'
>>> child.logout()
>>>
Notice the 'auto_prompt_reset=False' argument in the login() method. By default, pxssh uses the shell prompt to synchronize the output. But since it uses the PS1 option for most of bash or Csh, they will error out on Cisco or other network devices.
- Mastering Visual Studio 2017
- Visual C++數字圖像模式識別技術詳解
- 面向STEM的Scratch創新課程
- C語言從入門到精通(第4版)
- Mastering ServiceNow(Second Edition)
- Java程序設計入門
- Getting Started with Hazelcast(Second Edition)
- 深入淺出React和Redux
- 從0到1:HTML5 Canvas動畫開發
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- Python網絡爬蟲實例教程(視頻講解版)
- 體驗之道:從需求到實踐的用戶體驗實戰
- Ubuntu Server Cookbook
- Swift編程實戰:iOS應用開發實例及完整解決方案
- Web應用程序設計:ASP