forked from cls1991/ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng.py
133 lines (103 loc) · 3.42 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
# coding: utf8
"""
Get password of the wifi you're connected, and your current ip address.
"""
import platform
import re
import subprocess
import sys
import click
import requests
SUPPORTED_SYSTEMS = ['Darwin', 'Linux']
LOCAL_IP_ADDRESS = '127.0.0.1'
VERIFY_HOST = 'https://httpbin.org/ip'
def _system():
return platform.system()
def _exec(command):
out, err = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
return '{0}{1}'.format(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>\w+)')
else:
command = ['nm-tool']
pattern = re.compile(r'\*(?P<ssid>\w+):')
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>.+)"')
else:
command = ['sudo', 'cat', '/etc/NetworkManager/system-connections/{0}'.format(ssid)]
pattern = re.compile(r'psk\=(?P<password>\w+)')
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 = LOCAL_IP_ADDRESS
public_ip = LOCAL_IP_ADDRESS
command = ['ifconfig']
if system == 'Darwin':
pattern = re.compile(r'inet (?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
else:
pattern = re.compile(r'inet addr:(?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 != LOCAL_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()