Python远程连接模块-Telnet

时间:2022-09-26 00:30:25

       Python远程连接模块-Telnet(该协议明文传输数据不安全,推荐使用ssh协议)

                                        作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  虽然现在主流的python版本还是2.7,相信2020年python程序员都会偏向Python3.x版本的,今天研究了以下网上的telnet程序包,发现挺有意思的,把短连接的代码贴在这,有兴趣的小伙伴可以自行更改,哈哈哈~

 #!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com
import telnetlib def DoTelnet(Host, username, password, finish, commands):
'''''Telnet远程登录:Windows客户端连接Linux服务器'''
# 连接Telnet服务器
tn = telnetlib.Telnet(Host, port=23, timeout=10)
tn.set_debuglevel(2) # 输入登录用户名
tn.read_until('login: '.encode(encoding="utf-8"))
tn.write(username.encode(encoding="utf-8") + "\n".encode(encoding="utf-8")) # 输入登录密码
tn.read_until('Password: '.encode(encoding="utf-8"))
tn.write(password.encode(encoding="utf-8") + '\n'.encode(encoding="utf-8")) # 登录完毕后执行命令
tn.read_until(finish.encode(encoding="utf-8"))
for command in commands:
tn.write(('%s\n' % command).encode("utf-8"))
tn.read_until(finish.encode("utf-8"))
# 执行完毕后,终止Telnet连接(或输入exit退出)
tn.close() # tn.write('exit\n') if __name__ == '__main__':
# 配置选项
Host = '172.16.96.211' # Telnet服务器IP
username = 'yinzhengjie' # 登录用户名
password = 'jiubugaosuni' # 登录密码
finish = '~]$ ' # 命令提示符
commands = ['df -h'] #输入你需要执行的代码
DoTelnet(Host, username, password, finish, commands) #以上代码执行结果如下:
Telnet(172.16.96.211,23): recv b"\xff\xfd\x18\xff\xfd \xff\xfd#\xff\xfd'"
Telnet(172.16.96.211,23): IAC DO 24
Telnet(172.16.96.211,23): IAC DO 32
Telnet(172.16.96.211,23): IAC DO 35
Telnet(172.16.96.211,23): IAC DO 39
Telnet(172.16.96.211,23): recv b'\xff\xfb\x03\xff\xfd\x01\xff\xfd\x1f\xff\xfb\x05\xff\xfd!'
Telnet(172.16.96.211,23): IAC WILL 3
Telnet(172.16.96.211,23): IAC DO 1
Telnet(172.16.96.211,23): IAC DO 31
Telnet(172.16.96.211,23): IAC WILL 5
Telnet(172.16.96.211,23): IAC DO 33
Telnet(172.16.96.211,23): recv b'\xff\xfb\x03'
Telnet(172.16.96.211,23): IAC WILL 3
Telnet(172.16.96.211,23): recv b'\xff\xfb\x01CentOS release 6.6 (Final)\r\nKernel 2.6.32-504.e'
Telnet(172.16.96.211,23): IAC WILL 1
Telnet(172.16.96.211,23): recv b'l6.x86_64 on an x86_64\r\n'
Telnet(172.16.96.211,23): recv b'\xf2'
Telnet(172.16.96.211,23): recv b'login: '
Telnet(172.16.96.211,23): send b'yinzhengjie\n'
Telnet(172.16.96.211,23): recv b'Password: '
Telnet(172.16.96.211,23): send b'jiubugaosuni\n'
Telnet(172.16.96.211,23): recv b'\r\n'
Telnet(172.16.96.211,23): recv b'Last login: Fri Mar 2 15:21:07 from bogon\r\n'
Telnet(172.16.96.211,23): recv b'[yinzhengjie@yinzhengjie ~]$ '
Telnet(172.16.96.211,23): send b'df -h\n'
Telnet(172.16.96.211,23): recv b'Filesystem Size Used Avail Use% Mounte'
Telnet(172.16.96.211,23): recv b'd on\r\n/dev/mapper/vg_yinzhengjie-lv_root\r\n '
Telnet(172.16.96.211,23): recv b' 50G 6.2G 41G 14% /\r\ntmpfs '
Telnet(172.16.96.211,23): recv b' 494M 68K 494M 1% /dev/shm\r\n/dev/vd'
Telnet(172.16.96.211,23): recv b'a1 477M 29M 424M 7% /boot\r\n/dev/m'
Telnet(172.16.96.211,23): recv b'apper/vg_yinzhengjie-lv_home\r\n '
Telnet(172.16.96.211,23): recv b' 47G 86M 45G 1% /home\r\n[yinzhengjie@yinzhe'
Telnet(172.16.96.211,23): recv b'ngjie ~]$ '