telnetlib
It is a module in the Python standard library that provides the client function of the Telnet protocol. Using telnetlib
the module, we can write scripts in Python to automate Telnet sessions, execute commands, and collect output.
This article will introduce telnetlib
the basic use of modules and some examples.
Basic usage
When using telnetlib
, you need to create an Telnet
object first, and then use the object to conduct a Telnet session. A object can be created with the following code Telnet
:
import telnetlib
tn = telnetlib.Telnet(host, port)
where host
is the host name or IP address of the Telnet server, and port
is the port number of the Telnet server.
After creating Telnet
the object, you can use read_until()
the method to wait for data sent by the server. You can use the following code to wait for data sent by the Telnet server:
tn.read_until(b"login: ")
where b"login: "
is a byte array that specifies the string to wait for.
You can use write()
the method to send data to the Telnet server. For example, you can use the following code to send a username and password to a Telnet server:
tn.write(username.encode('ascii') + b"\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
After logging into the Telnet server, you can use write()
the method to execute commands and collect output. For example, you can use the following code to execute ls
the command and collect the output:
tn.write(b"ls\n")
output = tn.read_all().decode('ascii')
print(output)
The above is telnetlib
the basic usage of modules, next we will look at some examples.
Example
Example: Telnet login and execute commands
The following example demonstrates how to use telnetlib
the module to log in to a Telnet server and execute commands:
import telnetlib
# 配置 Telnet 服务器的 IP 地址和端口号
HOST = "localhost"
PORT = 23
# 配置 Telnet 登录信息
username = "admin"
password = "password"
# 创建 Telnet 对象
tn = telnetlib.Telnet(HOST, PORT)
# 等待服务器发送登录提示
tn.read_until(b"login: ")
# 发送用户名
tn.write(username.encode('ascii') + b"\n")
# 等待服务器发送密码提示
tn.read_until(b"Password: ")
# 发送密码
tn.write(password.encode('ascii') + b"\n")
# 执行命令并收集输出
tn.write(b"ls\n")
output = tn.read_all().decode('ascii')
print(output)
# 关闭 Telnet 连接
tn.close()
Here are more examples of the Python telnetlib framework:
1. Implement an interactive command line
Telnetlib can realize interactive command line operation. Here's a simple example that demonstrates how to connect to a remote device, execute a command, and get output:
import telnetlib
HOST = "192.168.0.1"
user = "admin"
password = "password"
tn = telnetlib.Telnet(HOST)
tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"enable\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"show interfaces\n")
output = tn.read_all().decode('ascii')
print(output)
In this example, we first connect to the remote device, then enter the username and password to log in. Next, we enter enable
the command and enter the password again to gain administrator privileges. Finally, we execute show interfaces
the command and print the output.
2. Implement interactive configuration
In addition to executing commands, we can also use telnetlib for interactive configuration. Here is a simple example that demonstrates how to connect to a remote device, enter configuration mode and configure the interface:
import telnetlib
HOST = "192.168.0.1"
user = "admin"
password = "password"
tn = telnetlib.Telnet(HOST)
tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"enable\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"configure terminal\n")
tn.write(b"interface ethernet 1/1\n")
tn.write(b"description Link to Switch A\n")
tn.write(b"no shutdown\n")
tn.write(b"exit\n")
tn.write(b"exit\n")
output = tn.read_all().decode('ascii')
print(output)
In this example, we first connect to the remote device and then enter the username and password to log in. Next, we enter enable
the command and enter the password again to gain administrator rights. Then, we enter configure terminal
the command to enter configuration mode and use interface ethernet 1/1
the command to enter the configuration interface of Ethernet interface 1/1. In this interface, we use description
commands to configure the interface description, use no shutdown
commands to enable the interface, and use exit
commands to exit the interface configuration interface and configuration mode. Finally, we print all the output.