Python远程连接模块-Telnet

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

                                        作者:尹正杰

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

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

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #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/
 5 #EMAIL:y1053419035@qq.com
 6 import telnetlib
 7 
 8 def DoTelnet(Host, username, password, finish, commands):
 9     '''''Telnet远程登录:Windows客户端连接Linux服务器'''
10     # 连接Telnet服务器
11     tn = telnetlib.Telnet(Host, port=23, timeout=10)
12     tn.set_debuglevel(2)
13 
14     # 输入登录用户名
15     tn.read_until('login: '.encode(encoding="utf-8"))
16     tn.write(username.encode(encoding="utf-8") + "
".encode(encoding="utf-8"))
17 
18     # 输入登录密码
19     tn.read_until('Password: '.encode(encoding="utf-8"))
20     tn.write(password.encode(encoding="utf-8") + '
'.encode(encoding="utf-8"))
21 
22     # 登录完毕后执行命令
23     tn.read_until(finish.encode(encoding="utf-8"))
24     for command in commands:
25         tn.write(('%s
' % command).encode("utf-8"))
26     tn.read_until(finish.encode("utf-8"))
27     # 执行完毕后,终止Telnet连接(或输入exit退出)
28     tn.close()  # tn.write('exit
')
29 
30 
31 if __name__ == '__main__':
32     # 配置选项
33     Host = '172.16.96.211'          # Telnet服务器IP
34     username = 'yinzhengjie'        # 登录用户名
35     password = 'jiubugaosuni'       # 登录密码
36     finish = '~]$ '                 # 命令提示符
37     commands = ['df -h']            #输入你需要执行的代码
38     DoTelnet(Host, username, password, finish, commands)
39 
40 
41 
42 
43 #以上代码执行结果如下:
44 Telnet(172.16.96.211,23): recv b"xffxfdx18xffxfd xffxfd#xffxfd'"
45 Telnet(172.16.96.211,23): IAC DO 24
46 Telnet(172.16.96.211,23): IAC DO 32
47 Telnet(172.16.96.211,23): IAC DO 35
48 Telnet(172.16.96.211,23): IAC DO 39
49 Telnet(172.16.96.211,23): recv b'xffxfbx03xffxfdx01xffxfdx1fxffxfbx05xffxfd!'
50 Telnet(172.16.96.211,23): IAC WILL 3
51 Telnet(172.16.96.211,23): IAC DO 1
52 Telnet(172.16.96.211,23): IAC DO 31
53 Telnet(172.16.96.211,23): IAC WILL 5
54 Telnet(172.16.96.211,23): IAC DO 33
55 Telnet(172.16.96.211,23): recv b'xffxfbx03'
56 Telnet(172.16.96.211,23): IAC WILL 3
57 Telnet(172.16.96.211,23): recv b'xffxfbx01CentOS release 6.6 (Final)
Kernel 2.6.32-504.e'
58 Telnet(172.16.96.211,23): IAC WILL 1
59 Telnet(172.16.96.211,23): recv b'l6.x86_64 on an x86_64
'
60 Telnet(172.16.96.211,23): recv b'xf2'
61 Telnet(172.16.96.211,23): recv b'login: '
62 Telnet(172.16.96.211,23): send b'yinzhengjie
'
63 Telnet(172.16.96.211,23): recv b'Password: '
64 Telnet(172.16.96.211,23): send b'jiubugaosuni
'
65 Telnet(172.16.96.211,23): recv b'
'
66 Telnet(172.16.96.211,23): recv b'Last login: Fri Mar  2 15:21:07 from bogon
'
67 Telnet(172.16.96.211,23): recv b'[yinzhengjie@yinzhengjie ~]$ '
68 Telnet(172.16.96.211,23): send b'df -h
'
69 Telnet(172.16.96.211,23): recv b'Filesystem            Size  Used Avail Use% Mounte'
70 Telnet(172.16.96.211,23): recv b'd on
/dev/mapper/vg_yinzhengjie-lv_root
        '
71 Telnet(172.16.96.211,23): recv b'               50G  6.2G   41G  14% /
tmpfs      '
72 Telnet(172.16.96.211,23): recv b'           494M   68K  494M   1% /dev/shm
/dev/vd'
73 Telnet(172.16.96.211,23): recv b'a1             477M   29M  424M   7% /boot
/dev/m'
74 Telnet(172.16.96.211,23): recv b'apper/vg_yinzhengjie-lv_home
                    '
75 Telnet(172.16.96.211,23): recv b'   47G   86M   45G   1% /home
[yinzhengjie@yinzhe'
76 Telnet(172.16.96.211,23): recv b'ngjie ~]$ '
原文地址:https://www.cnblogs.com/yinzhengjie/p/8490950.html