osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Issue in using "subprocess.Popen" for parsing the command output


Dear Python Experts Team,

As am newbie still learning the python syntax from past 2 weeks, Excuse me,
If this might be silly question, As I am trying to execute shell command
(ie, nmcli) using "subprocess.Popen".

1. Am trying to improve the below code with "try" and "exception", could
you please help me how "try" and "exception" can be used on the below code
snippet. I hope in my code with try and exception, seems to be a bug.

2. As I am trying to execute shell commands using "subprocess.Popen", I am
trying to parse the strings output by "cmd = "nmcli device wifi connect
'%s' password '%s'" % (ssid, pw)" command as below, but it is throwing the
below error as shown in "Output error logs:"

 Could you please let me to fix the bug in the below code snippet, where I
need the collect the strings of the command output and later how to be
parsed after execution of the command for example, I need to parse the
string "Connection activation failed: " and compare it with the command
output, could you please help me how this can be achieved?

*Command:*
:~$ nmcli device wifi connect 'Apartment 18' password
'40672958689850014685abcdf'
Error: Connection activation failed: (7) Secrets were required, but not
provided.
:~$

*Code:*
*import sys*
*import subprocess*

*interface = "wlan0"*


*def main(ssid, pw):*

*    # cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)*
*    #*
*    # proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True,  universal_newlines=True)*
*    # stdout, stderr = proc.communicate()*
*    # retcode = proc.returncode*
*    #*
*    # print("printing stdout!!!!!!!!!!", stdout)*
*    # print("printing retcode!!!!!!!!!!", retcode)*

*    try:*
*        cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)*

*        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True, universal_newlines=True)*
*        stdout, stderr = proc.communicate()*
*        retcode = proc.returncode*

*        print("printing stdout!!!!!!!!!!", stdout)*
*        print("printing retcode!!!!!!!!!!", retcode)*

*    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 proc.strip().decode("utf-8")*

*main("Apartment 18", "40672958689850014685")*

*Output error logs:*

/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/venv/bin/python
/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py
Traceback (most recent call last):
  File
"/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py",
line 38, in <module>
printing stdout!!!!!!!!!!
printing retcode!!!!!!!!!! 0
    main("Apartment 18", "40672958689850014685")
  File
"/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py",
line 36, in main
    return proc.strip().decode("utf-8")
AttributeError: 'Popen' object has no attribute 'strip'

Process finished with exit code 1

Kindly do the needful as am stuck with this issue from 2 days

Many Thanks in advance,