Issue in parsing the string output from the command using "subprocess"
MRAB
python at mrabarnett.plus.com
Sun Nov 18 14:15:26 EST 2018
On 2018-11-18 14:59, srinivasan wrote:
> Dear Python Experts Team,
>
> As am newbie to python and learning python, working on embedded linux
> platform, my intention is to delete all the SSID's before connecting my
> Wi-Fi module to specific SSID., I am trying to parse command output using
> the "subprocess" with wrapper "execute_cmd_output_string" written on
> it described
> as below, using the nmcli commands "*nmcli -t -f TYPE,UUID con" and "**"nmcli
> connection delete uuid ".*
>
> Could you please help me, what could be the bug in the below method "*def
> wifi_disconnect_reconnect(self, ssid, pw):" *using the method
> "execute_cmd_output_string" (in-turn uses "subprocess") which is failing to
> give correct output of., UUID's for *"nmcli connection delete uuid "* ?
>
> But which works fine with "commands.getstatusoutput" (res =
> commands.getstatusoutput("nmcli -t -f TYPE,UUID con")), but I dont want to
> include "commands.getstatusoutput" which costs me for including one more
> python module in my rootfs
>
> Typical output for "nmcli -t -f TYPE,UUID con"
>
> ~$ nmcli -t -f TYPE,UUID con
> 802-11-wireless:3f5011d4-5681-4fed-8dea-95dee790e9e2
> 802-11-wireless:bfb93be0-740d-426e-b215-0fdc2f652877
> 802-11-wireless:69b60cf4-65aa-442b-a54c-fb82905adb0d
> 802-11-wireless:dc2a15ec-d3da-491e-9c8f-cb054f375837
> 802-11-wireless:5164e7f2-4489-462e-b093-76bc51bf1303
> 802-11-wireless:d462e8c4-fac7-42f2-8f9a-6846f52d4e8c
> 802-11-wireless:e5020744-5c9c-453c-92ec-7a854fc893e6
> 802-11-wireless:eed3358e-8635-471d-b7e9-5c2973a05128
> 802-11-wireless:ceea6707-a929-4941-9047-a75e061914b6
> 802-11-wireless:dd6b9c83-db7b-42b9-99c0-14a04f6f35f5
> 802-11-wireless:9f764fff-6288-49c4-9412-902e89230136
> 802-11-wireless:72c627cd-77a3-4d16-bb2c-058040d8e4fc
> ~$
>
> *Python Code Snipeet:*
> *------------------------------*
> * def wifi_disconnect_reconnect(self, ssid, pw):*
> * """*
> * Connect to Access point using SSID and PW.*
>
> * :param ssid: SSID of the ACCESS POINT.*
> * :param pw: password for connecting to the access point.*
> * :return: command output as True or False.*
> * """*
>
> * cmd = "nmcli -t -f TYPE,UUID con"*
> * res = self._helper.execute_cmd_output_string(cmd)*
> * print(res)*
> * lines = res[1].split('\n') ------------------------> I suspect the
> issue might be here*
> * print(lines)*
>
> * for line in lines:*
> * parts = line.split(":")*
> * print(parts)*
> * print(parts[1])*
> * if (parts[0] == "802-11-wireless"):*
> * print("nmcli connection delete uuid "+ parts[1])*
> * os.system("nmcli connection delete uuid "+ parts[1])*
>
> * cmd = "nmcli device wifi connect '%s' password %s" % (ssid, pw)*
> * exit_code = self._helper.execute_cmd_return_code(cmd)*
>
> * return True if exit_code == 0 else False*
>
>
> def execute_cmd_output_string(self, cmd, enable_shell=False):
> """
> Execute a command and return its output as a string.
>
> :param cmd: abs path of the command with arguments
> :param enable_shell : force the cmd to be run as shell script
> :return: a string.
> """
>
> try:
> result = subprocess.check_output(split(cmd),
> stderr=subprocess.STDOUT,
> shell=enable_shell)
>
> except subprocess.CalledProcessError as e:
> s = """While executing '{}' something went wrong.
> Return code == '{}'
> Return output:\n'{}'
> """.format(cmd, e.returncode, e.output, shell=enable_shell)
> raise AssertionError(s)
>
> return result.strip().decode("utf-8")
>
>
> if __name__ == "__main__":
> m = wifi()
> print("disconnect and reconnect")
> print(m.wifi_disconnect_reconnect("NaWiFi", "abcds"))
>
>
> *Errors:*
> -----------
> Traceback (most recent call last):
> 802-11-wireless:3f5011d4-5681-4fed-8dea-95dee790e9e2
> 802-11-wireless:dc2a15ec-d3da-491e-9c8f-cb054f375837
> 802-11-wireless:5164e7f2-4489-462e-b093-76bc51bf1303
> File
> "/home/srinivasan/Downloads/qa_wifi_nov15_after_incorporating_thilo_comments_zip/qa/test_library/wifi.py",
> line 153, in <module>
> 802-11-wireless:d462e8c4-fac7-42f2-8f9a-6846f52d4e8c
> print(m.wifi_connect("NI WiFi", "T.f.o.s.1996!"))
> 802-11-wireless:e5020744-5c9c-453c-92ec-7a854fc893e6
> 802-11-wireless:eed3358e-8635-471d-b7e9-5c2973a05128
> File
> "/home/srinivasan/Downloads/qa_wifi_nov15_after_incorporating_thilo_comments_zip/qa/test_library/wifi.py",
> line 77, in wifi_connect
> 802-11-wireless:ceea6707-a929-4941-9047-a75e061914b6
> print(parts[1])
> 802-11-wireless:dd6b9c83-db7b-42b9-99c0-14a04f6f35f5
> *IndexError: list index out of range*
> 802-11-wireless:9f764fff-6288-49c4-9412-902e89230136
> 802-11-wireless:72c627cd-77a3-4d16-bb2c-058040d8e4fc
> [u'0']
> [u'0']
>
[snip]
execute_cmd_output_string returns a string, so 'res' will be a string.
For example, res == u'802-11-wireless:3f5011d4-5681-4fed-8dea-95dee790e9e2'.
That means that res[1] == u'0'.
So res[1].split('\n') == [u'0'].
If you follow the code from there, so you'll see that it'll eventually
raise IndexError.
More information about the Python-list
mailing list