cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3499
Views
5
Helpful
7
Replies

Unable to print telnet test result to Python

normzie
Level 1
Level 1

Hi All,

Hope someone can help us with this. We're not successful in printing a telnet test result. Netmiko is being used with Python. Tried several methods but it's the same error. Below is the code and the output that should appear. Other commands execute fine and results are printed except for telnet. It seems that the problem is on terminating the telnet. All the methods I tried doesn't seem to work to terminate telnet. Appreciate your help on this. Thanks!

 

sr_telnet_output = device.send_command('telnet 40.113.200.201 443 /source-interface vlan 1400', delay_factor=1)
# Already tried using various combinations of ASCII codes with different methods like write_channel(), send_command_timing() but still the same.
# Please see scripts below
                                       
device.write_channel('\x036','\x036','\x036','\x036','\x036','\x036','\n','\n')
device.write_channel('\x036')
time.sleep(0.2)
                        device.write_channel('\x036')
                        time.sleep(0.2)
                        device.write_channel('\x036')
                        time.sleep(0.2)
                        device.write_channel('\x036')
                        time.sleep(0.2)
                        device.write_channel('\x036')
                        time.sleep(0.2)
                        device.write_channel('\x036')
                        time.sleep(0.2)
                        device.write_channel('\n')
                        time.sleep(0.2)
                        device.write_channel('\n')

device.send_command_timing('\x036')
device.send_command_timing('\x036')
device.send_command_timing('\x036')
device.send_command_timing('\x036')
device.send_command_timing('\x036')
device.send_command_timing('\x036')
device.send_command_timing('\x036')
device.send_command_timing('\x036')
device.write_channel('\n')
device.write_channel('\n')
time.sleep(0.1)
sr_telnet_output = device.read_channel()
device.clear_buffer()'
device.disconnect()
print(sr_telnet_output)

Error message:

Traceback (most recent call last):
  File "bot.py", line 170, in <module>
    sr_telnet_output = device.send_command('telnet 40.113.200.201 443 /source-in
terface vlan 1400', delay_factor=1)
  File "D:\python\bot\lib\site-packages\netmiko\base_connection.py", line 129
5, in send_command
    search_pattern
OSError: Search pattern never detected in send_command_expect: Switch-\A\#

Desired result:

 

telnet result.JPG

1 Accepted Solution

Accepted Solutions

Finally we're able to find a solution!

The code below helped us. I'll be closing the discussion now. Thanks for all your inputs!

 

from netmiko import ConnectHandler
import csv
import os
import sys
import netmiko


connection = netmiko.ConnectHandler(ip="10.1.115.146", device_type="cisco_ios", username="admin", password="cisco")
output = connection.send_command("telnet 8.8.8.8 443 /source-interface vlan 7", expect_string=r"Trying")
connection.disconnect()
print(output)

View solution in original post

7 Replies 7

omz
VIP Alumni
VIP Alumni

Are you using the telnet driver for telnet connection? 

device_type='cisco_ios_telnet'

 

omz
VIP Alumni
VIP Alumni

You could try - telnetlib instead of netmiko 

import telnetlib
conn = telnetlib.Telnet(ip, port, TELNET_TIMEOUT)

Script was entirely coded using Netmiko. Will it work if I mix it with telnetlib? And can I run telnetlib using Cisco devices?

I'm using the below parameter. Should I add the telnet driver too?

device_type = platform

Yea - Im 90% sure the comment above is your issue - fix your device type.   I had the same type of problem with F5 not having the right device type.

I tried it and it resulted in the error message below. The error message seem to be related on how the python will connect to the device which is using Telnet and the device is not be accessible via telnet for security purposes.

Our goal is to run a telnet test on a specified IP address from the core switch from user input, then print the result.

 

Traceback (most recent call last):
  File "tshoot.py", line 166, in <module>
    device = ConnectHandler(device_type='cisco_ios_telnet', ip=switch_A, usernam
e=username, password=password, secret=password)
  File "D:\python\bot\lib\site-packages\netmiko\ssh_dispatcher.py", line 228,
 in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "D:\python\bot\lib\site-packages\netmiko\base_connection.py", line 283
, in __init__
    self.establish_connection()
  File "D:\python\pewbot\lib\site-packages\netmiko\base_connection.py", line 846
, in establish_connection
    self.host, port=self.port, timeout=self.timeout
  File "D:\python\pewbot\lib\telnetlib.py", line 218, in __init__
    self.open(host, port, timeout)
  File "D:\python\pewbot\lib\telnetlib.py", line 234, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "D:\python\pewbot\lib\socket.py", line 727, in create_connection
    raise err
  File "D:\python\pewbot\lib\socket.py", line 716, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the
 target machine actively refused it

Finally we're able to find a solution!

The code below helped us. I'll be closing the discussion now. Thanks for all your inputs!

 

from netmiko import ConnectHandler
import csv
import os
import sys
import netmiko


connection = netmiko.ConnectHandler(ip="10.1.115.146", device_type="cisco_ios", username="admin", password="cisco")
output = connection.send_command("telnet 8.8.8.8 443 /source-interface vlan 7", expect_string=r"Trying")
connection.disconnect()
print(output)
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: