Tor-New-Nym the code way

Hey there, it’s me again. This is a script I created a long time back. But when I decided to write a script for a tor notifier I recalled OMG, I do have a script that would help me fucking change the IP of tor programmatically. Oops sorry, if you have absolutely no idea what I am talking about, maybe you are in the wrong place, you should switch page and read one of the other posts.

For all of those you idiots who use tor, you know about the control port that usually resides at the 9051 port (unless you are damn clever to change it for sake of safety)

Well, I was in my baby boots when I was thrown into a data scraping project. Data scraping is the kind of the theft that you do when you know how to handle your python and beautiful-soup. One of the important steps in doing a theft that you have to make yourself invisible, kind of. And that was my job. To make the thieves invisible. The transporter.

I was asked to setup a tor based network  of machines which could be run as cluster for parallel data scraping. To those of who know about Linux, yea, well it is couple of apt-gets and few torrc edits and got it working. But the nightmare was yet to come.

There was terrible drop in certain exit nodes. And some of the servers had many exit nodes blacklisted. Which meant the scripts returned errors or null values time to time.

One fucking issue I had to face was converting socks5 proxy to http. I had only two options polipo and privoxy. I sticked with polipo as it was my pie. Polipo was crashing all over the place causing this.

504 Connect to failed: SOCKS request rejected or failed

Er… I needed something. So over a midnight of caffeine and nicotine I managed to pen down a script that would help the scraping scripts change Tor IP if encountered with a null return value or reject.  

“expect” was the miracle I was looking for.

Those who have used bash know how flexible it is to use multiple languages within same script. That is the beauty of shell, you can almost port from anything into anything. That is why I love bash. At a certain point I even used bash to scrape shit out of internet.

Following is the script that I created. With explanation, obviously.


Command Explanation
#!/usr/bin/expect Just like Shebang (or even another shebang)
spawn telnet localhost 9051 Asking expect to create a new connection to the control port of Tor. 9051 is default, which was handy back then.
expect “Escape character is ‘^]’.” Expect “expect” responses. Which are mostly passed as strings. In this case, we are passing the response from telnet.
send “authenticate \”yourpassword\”\r” Asking expect to send the string. “authenticate \”yourpassword\”\r”  

Send the string back to the telnet connection spawned causing telnet to receive the password. \r is the carriage return (was a mess when I first did it).

expect “250 OK” The response if the connection was successful.
sleep 3 Waiting for the connection to stabilize
send “signal newnym\r” Sending the newnym signal to tor control port to create a new circuit mostly causing in a new IP (surely a new identity).
expect “250 OK” The response if the connection was successful
sleep 2 Stabilizing
send “\x1D\r” Send the escape sequence to Telnet
expect “telnet>” Telnet exited, so expect the telnet shell prompt there.
send “q” Quit Connection

“Expect” is kind of another interpreter like bash. The beauty of expect is that we can interact with “interactive shells” while inside a script. It took me quite a long time to understand and make this script work. Well, first of all, I am really poor in reading and understanding stuff and second of all I was working in my office, through the night taking nothing but caffeine as food and nicotine for air. So, I wasn’t totally in my senses.

This are some of the documentations that you could use to script in expect

The beauty of expect is that we can use it in between bash. The benefit is that we can simply write down an entire different script that uses expect and call it as a function or something when there is an interactive requirement.

I so much love using TOR and this was a special requirement. That is why I had to find a fast and effective way of communicating with TOR. I don’t know if there are any easier methods out there but this is what I was able to do. If you know something better, let me know so that I can also take a look at it (shit I typed ‘look a tit’). 

And yea, for those who are newbies in TOR, just know that Tor is one fucking much more powerful thing than the typical Tor browser or simply running Tor at port 9050. Try and interact with the control port and learn to modify the torrc. You will eventually be torrified. I mean terrified.

The script is available for download at

I don’t suppose I need to create an installer for this? But it is easy and straightforward. I will cover a section on how to make binary installers for Linux in next blog. And how to make a script available as a command as well.

Leave a Reply

Your email address will not be published. Required fields are marked *