Skip to content

Instantly share code, notes, and snippets.

@Sic4rio
Forked from tarcisio-marinho/change_ip.py
Created May 8, 2023 05:25
Show Gist options
  • Save Sic4rio/92ddf2bf4db189f9cf8194310fca6c89 to your computer and use it in GitHub Desktop.
Save Sic4rio/92ddf2bf4db189f9cf8194310fca6c89 to your computer and use it in GitHub Desktop.
change IP address using tor with Python3
# change IP via tor with python
import requests
from stem import Signal
from stem.control import Controller
import socks, socket
import time
# tuts
# https://www.torproject.org/docs/faq.html.en#torrc
# https://stem.torproject.org/tutorials/the_little_relay_that_could.html
# https://stackoverflow.com/questions/9887505/how-to-change-tor-identity-in-python
# sudo pip3 install stem
# sudo apt-get install tor
# generate new password for tor > tor --hash-password "<new_password>"
# save new password hash -> /etc/tor on line -> HashedControlPassword
# uncomment HashedControlPassword and ControlPort
# restart tor service -> /etc/init.d/tor restart -> sudo service tor restart
def changeIp(current_ip):
controller.signal(Signal.NEWNYM)
time.sleep(controller.get_newnym_wait())
with Controller.from_port(port=9051) as controller:
controller.authenticate(password='sexo')
socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5, addr="127.0.0.1", port=9050)
socket.socket = socks.socksocket
current_ip = requests.get(url='http://icanhazip.com/')
print(current_ip.text)
changeIp(current_ip.text)
current_ip = requests.get(url='http://icanhazip.com/')
print(current_ip.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment