Network engineers learn Python-32-remote connection management Telnet

Python Telnet is a Python library for connecting and managing network devices remotely. Using Python Telnet, you can execute commands on remote devices, upload and download files, set up a Telnet proxy, and more. In this article, we will introduce some popular Python Telnet libraries and how to use them in Python.

Telnetlib

telnetlib is a module for Telnet connection and management in the Python standard library. It provides a Telnet client implementation for remotely connecting to devices and executing commands. Here is an example of using telnetlib to make a Telnet connection and execute a command:

import telnetlib

tn = telnetlib.Telnet('example.com')
tn.read_until(b'login: ')
tn.write(b'username\n')
tn.read_until(b'Password: ')
tn.write(b'password\n')
tn.read_until(b'$ ')
tn.write(b'ls\n')
print(tn.read_all().decode('ascii'))
tn.close()

The above code creates a Telnet connection to example.comthe server and authenticates with the provided username and password. It then executes the command on the remote device lsand prints the command output to the console.

Telnetlib3

Telnetlib3 is a Telnet client library for connecting and managing network devices. It provides more advanced features such as custom prompts, protocol exchange, and Telnet option handling. Here is an example of using Telnetlib3 to make a Telnet connection and execute a command:

from telnetlib3 import Telnet

async def main():
    async with Telnet('example.com') as tn:
        await tn.wait_for('login: ')
        await tn.writeline('username')
        await tn.wait_for('Password: ')
        await tn.writeline('password')
        await tn.wait_for('$ ')
        await tn.writeline('ls')
        output = await tn.read_until_close()
        print(output)

asyncio.run(main())

The above code creates an asynchronous Telnet connection to example.comthe server using Telnetlib3 and authenticates with the provided username and password. It then executes the command on the remote device lsand prints the command output to the console.

Witches

Nornir is a Python library for network automation that automates network device management using Telnetlib and Netmiko. It provides a unified API for managing remote device configuration, file transfer, file operations, etc. Here is an example of using Nornir to connect to a network device and execute a command:

from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_command

nr = InitNornir(config_file='config.yaml')
result = nr.run(task=netmiko_send_command, command_string='show interfaces')
for device, output in result.items():
    print(f'{device}: {output.result}')

The above code uses Nornir to create a Telnet connection to the network device, execute show interfacesthe command, and print the output to the console.

Summarize

Python Telnet provides a powerful way to manage network devices, including remote connections, executing commands, uploading and downloading files, and more. In this article, we introduced some popular Python Telnet libraries, including telnetlib, Telnetlib3, and Nornir. Whether you are a network administrator or a network automation engineer, Python Telnet is one of the skills you must master.

We hope this article can help you better understand Python Telnet and provide you with some useful examples and tools so that you can use Python Telnet for network device management in real work.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132621241