#!perl -w use strict; package TOR; use strict; use IO::Socket; use Net::Cmd; use Getopt::Long; my $password; GetOptions( "p=s"=>\$password ); our @ISA = qw(Net::Cmd IO::Socket::INET); sub new { my ($class)= @_; my $torsock= $class->SUPER::new(PeerAddr=>"127.0.0.1", Timeout=>10, PeerPort=>9051, Proto=>'tcp') or die "localhost:9051: $@\n"; $torsock->autoflush(1); #$torsock->debug(1); return $torsock; } sub authenticate { my ($tor)= @_; $tor->command($password ? "AUTHENTICATE \"$password\"" : "AUTHENTICATE"); return ($tor->response()==CMD_OK); } sub getinfo { my ($tor, $type)= @_; $tor->command("GETINFO", $type); if ($tor->response()==CMD_OK) { my $reply= $tor->message(); if ($reply =~ /$type=(.*)/) { return $1; } } } sub signal { my ($tor, $type)= @_; $tor->command("SIGNAL", $type); my $rc= $tor->response(); if ($rc!=CMD_OK) { printf("error: %d\n", $rc); } printf("signal: %s\n", $tor->message); return 1; } package main; my $tor= TOR->new(); $tor->authenticate(); $tor->signal("NEWNYM");