22 from subprocess
import Popen, PIPE
23 from packaging
import version
26 from diagnostic_msgs.msg
import DiagnosticArray, DiagnosticStatus, KeyValue
32 for information
in re.split(
" |\n",info):
34 content = re.split(
":|=", information)
37 content[0] = content[0].decode()
38 content[1] = content[1].decode()
39 except (UnicodeDecodeError, AttributeError):
41 values.append(KeyValue(content[0].lstrip(), content[1].rstrip()))
46 IwConfigParser.__init__(self)
49 self.
stat = DiagnosticStatus()
50 self.
stat.level = DiagnosticStatus.OK
51 self.
stat.message =
"OK"
54 p = Popen(
"iw dev | awk '$1==\"Interface\"{print $2}'", stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=
True)
56 (stdout,stderr) = p.communicate()
58 stdout = stdout.decode()
59 except (UnicodeDecodeError, AttributeError):
61 self.
interfaces = sorted(os.linesep.join([s
for s
in stdout.splitlines()
if s]).split(
'\n'))
62 except Exception
as e:
63 message =
"IwConfigLocal init exception: %s" % e
64 self.
stat.level = DiagnosticStatus.ERROR
65 self.
stat.message = message
66 self.
stat.values = [ KeyValue(key =
'Exception', value = str(e)), KeyValue(key =
'Traceback', value = str(traceback.format_exc())) ]
70 self.
stat.level = DiagnosticStatus.OK
71 self.
stat.message =
"OK"
74 self.
stat.values.append(KeyValue(key = str(interface), value =
"======================="))
76 p = Popen([
"iwconfig", interface], stdout=PIPE, stdin=PIPE, stderr=PIPE)
78 (stdout,stderr) = p.communicate()
80 stdout = stdout.decode()
81 except (UnicodeDecodeError, AttributeError):
85 self.
stat.values.append(KeyValue(key =
'iwconfig stderr', value = stderr))
86 self.
stat.values.append(KeyValue(key =
'iwconfig stdout', value = stdout))
89 except Exception
as e:
90 message =
"IwConfigLocal update exception: %s" % e
91 self.
stat.level = DiagnosticStatus.ERROR
92 self.
stat.message = message
93 self.
stat.values = [ KeyValue(key =
'Exception', value = str(e)), KeyValue(key =
'Traceback', value = str(traceback.format_exc())) ]
98 IwConfigParser.__init__(self)
103 self.
ssh = paramiko.SSHClient()
104 self.
ssh.load_system_host_keys()
106 self.
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
111 self.
stat.level = DiagnosticStatus.OK
112 self.
stat.message =
"OK"
113 self.
stat.values = []
115 (stdin, stdout, stderr) = self.
ssh.exec_command(
"iw dev | awk '$1==\"Interface\"{print $2}'")
116 output =
''.join(stdout.readlines())
117 self.
interfaces = sorted(os.linesep.join([s
for s
in output.splitlines()
if s]).split(
'\n'))
118 except Exception
as e:
119 message =
"IwConfigSSH init exception: %s" % e
120 self.
stat.level = DiagnosticStatus.ERROR
121 self.
stat.message = message
122 self.
stat.values = [ KeyValue(key =
'Exception', value = str(e)), KeyValue(key =
'Traceback', value = str(traceback.format_exc())) ]
123 rospy.logerr(message)
126 if version.parse(paramiko.__version__) < version.parse(
"2.11.0"):
137 disabled_algorithms={
"pubkeys": [
"rsa-sha2-256",
"rsa-sha2-512"]}
141 self.
stat.level = DiagnosticStatus.OK
142 self.
stat.message =
"OK"
143 self.
stat.values = []
146 if self.
ssh.get_transport()
is None or not self.
ssh.get_transport().is_active():
149 except Exception
as e:
150 message =
"IwConfigSSH connect exception: %s" % e
151 self.
stat.level = DiagnosticStatus.ERROR
152 self.
stat.message = message
153 self.
stat.values = [ KeyValue(key =
'Exception', value = str(e)), KeyValue(key =
'Traceback', value = str(traceback.format_exc())) ]
154 rospy.logerr(message)
158 self.
stat.values.append(KeyValue(key = str(interface), value =
"======================="))
160 (stdin, stdout, stderr) = self.
ssh.exec_command(
"iwconfig %s" % interface)
161 output =
''.join(stdout.readlines())
163 except Exception
as e:
164 message =
"IwConfigSSH update exception: %s" % e
165 self.
stat.level = DiagnosticStatus.ERROR
166 self.
stat.message = message
167 self.
stat.values = [ KeyValue(key =
'Exception', value = str(e)), KeyValue(key =
'Traceback', value = str(traceback.format_exc())) ]
168 rospy.logerr(message)
172 rospy.init_node(
"wlan_monitor")
181 self.
msg = DiagnosticArray()
182 self.
msg.header.stamp = rospy.get_rostime()
185 self.
diag_pub = rospy.Publisher(
"/diagnostics", DiagnosticArray, queue_size=1)
193 except Exception
as e:
194 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)
196 self.
_wlan_stat.level = DiagnosticStatus.ERROR
198 self.
_wlan_stat.values = [ KeyValue(key =
'Exception', value = str(e)), KeyValue(key =
'Traceback', value = str(traceback.format_exc())) ]
207 self.
msg = DiagnosticArray()
208 self.
msg.header.stamp = rospy.get_rostime()
220 self.
user = rospy.get_param(
'~user',
"")
223 if __name__ ==
"__main__":