ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

python: get list of running roscores URIs

asked 2020-07-02 05:23:27 -0500

azerila gravatar image

updated 2020-07-02 05:23:53 -0500

In python is there a way to get a list of URIs of the roscores that are running in a machine ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-07-02 05:37:23 -0500

gvdhoorn gravatar image

updated 2020-07-02 10:45:20 -0500

Edit 3: Ok, so making a bunch of assumptions and using psutil in Python we can put something like this together:

#!/usr/bin/env python
import psutil

masters = [p for p in psutil.process_iter() if 'rosmaster' in p.name()]
for m in masters:
    # assume there is only one connection per process which is LISTENing
    # TODO: make this more robust, do some checking, etc
    ip, port = [c.laddr for c in m.connections() if c.status == 'LISTEN'][0]
    print ("Master (pid: {}), URI: http://{}:{}".format(m.pid, ip, port))

Output on a machine running two masters (one on the standard port, the other on 11411):

Master (pid: 14735), URI: http://0.0.0.0:11311
Master (pid: 15018), URI: http://0.0.0.0:11411

The assumptions I'm making here:

  1. the process is actually called rosmaster
  2. it's listening on only a single port
  3. that port is the XML RPC port

I also don't know whether this will see roscores not started by your own $USER.

It sees them, but as expected, as $USER doesn't have the necessary rights, you get this:

Traceback (most recent call last):
File "list_rosmasters.py", line 7, in <module>
  listen_ports = [c.laddr for c in m.connections() if c.status == 'LISTEN']
File "/usr/lib/python2.7/dist-packages/psutil/__init__.py", line 1033, in connections
  return self._proc.connections(kind)
File "/usr/lib/python2.7/dist-packages/psutil/_pslinux.py", line 821, in wrapper
  raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=15557, name='rosmaster')

That would be the same limitation as running netstat without sudo.


Edit 2:

I'm looking for a workaround either with subprocess or linux commands in general that may give a list or roscore that are available and then to do some hardcoding for geting a list of the URIs but it's not clear how to find them. somehow there should be a way to find processes named roscore and get their information

On Linux you might be able to use a combination of pgrep roscore (which outputs the PIDs) and then netstat -antp and grep for the PID. Two "problems":

  1. with -p you will only see the PIDs/names of the processes your $USER started. To see all processes, you'd have to run netstat as root
  2. the PID pgrep roscore prints is not the PID which is associated with the XML-RPC port opened by roscore (ie: 11311)

You may need to do some additional work with ps or some other tool to find out the process group and filter on those PIDs.


Edit: what you could maybe look at is multimaster_fkie and the way it "discovers" running masters.


No, there is no such API.

is there a way to get a list of URIs of the roscores that are running in a machine ?

you could probably put together a bit of Python which could do this, but it would be custom code and probably come down to just trying to connect to a nr ... (more)

edit flag offensive delete link more

Comments

I'm looking for a workaround either with subprocess or linux commands in general that may give a list or roscore that are available and then to do some hardcoding for geting a list of the URIs but it's not clear how to find them. somehow there should be a way to find processes named roscore and get their information

azerila gravatar image azerila  ( 2020-07-02 05:48:02 -0500 )edit

so any hack would still solve my problem

azerila gravatar image azerila  ( 2020-07-02 05:50:36 -0500 )edit

it says psutil.AccessDenied (pid=1124, name='rosmaster') for m.connections()

azerila gravatar image azerila  ( 2020-07-02 10:38:20 -0500 )edit
1

Please read the last part of Edit 3.

gvdhoorn gravatar image gvdhoorn  ( 2020-07-02 10:39:04 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-07-02 05:23:27 -0500

Seen: 351 times

Last updated: Jul 02 '20