-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchange_ip.py
More file actions
61 lines (44 loc) · 1.14 KB
/
change_ip.py
File metadata and controls
61 lines (44 loc) · 1.14 KB
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import socket, socks
import requests, time
from stem import Signal
from stem.control import Controller
#Obtain ip address.
URL_SUBMIT="http://icanhazip.com/"
TOR_CONTROLLER_PASS='password' #Change by your password
#Use Tor for any connections.
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 9050, True)
#Class socket by default.
SOCK_DEFAULT=socket.socket
#Class socket for Tor
SOCK_TOR=socks.socksocket
def my_ip():
global URL_SUBMIT, SOCK_TOR
socket.socket=SOCK_TOR
r=requests.get(URL_SUBMIT)
return (r.content).decode("utf-8")[:-1]
def new_ip():
global SOCK_DEFAULT, TOR_CONTROLLER_PASS
socket.socket=SOCK_DEFAULT
changed=True
msg='[OK]'
try:
controller=Controller.from_port(port=9051)
controller.authenticate(password=TOR_CONTROLLER_PASS)
controller.signal(Signal.NEWNYM)
except Exception as err:
changed=False
msg=err
time.sleep(2)
return changed, msg
if __name__ == '__main__':
print("### TOR WITH PYTHON ###")
result=my_ip()
print('+[IP] %s'%result)
modified, msg=new_ip()
if modified:
result=my_ip()
print('+[IP] %s'%result)
else:
print(msg)