Search code examples
rubytor

Get new identity in Tor from Ruby script


require 'net/telnet'

localhost = Net::Telnet::new("Host" => "localhost", "Port" => "9151", "Timeout" => 10, "Prompt" => /250 OK\n/)
localhost.cmd('AUTHENTICATE hi') { |c| print c; throw "Cannot authenticate to Tor" if c != "250 OK\n" }
localhost.cmd('signal NEWNYM') { |c| print c; throw "Cannot switch Tor to new route" if c != "250 OK\n" }
localhost.close

I'm trying to get new identity in running Tor using ruby script, code is taken from this answer: https://stackoverflow.com/a/6882744/3192470.

My port is 9151 because I use Tor-Bundle.
To get a password hash, I wrote tor --hash-password hi in the terminal, which returned 16:970D11D59DCAC06E6075BED460511460C1EFD9CECDBB3C96A59298422A to me.
I have this line HashedControlPassword 16:970D11D59DCAC06E6075BED460511460C1EFD9CECDBB3C96A59298422A in my torrc file accordingly.

When I run my Ruby script, I get uncaught throw "Cannot authenticate to Tor" exception thrown.


Solution

  • You have to put your password in double quotes:

    localhost.cmd('AUTHENTICATE "hi"') { |c| print c; throw "Cannot authenticate to Tor" if c != "250 OK\n" }
    

    That is, the password is that you hashed with tor --hash-password hi, but put in double quotes.