-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathorangetool_ip.py
239 lines (211 loc) · 6.95 KB
/
orangetool_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf-8 -*-
"""Orangetool IP functions."""
from .orangetool_system import restart as restart_func
from .orangetool_params import IP_PATTERN, GLOBAL_IP_API_1, GENERAL_ERROR_MESSAGE
import subprocess as sub
import socket
import os
import requests
import re
import platform
def internet(host="8.8.8.8", port=53, timeout=3, debug=False):
"""
Check internet connections.
:param host: the host that check connection to
:type host:str
:param port: port that check connection with
:type port:int
:param timeout: times that check the connection
:type timeout:int
:param debug:flag for using debug mode
:type debug:bool
:return bool: True if connection is stable
>>> internet() # if there is stable internet connection
True
>>> internet() # if there is no stable internet connection
False
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception as e:
if debug:
print(str(e))
return False
def local_ip(debug=False):
"""
Return local ip of computer in windows by socket module and in unix with hostname command in shell.
:param debug:flag for using debug mode
:type debug:bool
:return: local ip as string
"""
try:
ip = socket.gethostbyname(socket.gethostname())
if ip != "127.0.0.1":
return ip
if platform.system() != "Windows":
command = sub.Popen(["hostname",
"-I"],
stdout=sub.PIPE,
stderr=sub.PIPE,
stdin=sub.PIPE,
shell=False)
response = list(command.communicate())
if len(response[0]) > 0:
return str(response[0])[2:-4]
return GENERAL_ERROR_MESSAGE
return GENERAL_ERROR_MESSAGE
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def global_ip(debug=False):
"""
Return ip with by http://ipinfo.io/ip api.
:param debug:flag for using debug mode
:type debug:bool
:return: global ip as string
"""
try:
new_session = requests.session()
response = new_session.get(GLOBAL_IP_API_1)
ip_list = re.findall(IP_PATTERN, response.text)
new_session.close()
return ip_list[0]
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def set_ip(ip, restart=False, device="eth0", debug=False):
"""
Set static ip in interfaces file (need sudo).
:param ip: static ip
:type ip :str
:param restart : restart flag
:type restart : bool
:param device: network device name
:type device:str
:param debug: flag for using debug mode
:type debug:bool
:return: True in successful
"""
static_string = '''
auto lo device
iface lo inet loopback
iface device inet static
address ip
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
'''
try:
if not bool(re.match(IP_PATTERN, ip)) or ip.find(
"192.168.") == -1 or device not in mac().keys():
raise Exception("IP Formation Error")
static_string = static_string.replace("ip", ip)
static_string = static_string.replace("device", device)
file = open("/etc/network/interfaces", "w")
file.write(static_string)
file.close()
sub.Popen(["ifdown", device, "&&", "ifup", device],
stderr=sub.PIPE, stdin=sub.PIPE, stdout=sub.PIPE)
if restart:
restart_func()
return True
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def ping(ip, packet_number=3, debug=False):
"""
Ping ip and return True if this ip is available and False otherwise.
:param ip: target ip
:type ip :str
:param packet_number: number of packet to size
:type packet_number:int
:param debug: flag for using debug mode
:type debug:bool
:return: a boolean value (True if ip is available and False otherwise)
"""
try:
if not re.match(IP_PATTERN, ip):
raise Exception("IP Formation Error")
output = str(list(sub.Popen(["ping",
ip,
"-c",
str(packet_number)],
stdout=sub.PIPE,
stderr=sub.PIPE).communicate())[0])
if output.find("Unreachable") == -1:
return True
return False
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def mac(debug=False):
"""
Return mac addresses of net devices.
:param debug: flag for using debug mode
:type debug:bool
:return: return mac addresses as dict with name as keys and mac addresses as values
"""
try:
net_dir = "/sys/class/net"
mac_list = []
dir_list = os.listdir(net_dir)
for item in dir_list:
mac_addr = open(net_dir + "/" + item + "/address", "r")
mac_list.append(mac_addr.read()[:-1])
mac_addr.close()
return dict(zip(dir_list, mac_list))
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def network_control(command, device="eth0", debug=False):
"""
Control network adaptor.
:param command: input command
:type command: str
:param device: network device name
:type device:str
:param debug: flag for using debug mode
:type debug:bool
:return: True in successful and False otherwise
"""
try:
cmd = "up"
if command == "down":
cmd = "down"
cmd_out = sub.Popen(["ifconfig", device, cmd],
stderr=sub.PIPE, stdin=sub.PIPE, stdout=sub.PIPE)
output = list(cmd_out.communicate())
if len(output[0]) == 0 and len(output[1]) == 0:
return True
return False
except Exception as e:
if debug:
print(str(e))
return GENERAL_ERROR_MESSAGE
def network_enable(device="eth0", debug=False):
"""
Shortcut to enable network adaptor.
:param device: network device name
:type device:str
:param debug: flag for using debug mode
:type debug:bool
:return: True in successful and False otherwise
"""
return network_control("up", device=device, debug=debug)
def network_disable(device="eth0", debug=False):
"""
Shortcut to disable network adaptor.
:param device: network device name
:type device:str
:param debug: flag for using debug mode
:type debug:bool
:return: True in successful and False otherwise
"""
return network_control("down", device=device, debug=debug)