def test_pid_by_name_tasklist(self, call_mock): """ Tests the pid_by_name function with tasklist responses. """ call_mock.side_effect = mock_call('tasklist', GET_PID_BY_NAME_TASKLIST_RESULTS) self.assertEqual(3712, system.pid_by_name('tor')) self.assertEqual(None, system.pid_by_name('DirectoryService')) self.assertEqual(None, system.pid_by_name('blarg')) self.assertEqual([3712, 3713], system.pid_by_name('tor', multiple = True))
def test_pid_by_name_ps_bsd(self, call_mock): """ Tests the pid_by_name function with the bsd variant of ps. """ call_mock.side_effect = mock_call(system.GET_PID_BY_NAME_PS_BSD, GET_PID_BY_NAME_PS_BSD) self.assertEqual(1, system.pid_by_name('launchd')) self.assertEqual(11, system.pid_by_name('DirectoryService')) self.assertEqual(None, system.pid_by_name('blarg')) call_mock.side_effect = mock_call(system.GET_PID_BY_NAME_PS_BSD, GET_PID_BY_NAME_PS_BSD_MULTIPLE) self.assertEqual([1, 41], system.pid_by_name('launchd', multiple = True))
def test_pid_by_name_lsof(self, call_mock): """ Tests the pid_by_name function with lsof responses. """ responses = dict(GET_PID_BY_NAME_BASE_RESULTS) responses['success'] = ['1111'] responses['multiple_results'] = ['123', '456', '789'] call_mock.side_effect = mock_call(system.GET_PID_BY_NAME_LSOF, responses) for test_input in responses: expected_response = 1111 if test_input == 'success' else None self.assertEqual(expected_response, system.pid_by_name(test_input)) self.assertEqual([123, 456, 789], system.pid_by_name('multiple_results', multiple = True))
def get_entry_ip(): resolvers = system_resolvers() # check avaiable resolvers tor_pids = pid_by_name('tor', multiple=True) # gets tor's PID if len(resolvers) > 0 and len(tor_pids) > 0: picked_resolver = resolvers[0] # getting the first one try: process_pid = tor_pids[0] # getting the first, if we are running tor only as a service it must have just one PID cons = get_connections(picked_resolver, process_pid, process_name='tor') return cons except ValueError, IOError: print "Error getting connections to entry point"
def start_tor(): tor_process = system.pid_by_port(SOCKS_PORT) if tor_process is None: tor_process = system.pid_by_name('tor') if tor_process is None: tor_process = stem.process.launch_tor_with_config( config={ 'SocksPort': str(SOCKS_PORT), 'ControlPort': str(CONTROL_PORT), 'ExitNodes': '{ru}', }, init_msg_handler=print_bootstrap_lines, ) else: print "tor already running, no need to start"
import sys from stem.util.connection import get_connections, system_resolvers from stem.util.system import pid_by_name resolvers = system_resolvers() if not resolvers: print("Stem doesn't support any connection resolvers on our platform.") sys.exit(1) picked_resolver = resolvers[0] # lets just opt for the first print("Our platform supports connection resolution via: %s (picked %s)" % (', '.join(resolvers), picked_resolver)) tor_pids = pid_by_name('tor', multiple = True) if not tor_pids: print("Unable to get tor's pid. Is it running?") sys.exit(1) elif len(tor_pids) > 1: print("You're running %i instances of tor, picking the one with pid %i" % (len(tor_pids), tor_pids[0])) else: print("Tor is running with pid %i" % tor_pids[0]) print("\nConnections:\n") for conn in get_connections(picked_resolver, process_pid = tor_pids[0], process_name = 'tor'): print(" %s:%s => %s:%s" % (conn.local_address, conn.local_port, conn.remote_address, conn.remote_port))
import sys from stem.util.connection import get_connections, system_resolvers from stem.util.system import pid_by_name resolvers = system_resolvers() if not resolvers: print("Stem doesn't support any connection resolvers on our platform.") sys.exit(1) picked_resolver = resolvers[0] # lets just opt for the first print('Our platform supports connection resolution via: %s (picked %s)' % (', '.join(resolvers), picked_resolver)) tor_pids = pid_by_name('tor', multiple=True) if not tor_pids: print("Unable to get tor's pid. Is it running?") sys.exit(1) elif len(tor_pids) > 1: print("You're running %i instances of tor, picking the one with pid %i" % (len(tor_pids), tor_pids[0])) else: print('Tor is running with pid %i' % tor_pids[0]) print('\nConnections:\n') for conn in get_connections(picked_resolver, process_pid=tor_pids[0], process_name='tor'):
def stop_process_on_name(): process = system.pid_by_name('tor') if process is not None: os.kill(process, 2)