07-17-2020 03:40 AM
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:
Solved! Go to Solution.
07-24-2020 12:06 AM
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)
07-20-2020 12:06 AM
Are you using the telnet driver for telnet connection?
device_type='cisco_ios_telnet'
07-20-2020 12:18 AM
You could try - telnetlib instead of netmiko
import telnetlib conn = telnetlib.Telnet(ip, port, TELNET_TIMEOUT)
07-20-2020 02:20 AM
Script was entirely coded using Netmiko. Will it work if I mix it with telnetlib? And can I run telnetlib using Cisco devices?
07-20-2020 02:18 AM
I'm using the below parameter. Should I add the telnet driver too?
device_type = platform
07-20-2020 02:33 PM
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.
07-21-2020 06:50 AM
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
07-24-2020 12:06 AM
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)
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide