-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_ip.py
80 lines (74 loc) · 2.33 KB
/
find_ip.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Author: yasong
# @Date: 2017-04-21 11:08:30
# @Last Modified by: Xiaokang Yin
# @Last Modified time: 2017-04-27 10:07:51
import os
import sys
import subprocess
cmd="cmd.exe"
ip_pre = "10.104.171."
baidu = 'www.baidu.com'
empty_ip = []
cmd_ip = 'netsh interface ip set address "以太网" static '#your Ethernet name
netmask = ' 255.255.255.0'
gateway = ' 10.104.171.1'
def find_ip():
p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
p.stdin.write("arp -a"+"\n");
p.stdin.close()
i = 0;
ip =''
for line in p.stdout.readlines():
index = line.find(ip_pre)
if (index == 6):
for j in range(6,20):
ip = ip + line[j]
print"the ip of this host is %s"%ip
#print "the empty ip is:"
if (index == 2):
i = i + 1
ip = ip_pre + str(i)
while line.find(ip_pre + str(i)) < 0 :
#print ip_pre + str(i)
empty_ip.append(ip_pre+str(i))
i = i + 1
if i > 255:
sys.exit(0)
def ping():
p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
p.stdin.write("ping "+ baidu + "\n");
p.stdin.close()
p.wait()
result = p.stdout.read()
#print"result is:%s"%result
#print result[1]
if result.find("0%") < 0:
return 0
else:
return 1
def change_ip():
flag = 0
for i in range(len(empty_ip)):
p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
p.stdin.write(cmd_ip + empty_ip[i] + netmask + gateway + "\n");
p.stdin.close()
p.wait()
result = p.stdout.read()
print len(result)
if len(result) == 0:
if ping() == 1:
print "the ip %s address is ok!"%empty_ip[i]
break
elif result.find("请求的操作需要提升(作为管理员运行)。"):
print "you should run it in Administrator privileges"
break
def main():
find_ip()
print "the empty ip is:"
for i in range(len(empty_ip)):
print empty_ip[i]
#change_ip()
if __name__ == '__main__':
main()