在Python中,你可以使用第三方庫paramiko
來調用Telnet命令
pip install paramiko
然后,你可以使用以下代碼示例來調用Telnet命令:
import paramiko
# 設置Telnet服務器的IP地址和端口
ip = "192.168.1.1"
port = 23
# 創建一個Telnet對象
tn = paramiko.Telnet(ip, port)
# 登錄到Telnet服務器(如果需要的話)
username = "your_username"
password = "your_password"
tn.read_until(b"login: ")
tn.write(username.encode() + b"\n")
tn.read_until(b"Password: ")
tn.write(password.encode() + b"\n")
# 調用Telnet命令
tn.write(b"your_command\n")
output = tn.read_until(b"$ ").decode() # 假設命令提示符為"$"
# 打印輸出結果
print(output)
# 退出Telnet會話
tn.write(b"exit\n")
請將your_username
、your_password
和your_command
替換為實際的值。注意,這個示例假設Telnet服務器的命令提示符為"$",你可能需要根據實際情況進行調整。