0

I have a python app in Gnu/Linux that change mac address in GUI. So I have to run pkexec command. I tried a lots of method with subprocess command with no luck. Can I anyone help to solve this problem? Now I run the app with 'sudo python3 app.py'.

wireless = config.get('wifi', 'wireless') <- This line gets from some file the name of wireless connection (wlx784476aae097)

subprocess.run(['pkexec', 'sh', '-c', 'ifconfig', wireless, 'down'])
subprocess.run(['pkexec', 'sh', '-c', 'ifconfig', wireless, 'up'])

Below 2 lines gets specifc mac address that type the user in GUI window that pop up then put it in variable with name 'text'.I didn't upload code for that.

text = self.lineEdit.text()
self.master.label_4.setText(text)

subprocess.run(['pkexec', 'sh', '-c', 'macchanger', '-m', text, wireless])

I am sorry for my english, I am not native speaker. All code is big, if above lines don't help I am able to upload all files with code. Thank you

1 Answers1

0

Have you tried pyroute2 module that uses Linux ip command?

from pyroute2 import IPRoute
ipr = IPRoute()
# lookup the index
dev = ipr.link_lookup(ifname=wireless)[0]
# change the interface MAC address 
try:
    ipr.link('set', index=dev, address=text)
except pyroute2.netlink.exceptions.NetlinkError:
    print('unable to set mac')
paulyang0125
  • 127
  • 6
  • It doesn't work. `ModuleNotFoundError: No module named 'pyroute2' ` – user19127009 May 16 '22 at 19:46
  • you haven't installed the module yet - do $ pip install pyroute2 in your command line first – paulyang0125 May 17 '22 at 00:38
  • I have installed then I have the same error mesage. Do you know how to do that with `subprocess.run` ? – user19127009 May 17 '22 at 08:05
  • for pyroute2 import error, I guess you may have multiple Python interpreters installed (and by extension, multiple pip executables.) Depending on your shell’s PATH, running pip may invoke the pip executable linked to the version of Python you’re using, or to a different one. If the wrong pip is invoked, then the packages it installs will likely not be visible to the Python interpreter you’re using, causing the ImportError. – paulyang0125 May 17 '22 at 08:53
  • I can't do experiments for MAC change using my laptop (my company IT will kill me) but you feel free to try - `subprocess.run(['pkexec','sh','-c','ip','link','set','dev',wireless,'address',text], capture_output=True)` or `subprocess.run(['pkexec','sh','-c','ifconfig',wireless,'hw','ether',text], capture_output=True)` – paulyang0125 May 17 '22 at 08:56
  • Both of 2 examples with subprocess.run didn't work! There is no error message. – user19127009 May 17 '22 at 18:19
  • @user19127009 if so, have you tried to manully use Linux commands over Linux bash terminal, such as `ip`, `ifconfig` to update your network interface with your custom MAC in your system under test to verify whether MAC address change is doable in your OS system. Examples: `sudo ifconfig eth0 hw ether 44:ee:bc:6c:76:ba' or 'sudo ip link set dev eth0 address 44:ee:bc:6c:76:ba'. if they don't work, subprocess over Python will not work too. but if they work, you can revisit where goes wrong in subprocees. – paulyang0125 May 18 '22 at 05:39
  • It works over linux terminal. The problem is with subprocees. I am not able to solve myself that is the reason that post here! – user19127009 May 18 '22 at 09:02
  • you can add the check for returncode of your subprocess.run() to see if they return a non-zero exit status code. An example with check=True Option: `subprocess.run( [ 'cat', '/foo' ], check=True )`. Also, use PIPE to check outputs of stdout and stderr. an example: ` from subprocess import run, PIPE p = run( [ 'echo', 'hello' ], stdout=PIPE, stderr=PIPE ) print( 'stdout:', p.stdout.decode() ) print( 'stderr:', p.stderr.decode() ) ` – paulyang0125 May 18 '22 at 09:58
  • I run the program from gnome-terminal with sudo python3 ...." or inside pycharm.The results are the same. Nothing work.There are three commands.The two command ifconfig up or down that doesn't work but they doesn't send any message true or false. The third command with macchanger show exit status 255. What Can I do?? – user19127009 May 19 '22 at 08:26
  • In this silent error situation, you need to find clues to dig in further. For high-level debugging, focus on stderr and stdout using `subprocess.PIPE` and `subprocess.run` options like `check=True` to raise an exception on a Bad Exit Code. Otherwise, you will need to make use of Python Debugger to step in and breakpoints further. But before you use the pycharm debugger, you may need to understand some known issues like https://stackoverflow.com/questions/41397727/pycharm-ide-breakpoints-dont-work-in-subprocess-call before you start doing that. – paulyang0125 May 19 '22 at 09:40