torproject / stem

Python controller library for Tor
https://stem.torproject.org/
GNU Lesser General Public License v3.0
260 stars 75 forks source link

ss command is no longer supported by relay_connections.py example #46

Closed atagar closed 3 years ago

atagar commented 4 years ago

Migrated from ticket 31551.

I do get :

# for p in 9051 29051 ; do python /usr/share/doc/stem-1.7.1/_static/example/relay_connections.py --ctrlport $p --resolver ss; done
 0.4.1.5   uptime: 3-01:35:39   flags: Fast, Running, Stable, V2Dir, Valid

+------------------------------+------+------+
| Type                         | IPv4 | IPv6 |
+------------------------------+------+------+
| Inbound to our ORPort        |    0 |    1 |
+------------------------------+------+------+
| Total                        |    0 |    1 |
+------------------------------+------+------+

 0.4.1.5   uptime: 3-01:36:37   flags: Fast, Running, Stable, V2Dir, Valid

Traceback (most recent call last):
  File "/usr/share/doc/stem-1.7.1/_static/example/relay_connections.py", line 130, in <module>
    main()
  File "/usr/share/doc/stem-1.7.1/_static/example/relay_connections.py", line 66, in main
    for conn in get_connections(resolver = args.resolver, process_pid = pid):
  File "/usr/lib64/python3.6/site-packages/stem/util/connection.py", line 300, in get_connections
    raise IOError('No results found using: %s' % resolver_command)
OSError: No results found using: ss -nptu

at a hardened stable Gentoo Linux with kernel 5.2.10 and sys-apps/iproute2-4.19.0-r1 (provides "ss"), but th ecomamdn itself prints :

mr-fox ~ # ss -nptu | head
Netid   State         Recv-Q    Send-Q                   Local Address:Port                                             Peer Address:Port
tcp     ESTAB         0         0                           5.9.158.75:39658                                            51.77.52.216:443                         users:(("tor",pid=2365,fd=4313))
tcp     ESTAB         0         0                           5.9.158.75:9001                                            52.14.166.220:51466                       users:(("tor",pid=2397,fd=5105))
tcp     ESTAB         0         1057                        5.9.158.75:46379                                         158.174.255.235:4430                        users:(("tor",pid=2365,fd=4663))
tcp     ESTAB         0         0                           5.9.158.75:443                                             82.35.159.254:57674                       users:(("tor",pid=2365,fd=3870))
tcp     ESTAB         0         0                           5.9.158.75:35005                                            94.16.113.67:9001                        users:(("tor",pid=2365,fd=4975))
tcp     ESTAB         0         0                           5.9.158.75:36837                                            88.3.139.225:48998                       users:(("tor",pid=2365,fd=6473))
tcp     ESTAB         0         0                           5.9.158.75:44183                                            82.118.21.60:9001                        users:(("tor",pid=2365,fd=720))
tcp     ESTAB         0         0                           5.9.158.75:36997                                             50.7.115.67:443                         users:(("tor",pid=2397,fd=1543))
tcp     ESTAB         1571      1057                        5.9.158.75:9001                                           46.165.250.224:34639                       users:(("tor",pid=2397,fd=2352))

All other 3 resolvers are fine and do print something like

# for p in 9051 29051 ; do python /usr/share/doc/stem-1.7.1/_static/example/relay_connections.py --ctrlport $p; done
 0.4.1.5   uptime: 3-01:51:38   flags: Fast, Running, Stable, V2Dir, Valid

+------------------------------+------+------+
| Type                         | IPv4 | IPv6 |
+------------------------------+------+------+
| Inbound to our ORPort        | 2267 |    1 |
| Inbound to our DirPort       |    3 |    0 |
| Inbound to our ControlPort   |    1 |    0 |
| Outbound to a relay          | 3965 |    0 |
| Outbound uncategorized       |   12 |    0 |
+------------------------------+------+------+
| Total                        | 6248 |    1 |
+------------------------------+------+------+

 0.4.1.5   uptime: 3-01:51:40   flags: Fast, Running, Stable, V2Dir, Valid

+------------------------------+------+------+
| Type                         | IPv4 | IPv6 |
+------------------------------+------+------+
| Inbound to our ORPort        | 1629 |    2 |
| Inbound to our ControlPort   |    1 |    0 |
| Outbound to a relay          | 4083 |    0 |
| Outbound uncategorized       |   12 |    0 |
+------------------------------+------+------+
| Total                        | 5725 |    2 |
+------------------------------+------+------+
atagar commented 3 years ago

Hi toralf, sorry this went a full year without a reply. I just traced stem with the ss output you cite above and it seems to work.

That output lists four different tor pids, indicating multiple tor processes. relay_connections.py narrows its results to the control port's pid, so I suspect the issue here is that it isn't among them.

Feel free to reopen if you have any other questions.

toralf commented 3 years ago

Similar problem here at a system with 2 relays at the same hardware and the same ip address:

mr-fox ~ # python /usr/share/doc/stem-1.8.0/_static/example/relay_connections.py  --ctrlport 9051 --resolver ss
 0.4.5.0-alpha-dev   uptime: 22:23:09   flags: Fast, Guard, Running, Stable, V2Dir, Valid

Traceback (most recent call last):
  File "/usr/share/doc/stem-1.8.0/_static/example/relay_connections.py", line 130, in <module>
    main()
  File "/usr/share/doc/stem-1.8.0/_static/example/relay_connections.py", line 66, in main
    for conn in get_connections(resolver = args.resolver, process_pid = pid):
  File "/usr/lib/python3.7/site-packages/stem/util/connection.py", line 348, in get_connections
    raise IOError('No results found using: %s' % resolver_command)
OSError: No results found using: ss -nptu
mr-fox ~ # python /usr/share/doc/stem-1.8.0/_static/example/relay_connections.py  --ctrlport 9051 --resolver lsof
 0.4.5.0-alpha-dev   uptime: 22:23:28   flags: Fast, Guard, Running, Stable, V2Dir, Valid

+------------------------------+------+------+
| Type                         | IPv4 | IPv6 |
+------------------------------+------+------+
| Inbound to our ControlPort   |    1 |    0 |
| Outbound to a relay          | 3709 |    0 |
| Outbound exit traffic        |   60 |    5 |
| Outbound uncategorized       | 4509 |   11 |
+------------------------------+------+------+
| Total                        | 8279 |   16 |
+------------------------------+------+------+

+------------------------------+------+------+
| Exit Port                    | IPv4 | IPv6 |
+------------------------------+------+------+
| 853                          |    4 |    0 |
| 5222 (Jabber)                |   42 |    1 |
| 5223 (Jabber)                |   13 |    1 |
| 6667 (IRC)                   |    0 |    2 |
| 6697 (IRC)                   |    0 |    1 |
| 7777                         |    1 |    0 |
+------------------------------+------+------+
| Total                        |   60 |    5 |
+------------------------------+------+------+

mr-fox ~ # python /usr/share/doc/stem-1.8.0/_static/example/relay_connections.py  --ctrlport 29051 --resolver lsof
 0.4.5.0-alpha-dev   uptime: 22:23:50   flags: Fast, Guard, Running, Stable, V2Dir, Valid

+------------------------------+------+------+
| Type                         | IPv4 | IPv6 |
+------------------------------+------+------+
| Inbound to our ControlPort   |    1 |    0 |
| Outbound to a relay          | 3947 |    0 |
| Outbound exit traffic        |   54 |    4 |
| Outbound uncategorized       | 4216 |    8 |
+------------------------------+------+------+
| Total                        | 8218 |   12 |
+------------------------------+------+------+

+------------------------------+------+------+
| Exit Port                    | IPv4 | IPv6 |
+------------------------------+------+------+
| 853                          |    4 |    0 |
| 5222 (Jabber)                |   38 |    3 |
| 5223 (Jabber)                |   10 |    0 |
| 6667 (IRC)                   |    2 |    1 |
+------------------------------+------+------+
| Total                        |   54 |    4 |
+------------------------------+------+------+

The first relay runs at pid 2238 (ok), the 2nd at 2269 (wrong). I will attach outputs of

mr-fox ~ #  ss -nptu -6 > ss.6
mr-fox ~ #  ss -nptu -4 > ss.4

soon.

toralf commented 3 years ago

ss.4.txt ss.6.txt

toralf commented 3 years ago
mr-fox ~ # pgrep -af /usr/bin/tor
2238 /usr/bin/tor -f /etc/tor/torrc --hush --runasdaemon 1 --pidfile /run/tor/tor.pid
2269 /usr/bin/tor -f /etc/tor/torrc2 --hush --runasdaemon 1 --pidfile /run/tor/tor2.pid

mr-fox ~ # python -V
Python 3.7.8
atagar commented 3 years ago

Thanks toralf. Two questions...

  1. What is the output of 'ss -nptu' without the '-4' or '-6' arguments that you included?
  2. Which tor pid belongs to control port 9051(ie, the one that errors)?
toralf commented 3 years ago

Thanks toralf. Two questions...

1. What is the output of 'ss -nptu' without the '-4' or '-6' arguments that you included?

Out put of

ss -nptu &> ss-nptu.log

and

for r in proc lsof netstat ss; do for p in 9051 29051; do echo "resolver=$r ctrlpor=$p"; python /usr/share/doc/stem-1.8.0/_static/example/relay_connections.py  --ctrlport $p --resolver $r; done; done 2>&1 | tee rc.log

will be attached soon.

  1. Which tor pid belongs to control port 9051(ie, the one that errors)? pid 2238 belongs to 9051 and pid 2269 belongs to 29051.
toralf commented 3 years ago

rc.log ss-nptu.log

atagar commented 3 years ago

Ah! Finally figured out what's up. Your copy of 'ss' appends trailing whitespace that I don't expect. Fixed and added a test...

https://gitweb.torproject.org/stem.git/commit/?id=8a00dcb

Thanks for pointing this out!