您的位置:首页 > 编程语言 > Python开发

Python远程连接模块-Telnet

2018-03-01 22:50 204 查看
                    Python远程连接模块-Telnet

                                      作者:尹正杰

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

  虽然现在主流的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") + "\n".encode(encoding="utf-8"))
17
18     # 输入登录密码
19     tn.read_until('Password: '.encode(encoding="utf-8"))
20     tn.write(password.encode(encoding="utf-8") + '\n'.encode(encoding="utf-8"))
21
22     # 登录完毕后执行命令
23     tn.read_until(finish.encode(encoding="utf-8"))
24     for command in commands:
25         tn.write(('%s\n' % command).encode("utf-8"))
26     tn.read_until(finish.encode("utf-8"))
27     # 执行完毕后,终止Telnet连接(或输入exit退出)
28     tn.close()  # tn.write('exit\n')
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"\xff\xfd\x18\xff\xfd \xff\xfd#\xff\xfd'"
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'\xff\xfb\x03\xff\xfd\x01\xff\xfd\x1f\xff\xfb\x05\xff\xfd!'
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'\xff\xfb\x03'
56 Telnet(172.16.96.211,23): IAC WILL 3
57 Telnet(172.16.96.211,23): recv b'\xff\xfb\x01CentOS release 6.6 (Final)\r\nKernel 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\r\n'
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\n'
63 Telnet(172.16.96.211,23): recv b'Password: '
64 Telnet(172.16.96.211,23): send b'jiubugaosuni\n'
65 Telnet(172.16.96.211,23): recv b'\r\n'
66 Telnet(172.16.96.211,23): recv b'Last login: Fri Mar  2 15:21:07 from bogon\r\n'
67 Telnet(172.16.96.211,23): recv b'[yinzhengjie@yinzhengjie ~]$ '
68 Telnet(172.16.96.211,23): send b'df -h\n'
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\r\n/dev/mapper/vg_yinzhengjie-lv_root\r\n        '
71 Telnet(172.16.96.211,23): recv b'               50G  6.2G   41G  14% /\r\ntmpfs      '
72 Telnet(172.16.96.211,23): recv b'           494M   68K  494M   1% /dev/shm\r\n/dev/vd'
73 Telnet(172.16.96.211,23): recv b'a1             477M   29M  424M   7% /boot\r\n/dev/m'
74 Telnet(172.16.96.211,23): recv b'apper/vg_yinzhengjie-lv_home\r\n                    '
75 Telnet(172.16.96.211,23): recv b'   47G   86M   45G   1% /home\r\n[yinzhengjie@yinzhe'
76 Telnet(172.16.96.211,23): recv b'ngjie ~]$ '
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: