21 from subprocess
import Popen, PIPE
25 from diagnostic_msgs.msg
import DiagnosticArray, DiagnosticStatus, KeyValue
31 for information
in re.split(
" |\n",info):
33 content = re.split(
":|=", information)
36 content[0] = content[0].decode()
37 content[1] = content[1].decode()
38 except (UnicodeDecodeError, AttributeError):
40 values.append(KeyValue(content[0].lstrip(), content[1].rstrip()))
45 IwConfigParser.__init__(self)
48 self.
stat = DiagnosticStatus()
49 self.stat.level = DiagnosticStatus.OK
50 self.stat.message =
"OK" 53 p = Popen(
"iw dev | awk '$1==\"Interface\"{print $2}'", stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=
True)
55 (stdout,stderr) = p.communicate()
57 stdout = stdout.decode()
58 except (UnicodeDecodeError, AttributeError):
60 self.
interfaces = sorted(os.linesep.join([s
for s
in stdout.splitlines()
if s]).split(
'\n'))
61 except Exception
as e:
62 rospy.logerr(
"IwConfigLocal init exception: %s" %e)
65 self.stat.level = DiagnosticStatus.OK
66 self.stat.message =
"OK" 69 self.stat.values.append(KeyValue(key = str(interface), value =
"======================="))
71 p = Popen([
"iwconfig", interface], stdout=PIPE, stdin=PIPE, stderr=PIPE)
73 (stdout,stderr) = p.communicate()
75 stdout = stdout.decode()
76 except (UnicodeDecodeError, AttributeError):
80 self.stat.values.append(KeyValue(key =
'iwconfig stderr', value = stderr))
81 self.stat.values.append(KeyValue(key =
'iwconfig stdout', value = stdout))
84 except Exception
as e:
85 rospy.logerr(
"IwConfigLocal update exception: %s" %e)
86 self.stat.values.append(KeyValue(key =
'update exception', value = str(e)))
90 IwConfigParser.__init__(self)
91 self.
ssh = paramiko.SSHClient()
92 self.ssh.load_system_host_keys()
93 ssh_key_file = os.getenv(
"HOME")+
'/.ssh/id_rsa.pub' 94 self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
95 self.ssh.connect(str(hostname), username=user, key_filename=ssh_key_file)
99 self.
stat = DiagnosticStatus()
100 self.stat.level = DiagnosticStatus.OK
101 self.stat.message =
"OK" 102 self.stat.values = []
104 (stdin, stdout, stderr) = self.ssh.exec_command(
"iw dev | awk '$1==\"Interface\"{print $2}'")
105 output =
''.join(stdout.readlines())
106 self.
interfaces = sorted(os.linesep.join([s
for s
in output.splitlines()
if s]).split(
'\n'))
107 except Exception
as e:
108 rospy.logerr(
"IwConfigSSH init exception: %s" %e)
111 self.stat.level = DiagnosticStatus.OK
112 self.stat.message =
"OK" 113 self.stat.values = []
115 self.stat.values.append(KeyValue(key = str(interface), value =
"======================="))
117 (stdin, stdout, stderr) = self.ssh.exec_command(
"iwconfig %s"%interface)
119 output =
''.join(stdout.readlines())
121 except Exception
as e:
122 rospy.logerr(
"IwConfigSSH update exception: %s" %e)
123 self.stat.values.append(KeyValue(key =
'update exception', value = str(e)))
127 rospy.init_node(
"wlan_monitor")
133 self._wlan_stat.level = DiagnosticStatus.OK
134 self._wlan_stat.message =
'No Data' 135 self._wlan_stat.values = []
136 self.
msg = DiagnosticArray()
137 self.msg.header.stamp = rospy.get_rostime()
140 self.
diag_pub = rospy.Publisher(
"/diagnostics", DiagnosticArray, queue_size=1)
148 except Exception
as e:
149 msg =
"Cannot connect to router via ssh. Please check if ssh key of user '{}' is contained in the router configuration or password is provided. Error message: {}".format(getpass.getuser(), e)
151 self._wlan_stat.level = DiagnosticStatus.ERROR
152 self._wlan_stat.message = msg
153 self._wlan_stat.values = [ KeyValue(key =
'Exception', value = str(e)) ]
160 self.iwconfig.update()
162 self.
msg = DiagnosticArray()
163 self.msg.header.stamp = rospy.get_rostime()
164 self._wlan_stat.level = self.iwconfig.stat.level
165 self._wlan_stat.message = self.iwconfig.stat.message
166 self._wlan_stat.values = self.iwconfig.stat.values
170 self.diag_pub.publish(self.
msg)
175 self.
user = rospy.get_param(
'~user',
"")
178 if __name__ ==
"__main__":
def update_diagnostics(self, event)
def _parse_info(self, info)
def publish_diagnostics(self, event)
def __init__(self, hostname, user, password)