【问题标题】:Python (pexpect and pxssh) SSH to Cisco ASA firewall with MOTD BannerPython(pexpect 和 pxssh)SSH 到带有 MOTD 横幅的 Cisco ASA 防火墙
【发布时间】:2016-04-07 06:09:13
【问题描述】:

我正在用 Python 编写脚本,我正在使用 pxssh 和 pexpect 来完成工作,问题是我无法成功发送任何命令,我相信这是由于 MOTD 横幅造成的。以下是我到目前为止的代码,下面是横幅的样子:

import pexpect
import getpass
import pxssh
import sys

try:

    s = pxssh.pxssh()

    #this is for input file/lists - host, username, and password
    hostname = ('fw1.aff.tempe')
    username = ('tmarciniak')
    password = ('<password>')
    s.login(hostname, username, password, auto_prompt_reset=False)

    s.logfile = sys.stdout

    #s.expect('***.*') #matching the first characters of the MOTD banner for sending command
    s.sendline('enable') # run a command      
    s.prompt() # match the prompt                                           
    print(s.before)

    #s.prompt() # match the prompt
    # s.sendline('enable') # run a command
    #s.prompt() # match the prompt
    print(s.before) # print everything before the prompt
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")
    print(e)

SSH 连接成功后的 MOTD 横幅和输出:

***********************************************
*                                             *
* This Device is owned by Telesphere Networks *
*                                             *
* Unauthorized Access is Strictly Prohibited  *
*                                             *
*       Telesphere NOC: (800) 680-2203        *
*                                             *
***********************************************
************************************************************************
*
* Name: Amerifirst Financial - Tempe (36714)
*
* Hostname: fw1.aff.tempe
*
* Location: 2151 E Broadway Rd
*           Tempe, AZ 85282
*
* Notes:
*
************************************************************************
Type help or '?' for a list of available commands.
fw1-aff-tempe>

【问题讨论】:

    标签: python ssh cisco pexpect


    【解决方案1】:

    原来 pxssh 让你越过横幅,你只需要担心提示。对于那些好奇的人,我做了以下事情:

    def ssh(hostname, username, password, enable_password):
    ssh_session = pxssh.pxssh(maxread=32768, searchwindowsize=1024)
    
    original_prompt = '%s>' % (hostname.replace('.', '-'),) #matching the first line with the prompt as exact as possible
    enable_prompt = '%s#' % (hostname.replace('.', '-'),)
    ssh_session.login(hostname, username, password, original_prompt=original_prompt, auto_prompt_reset=False)
    
    ssh_session.sendline('enable') # run a command
    ssh_session.expect_exact('Password:') #match the response as exact as possible
    ssh_session.sendline(enable_password)
    
    #print('%s#' % (hostname.replace('.','-'),))    #not sure what this part does
    ssh_session.expect_exact(enable_prompt)
    
    ssh_session.sendline('terminal pager 0')
    ssh_session.expect_exact(enable_prompt)
    

    我正在登录不接受“.”的 Cisco ASA 防火墙。字符作为主机名中的分隔符。我定义了一个将替换“。”的变量。使用“-”,然后将其用作我的“expact_exact”,用于发送行之后的期望。

    在相关说明中(针对 Cisco 设备),“终端寻呼机 0”用于将特定会话的寻呼机缓冲区修改为 0;这会导致一次显示所有输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 2013-12-12
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多