python: get list of running roscores URIs
In python is there a way to get a list of URIs of the roscores that are running in a machine ?
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
In python is there a way to get a list of URIs of the roscores that are running in a machine ?
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:
rosmaster
I also don't know whether this will see roscore
s 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":
-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
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)
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
Asked: 2020-07-02 05:23:27 -0500
Seen: 351 times
Last updated: Jul 02 '20
How to display only selected topics in rqt topic monitor
alwaysOn camera parameter Gazebo
error while launching lslidar_c16
Lowest end computer that will run ROS and all its tools
Float32MultiArray in pub/sub example
ServiceServer declaration within a class
Edit encoding of sensor_msgs/Image message
How to make ros3djs respect the frame_locked marker message parameter?