ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.59k stars 1.31k forks source link

Timed-out reading channel #1392

Closed Noct3 closed 5 years ago

Noct3 commented 5 years ago

Hello,

I have a script that uses Netmiko to connect to multiple switches and create VLANs, then Tag some ports.

I was able run it successfully but it stopped at about 3/4th of the script. Here's the traceback.

Traceback (most recent call last):
  File "/home/administrator/.local/lib/python3.6/site-packages/paramiko/channel.py", line 699, in recv
    out = self.in_buffer.read(nbytes, self.timeout)
  File "/home/administrator/.local/lib/python3.6/site-packages/paramiko/buffered_pipe.py", line 164, in read
    raise PipeTimeout()
paramiko.buffered_pipe.PipeTimeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/base_connection.py", line 534, in _read_channel_expect
    new_data = self.remote_conn.recv(MAX_BUFFER)
  File "/home/administrator/.local/lib/python3.6/site-packages/paramiko/channel.py", line 701, in recv
    raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "create_vlans_STG.py", line 239, in <module>
    net_connect = ConnectHandler(device_type=device_type, host=ip_address, username=username, password=password)
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py", line 246, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/base_connection.py", line 317, in __init__
    self._open()
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/base_connection.py", line 323, in _open
    self._try_session_preparation()
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/base_connection.py", line 738, in _try_session_preparation
    self.session_preparation()
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/hp/hp_procurve.py", line 110, in session_preparation
    super(HPProcurveSSH, self).session_preparation()
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/hp/hp_procurve.py", line 31, in session_preparation
    self.disable_paging(command=command)
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/base_connection.py", line 1015, in disable_paging
    output = self.read_until_prompt()
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/base_connection.py", line 605, in read_until_prompt
    return self._read_channel_expect(*args, **kwargs)
  File "/home/administrator/.local/lib/python3.6/site-packages/netmiko/base_connection.py", line 543, in _read_channel_expect
    "Timed-out reading channel, data not available."
netmiko.ssh_exception.NetMikoTimeoutException: Timed-out reading channel, data not available.

Here's the entire script:


from netmiko import ConnectHandler 

stg_switches_1910 = {
    'visw0102' : {
        'device_type': 'hp_comware',
        'ip': '192.168.0.241',
        'username': 'admin',
        'password': 'password'
    },
    'visw0103' : {
        'device_type': 'hp_comware',
        'ip': '192.168.0.242',
        'username': 'admin',
        'password': 'password'
    },
    'visw0105' : {
        'device_type': 'hp_comware',
        'ip': '192.168.0.244',
        'username': 'admin',
        'password': 'password'
    }
}

stg_switches_1920 = {
    'visw0107' : {
        'device_type': 'hp_comware',
        'ip': '192.168.0.246',
        'username': 'admin',
        'password': 'password'
    }
}

stg_switches_aruba = {
    'vil3sw0101' : {
        'device_type': 'hp_procurve',
        'ip': '192.168.0.30',
        'username': 'manager',
        'password': 'password'
    }
    'vil3sw0102' : {
        'device_type': 'hp_procurve',
        'ip': '192.168.0.33',
        'username': 'manager',
        'password': 'password'
    }
    'visw0108' : {
        'device_type': 'hp_procurve',
        'ip': '192.168.0.251',
        'username': 'manager',
        'password': 'password'
    }
    'visw0111' : {
        'device_type': 'hp_procurve',
        'ip': '192.168.0.248',
        'username': 'manager',
        'password': 'password'
    }
}

vlans = {
    '7': 'SERVERS',
    '8': 'LEGACY_SERVERS',
    '16': 'NETWORK_MGMT',
    '21': 'VIDEOCONFERENCE',
    '22': 'VOIP',
    '33': 'TIME_CLOCKS',
    '34': 'PRINTERS',
    '36': 'PLC_TOOLS',
    '37': 'PLANT_COMPUTERS',
    '38': 'OFFICE_COMPUTER',
    '66': 'GUEST_WIRELESS',
    '67': 'IoT_WIRELESS',
    '68': 'CORPORATE_WIRELESS',
    '78': 'LEGACY_COMPUTERS',
    '85': 'WDS'
}

uplinks_visw0102 = ['GigabitEthernet1/0/1']
uplinks_visw0103 = ['GigabitEthernet1/0/1']
uplinks_visw0105 = ['GigabitEthernet1/0/2','GigabitEthernet1/0/15','Bridge-Aggregation 1']
uplinks_visw0107 = ['GigabitEthernet1/0/1']
uplinks_visw0108 = ['1']
uplinks_visw0111 = ['24']
uplinks_vil3sw0101 = ['A1','Trk2','1','2','4','11','21','33','34','35','44']
uplinks_vil3sw0102 = ['A1','1']

# LOOPING THROUGH HP 1910 SWITCHES

for key, values in stg_switches_1910.items():
    device_type = values.get('device_type', {})
    ip_address = values.get('ip', {})
    username = values.get('username', {})
    password = values.get('password', {})

    net_connect = ConnectHandler(device_type=device_type, host=ip_address, username=username, password=password)

    output = net_connect.send_command_timing(
        'y', 
        strip_prompt=False, 
        strip_command=False
        )
    output = net_connect.send_command_timing(
        '_cmdline-mode on', 
        strip_prompt=False, 
        strip_command=False
        )
    print (output)
    if 'Continue' in output:
        output += net_connect.send_command_timing(
            'y', 
            strip_prompt=False, 
            strip_command=False
        )
    print (output)
    if 'ssword' in output:
        output = net_connect.send_command_timing(
            '512900',
            strip_prompt=False, 
            strip_command=False
            )
    print (output)
    output = net_connect.send_command_timing(
        'system-view',
        strip_prompt=False,
        strip_command=False
        )
    print (output)

    for tag, vlan_name in vlans.items():
        output = net_connect.send_command_timing(
            'vlan' + ' ' + tag,
            strip_prompt=False,
            strip_command=False
            )
        print (output)
        output = net_connect.send_command_timing(
            'description' + ' ' + vlan_name,
            strip_prompt=False,
            strip_command=False
            )
        print (output)

        for port in eval('uplinks_' + key):
            output = net_connect.send_command_timing(
                'interface' + ' ' + port ,
                strip_prompt=False,
                strip_command=False
                )
            print (output)
            output = net_connect.send_command_timing(
                'port hybrid vlan ' + tag + ' tagged' ,
                strip_prompt=False,
                strip_command=False
                )
            print (output)

# LOOPING THROUGH HP 1920 SWITCHES

for key, values in stg_switches_1920.items():
    device_type = values.get('device_type', {})
    ip_address = values.get('ip', {})
    username = values.get('username', {})
    password = values.get('password', {})

    net_connect = ConnectHandler(device_type=device_type, host=ip_address, username=username, password=password)

    output = net_connect.send_command_timing(
        'y', 
        strip_prompt=False, 
        strip_command=False
        )
    output = net_connect.send_command_timing(
        '_cmdline-mode on', 
        strip_prompt=False, 
        strip_command=False
        )
    print (output)
    if 'Continue' in output:
        output += net_connect.send_command_timing(
            'y', 
            strip_prompt=False, 
            strip_command=False
        )
    print (output)
    if 'ssword' in output:
        output = net_connect.send_command_timing(
            'Jinhua1920unauthorized',
            strip_prompt=False, 
            strip_command=False
            )
    print (output)
    output = net_connect.send_command_timing(
        'system-view',
        strip_prompt=False,
        strip_command=False
        )
    print (output)

    for tag, vlan_name in vlans.items():

        output = net_connect.send_command_timing(
            'vlan' + ' ' + tag,
            strip_prompt=False,
            strip_command=False
            )
        print (output)
        output = net_connect.send_command_timing(
            'description' + ' ' + vlan_name,
            strip_prompt=False,
            strip_command=False
            )
        print (output)

        for port in eval('uplinks_' + key):
            output = net_connect.send_command_timing(
                'interface' + ' ' + port ,
                strip_prompt=False,
                strip_command=False
                )
            print (output)
            output = net_connect.send_command_timing(
                'port hybrid vlan ' + tag + ' tagged' ,
                strip_prompt=False,
                strip_command=False
                )
            print (output)

# LOOPING THROUGH ARUBA SWITCHES

for key, values in stg_switches_aruba.items():
    device_type = values.get('device_type', {})
    ip_address = values.get('ip', {})
    username = values.get('username', {})
    password = values.get('password', {})

    net_connect = ConnectHandler(device_type=device_type, host=ip_address, username=username, password=password)

    output = net_connect.send_command_timing(
        'conf t', 
        strip_prompt=False, 
        strip_command=False
        )

    for tag, vlan_name in vlans.items():
        output = net_connect.send_command_timing(
            'vlan' + ' ' + tag,
            strip_prompt=False,
            strip_command=False
            )
        print (output)
        output = net_connect.send_command_timing(
            'name' + ' ' + vlan_name,
            strip_prompt=False,
            strip_command=False
            )
        print (output)  

        for port in eval('uplinks_' + key):
            output = net_connect.send_command_timing(
            'tagged' + ' ' + port ,
            strip_prompt=False, 
            strip_command=False
            )
            print (output)

I feel like I have to justify why I used nested dictionaries. I wanted to be able to get the dictionary name and use it in a forloop. I am open to better suggestion though.

The script failed at "VIL3SW0102". Any idea why it stops there?

Thanks

carlmontanari commented 5 years ago

If that device is slower for any reason (slow links to it, old/slow device) you may just need to crank up the delay factor for it. In your device dicts that have the connection info you can add gloabl_delay_factor key -- could try 2 or 4 in there to slow things down a bit to maybe accommodate that switch.

Otherwise you'll probably want to snag some logs -- you can see examples to get logging setup here

Noct3 commented 5 years ago

Hey Carl,

Thanks I will try that and let you know. It's a fairly recent device though but it's worth a try.

Thanks!

Noct3 commented 5 years ago

Apparently this was some temporary issue. I tried again whitout changing anything and it worked fine.

Thanks again Carl!