Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pxssh timeout issue
#1
At some situation I'm facing this error, I have added the error details below. Can somebody help me in fixing this issue?

...
        s = pxssh.pxssh(timeout=5, maxread=2000000)
        switch_ip = raw_input('Enter the switch ip: ')
        username = raw_input('username: ')
        password = getpass.getpass('password: ')
        s.PROMPT = "\[.][\#]"
        s.force_password = True
...
Error:
Traceback (most recent call last): File "clean_intf_acl_pxssh.py", line 100, in <module> s.logout() File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 350, in logout index = self.expect([EOF, "(?i)there are stopped jobs"]) File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 315, in expect timeout, searchwindowsize, async) File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 339, in expect_list return exp.expect_loop(timeout) File "/usr/lib/python2.7/dist-packages/pexpect/expect.py", line 104, in expect_loop return self.timeout(e) File "/usr/lib/python2.7/dist-packages/pexpect/expect.py", line 68, in timeout raise TIMEOUT(msg) pexpect.exceptions.TIMEOUT: Timeout exceeded. <pexpect.pxssh.pxssh object at 0x7fa7eb01f490> command: /usr/bin/ssh args: ['/usr/bin/ssh', '-q', '-oRSAAuthentication=no', '-o', 'PubkeyAuthentication=no', '-l', 'admin', '10.16.206.119'] searcher: None buffer (last 100 chars): 'ow\r\r\n\rN9K-119(config-if)# interface ethernet 1/16\r\r\n\rN9K-119(config-if)# no switchport mode trunk\r\r\n' before (last 100 chars): 'ow\r\r\n\rN9K-119(config-if)# interface ethernet 1/16\r\r\n\rN9K-119(config-if)# no switchport mode trunk\r\r\n' after: <class 'pexpect.exceptions.TIMEOUT'> match: None match_index: None exitstatus: None flag_eof: False pid: 23594 child_fd: 5 closed: False timeout: 5 delimiter: <class 'pexpect.exceptions.EOF'> logfile: None logfile_read: None logfile_send: None maxread: 2000000 ignorecase: False searchwindowsize: None delaybeforesend: 0.05 delayafterclose: 0.1 delayafterterminate: 0.1
I have tired changing maxread value and timeout value, but the error still persist, I'm not sure whats happening here.
Reply
#2
you need to capture the timeout:
        try:
            s = pxssh.pxssh(timeout=5, maxread=2000000)
            switch_ip = raw_input('Enter the switch ip: ')
            username = raw_input('username: ')
            password = getpass.getpass('password: ')
            s.PROMPT = "\[.][\#]"
            s.force_password = True
        except pxssh.ExceptionPxssh as e:
            print("pxssh failed on login.")
            print(e)
Not tested, but should be ok
Reply
#3
Hi Larz60+,

Already I have the try...except block

The error I added is a captured one
Reply
#4
Ok, so why are you holding out? in order to give advise, need the full picture
Show enough code to determine what is wrong
Reply
#5
I have added the entire code.
This code is for removing some configurations in Switch, Please go through this and the error which I attached already and Give some view on it.


from pexpect import pxssh
import pexpect
import getpass
import re
import time
import string
import sys
import pdb
def acl_parser(out):
    ip_acl_names = []
    ipv6_acl_names = []
    mac_acl_names = []
    parser_dict = {}
    lines = out.replace('\\n','\n')
    lines = lines.split("\n")
    for line in lines:
        line = line.replace('\\r', ' ')
        line = line.strip(" ")
        if re.search(r'(IP access list)\s+(.*)',line,re.IGNORECASE):
            value = re.search(r'(IP access list)\s+(.*)',line,re.IGNORECASE)
            ip_acl_names.append(value.group(2))
        if re.search(r'(IPv6 access list)\s+(.*)',line,re.IGNORECASE):
            value = re.search(r'(IPv6 access list)\s+(.*)',line,re.IGNORECASE)
            ipv6_acl_names.append(value.group(2))
        if re.search(r'(MAC access list)\s+(.*)',line,re.IGNORECASE):
            value = re.search(r'(MAC access list)\s+(.*)',line,re.IGNORECASE)
            mac_acl_names.append(value.group(2))
    parser_dict['IP'] = ip_acl_names
    parser_dict['IPv6'] = ipv6_acl_names
    parser_dict['MAC'] = mac_acl_names

    return parser_dict

def intf_parser(out):
    intf_values = []
    intf_keys = []
    parser_dict = {}
    lines = out.replace('\\r', '')
    lines = lines.replace('\r','')
    lines = lines.replace('\\n','\n')
    lines = lines.split("\n\n")
    for line in lines:
        line = line.strip(" ")
        if re.search(r'(interface)\s+(.*)',line,re.IGNORECASE):
            value = re.search(r'(interface)\s+(.*)',line,re.IGNORECASE)
            intf_key = value.group(2)
            if 'Ethernet' in intf_key:
               parser_dict[intf_key] = line.split('\n')
               del_str = 'interface '+intf_key
               parser_dict[intf_key].remove(del_str)
    return parser_dict


if __name__ == '__main__':
    try:
        s = pxssh.pxssh(timeout=5, maxread=2000000)
        switch_ip = raw_input('Enter the switch ip: ')
        username = raw_input('username: ')
        password = getpass.getpass('password: ')
        s.PROMPT = "\[.][\#]"
        s.force_password = True
        s.login(switch_ip, username, password,auto_prompt_reset=False)
        s.sendline('sh access-lists | no-more')
        s.prompt()
        all_acls = s.before
        acl_resp = acl_parser(all_acls)
        # Removing ACLS
        s.sendline('conf t')
        for item in acl_resp.keys():
            if item == 'IP':
                for ip_item in acl_resp[item]:
                    acl_name = 'no ip access-list '+ip_item
                    s.sendline(acl_name)
            elif item == 'IPv6':
                for ipv6_item in acl_resp[item]:
                    acl_name = 'no ipv6 access-list '+ipv6_item
                    s.sendline(acl_name)
            elif item == 'MAC':
                for mac_item in acl_resp[item]:
                    acl_name = 'no mac access-list '+mac_item
                    s.sendline(acl_name)
        s.sendline('end')
        s.sendline('sh run int | no-more')   # run a command
        s.prompt()             # match the prompt
        all_intf = s.before
        #print(all_intf)        # print everything before the prompt.
        intf_resp = intf_parser(all_intf)
        # pdb.set_trace()
        s.sendline('conf t')
        for interface in intf_resp.keys():
            if len(intf_resp[interface]) != 0:
                intf_str = 'interface ethernet '+interface[8:]
                s.sendline(intf_str)
                for content in intf_resp[interface]:
                    content = content.strip(" ")
                    content_str = 'no '+ content
                    s.sendline(content_str)
        s.sendline('end')
        #pdb.set_trace()
        s.logout()
        print "Successfully removed all port configuration and access-lists"
    except pxssh.ExceptionPxssh as e:
        print e
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FTp timeout except korenron 2 3,566 Feb-01-2022, 06:51 AM
Last Post: korenron
  timeout value in subprocess jonesin1974 2 5,015 Dec-01-2017, 02:18 PM
Last Post: snippsat
  AsyncSSH and timeout Standard_user 1 5,483 Nov-03-2016, 06:05 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020