-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathng.py
executable file
·184 lines (146 loc) · 5.11 KB
/
ng.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
# coding: utf8
# .-----------------. .----------------.
# | .--------------. || .--------------. |
# | | ____ _____ | || | ______ | |
# | ||_ \|_ _| | || | .' ___ | | |
# | | | \ | | | || | / .' \_| | |
# | | | |\ \| | | || | | | ____ | |
# | | _| |_\ |_ | || | \ `.___] _| | |
# | ||_____|\____| | || | `._____.' | |
# | | | || | | |
# | '--------------' || '--------------' |
# '----------------' '----------------'
"""
Get password of the wifi you're connected, and your current ip address.
"""
import locale
import platform
import re
import subprocess
import sys
import click
import requests
_ver = sys.version_info
# Python 2.x?
is_py2 = (_ver[0] == 2)
# Python 3.x?
is_py3 = (_ver[0] == 3)
SUPPORTED_SYSTEMS = ['Darwin', 'Linux', 'Windows']
DEFAULT_LOCALE_LANGUAGE = ('en_US', 'UTF-8')
DEFAULT_IP_ADDRESS = '127.0.0.1'
VERIFY_HOST = 'https://httpbin.org/ip'
def _system():
return platform.system()
def _language():
try:
language = locale.getdefaultlocale()
except ValueError:
language = DEFAULT_LOCALE_LANGUAGE
return language
def _exec(command):
out, err = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
if is_py3:
language = _language()
return (out + err).decode(language[1]).strip()
return (out + err).strip()
def _detect_wifi_ssid():
system = _system()
if system not in SUPPORTED_SYSTEMS:
return False, 'Unknown operation system {0}'.format(system)
if system == 'Darwin':
command = ['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '-I']
pattern = re.compile(r' SSID: (?P<ssid>.+)')
elif system == 'Linux':
command = ['nmcli', '-t', '-f', 'active,ssid', 'dev', 'wifi']
pattern = re.compile(r"yes:'(?P<ssid>.+)'")
else:
command = ['netsh', 'wlan', 'show', 'interfaces']
pattern = re.compile(r' SSID.+: (?P<ssid>.+)\r')
rs = _exec(command)
match = re.search(pattern, rs)
if not match:
return False, rs
return True, match.group('ssid')
def _hack_wifi_password(ssid):
system = _system()
if system not in SUPPORTED_SYSTEMS:
return False, 'Unknown operation system {0}'.format(system)
if system == 'Darwin':
command = ['security', 'find-generic-password', '-D', 'AirPort network password', '-ga', ssid]
pattern = re.compile(r'password: "(?P<password>.+)"')
elif system == 'Linux':
command = ['sudo', 'cat', '/etc/NetworkManager/system-connections/{0}'.format(ssid)]
pattern = re.compile(r'psk\=(?P<password>.+)')
else:
command = ['netsh', 'wlan', 'show', 'profile', 'name={0}'.format(ssid), 'key=clear']
language = _language()
if language[0] == 'zh_CN':
if is_py3:
pattern = re.compile(r'关键内容.+: (?P<password>.+)')
else:
pattern = re.compile(r'{0}.+: (?P<password>.+)'.format(u'关键内容'.encode(language[1])))
else:
pattern = re.compile(r'Key Content.+: (?P<password>.+)')
rs = _exec(command)
match = re.search(pattern, rs)
if not match:
return False, rs
return True, match.group('password')
def _hack_ip():
system = _system()
if system not in SUPPORTED_SYSTEMS:
return False, 'Unknown operation system {0}'.format(system)
local_ip = public_ip = DEFAULT_IP_ADDRESS
if system == 'Darwin':
command = ['ifconfig']
pattern = re.compile(r'inet (?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
elif system == 'Linux':
command = ['ip', 'addr']
pattern = re.compile(r'inet (?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
else:
command = ['ipconfig']
pattern = re.compile(r'IPv4.+: (?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
rs = _exec(command)
for match in re.finditer(pattern, rs):
sip = match.group('ip')
if sip != DEFAULT_IP_ADDRESS:
local_ip = sip
break
try:
r = requests.get(VERIFY_HOST)
public_ip = r.json()['origin']
except requests.RequestException:
pass
return True, '{0}\n{1}'.format(local_ip, public_ip)
@click.group()
def cli():
"""Get password of the wifi you're connected, and your current ip address."""
pass
@click.command()
def ip():
"""Show ip address."""
ok, err = _hack_ip()
if not ok:
click.secho(click.style(err, fg='red'))
sys.exit(1)
click.secho(click.style(err, fg='green'))
@click.command()
@click.argument('ssid', required=False)
def wp(ssid):
"""Show wifi password."""
if not ssid:
ok, err = _detect_wifi_ssid()
if not ok:
click.secho(click.style(err, fg='red'))
sys.exit(1)
ssid = err
ok, err = _hack_wifi_password(ssid)
if not ok:
click.secho(click.style(err, fg='red'))
sys.exit(1)
click.secho(click.style('{ssid}:{password}'.format(ssid=ssid, password=err), fg='green'))
# Install click commands.
cli.add_command(ip)
cli.add_command(wp)
if __name__ == '__main__':
cli()