python telnet使用方法举例。
#coding=utf-8
import telnetlibimport re
import time
HOST = 'hostip'
user = 'user'
password = 'password'
sh = 'sh'
su = 'su'
reboot = 'reboot'
tn = telnetlib.Telnet(HOST)
tn.read_until("Login: ")
print ("input user")
tn.write(user + "\n")
tn.read_until("Password: ")
print ("input pwd")
tn.write(password + "\n")
print ("Login in success")
time.sleep(2)
tn.write(sh + "\n")
time.sleep(2)
print ("sh success")
tn.write(su + "\n")
time.sleep(2)
print ("su success")
tn.write(reboot + "\n")
time.sleep(2)
print ("reboot success")
print (tn.read_all())
tn.close()