发布时间:2019-07-26 09:46:25编辑:auto阅读(2237)
玩蛇网本篇文章主题是Python教程之如何通过telnet执行脚本达到可以登陆远程机器,并执行命令的流程。python telnet调用,登录远程机器执行命令的方法是怎么样的?整段代码中用到了哪几个python模块?大家通过以下的代码就可以了解到整个流程的思路。
端口交互在代码中会用到telnetlib,python sys模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import telnetlib, sys if not HOST: try: HOST = sys.argv[1] USER = sys.argv[2] PASS = sys.argv[3] COMMAND = sys.argv[4] except: print "Usage: telnetdo.py host user pass command" return msg = ['Debug messages:\n'] # tn = telnetlib.Telnet() # try: tn.open(HOST) except: print "Cannot open host" return #msg.append(tn.expect(['login:'], 5)) # tn.read_until("login:") tn.write(USER + '\n') if PASS: #msg.append(tn.expect(['Password:'], 5)) tn.read_until("Password:") tn.write(PASS + '\n') #msg.append(tn.expect([USER], 5)) tn.write(COMMAND + '\n') tn.write("exit\n") #msg.append(tn.expect(['#'], 5)) tmp = tn.read_all() tn.close() del tn return tmp
|
玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/code/c2701.html [复制]
上一篇: python一二三【warning模块使
下一篇: 新手容易懵之—Python虚拟环境
47490
45792
36789
34321
28958
25594
24441
19608
19109
17630
5463°
6046°
5568°
5636°
6571°
5374°
5375°
5882°
5853°
7167°