usr bin env python coding utf-8 import socket import sys import re imp

 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import sys
import re
import logging
import socks
HOST = '127.0.0.1'
PORT_A = 9051
PORT_B = 9050
FORMAT = '%(message)s'
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
def change_ip():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
try:
sock.connect((HOST, PORT_A))
logging.debug("Connected to Tor ({0}:{1})".format(HOST, PORT_A))
except socket.error:
logging.error('Error when connect to Tor')
sys.exit(1)
sock.send('AUTHENTICATE ""\n')
logging.debug('Authorization')
sock.send('SIGNAL newnym\n')
logging.debug('Send command to get new ip')
logging.debug('Disconnected')
sock.close()
def check_ip():
domain = 'whoer.net'
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, HOST, PORT_B)
sock = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(15)
host = socket.gethostbyaddr(domain)[2][0]
try:
sock.connect((host, 80))
except socket.error:
logging.error('Error when checking ip')
sys.exit(2)
sock.send('GET {0} HTTP/1.1\r\nHost: {1}\r\n\r\n'.format('/ip', domain))
data = sock.recv(1024)
sock.close()
regex = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
ip = re.search(regex, data).group(0)
return ip
current_ip = check_ip()
logging.debug("Current ip: {0}".format(current_ip))
while True:
change_ip()
ip = check_ip()
if ip != current_ip:
logging.debug("New ip: {0}".format(ip))
break
sys.exit(0)