Reputation:
I wrote a script that changes the IP with stem, but it seems like it does not work. Here is a shortened version of the script:
from stem import Signal
from stem.control import Controller
from stem.connection import connect
def changeIP():
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
def printIP():
my_ip = urlopen('http://ip.42.pl/raw').read()
print("IP -> %s" % my_ip)
#Some of my other codes
while(true):
j+=1
if j == 2:
changeIP()
j = 0
printIP()
And it just prints my public IP again and again. It should print the same IP 2 times and then change, but it doesn't. My torrc config is correctly configured.
ControlPort = 9051
HashedControlPassword 16:AD2DD67382E391D960F7E38F49A1AAB31479A0576222AB885C3CCFD70B
cookie authentication 1
I even tried putting the hashed Control password in the control.authenticate(password='AD2DD67382E391D960F7E38F49A1AAB31479A0576222AB885C3CCFD70B')
, but it still did not work, and I also want my script to not use it.
I have been searching for this weeks now, and I found out that I can use SocksiPy
module, but I can't do it.
Please recode my script, Thank you very much.
Upvotes: 1
Views: 2221
Reputation: 41
I am assuming you are working on windows. I am not sure if this will work on Linux.
I would suggest you to download a fresh copy of Tor without altering the torrc config. Personally, I didn't configure the hashcontrolpassword
but the following python code still works for me.
Try the step 1 listed here : https://stackoverflow.com/a/48144473/9183144
Following step 1, You should be able to see 127.0.0.1:9150
and 127.0.0.1:9151
when you run netstat -an
in your terminal.
After that copy the following code and try running it (change the directory to your Tor folder).
# library to launch and kill Tor process
import os
import subprocess
# library for Tor connection
import socket
import socks
import http.client
import time
import requests
from stem import Signal
from stem.control import Controller
def launchTor():
# start Tor (wait 30 sec for Tor to load)
sproc = subprocess.Popen(r'.../Tor Browser/Browser/firefox.exe')
time.sleep(30)
return sproc
def killTor(sproc):
sproc.kill()
def connectTor():
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150, True)
socket.socket = socks.socksocket
print("Connected to Tor")
def set_new_ip():
# disable socks server and enabling again
socks.setdefaultproxy()
"""Change IP using TOR"""
with Controller.from_port(port=9151) as controller:
controller.authenticate()
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150, True)
socket.socket = socks.socksocket
controller.signal(Signal.NEWNYM)
def checkIP():
conn = http.client.HTTPConnection("icanhazip.com")
conn.request("GET", "/")
time.sleep(3)
response = conn.getresponse()
print('current ip address :', response.read())
# Launch Tor and connect to Tor network
sproc = launchTor()
connectTor()
# Check current IP
checkIP()
# Set new IP
set_new_IP()
# Check newly assigned IP
time.sleep(10)
checkIP()
# remember to kill process
killTor(sproc)
Upvotes: 2