problem with example python script on macOS for leak prevention

Got a problem with Viscosity or need help? Ask here!

b7d33s

Posts: 2
Joined: Mon Mar 27, 2023 1:26 am

Post by b7d33s » Mon Mar 27, 2023 1:38 am
Hello!
I'm trying to run the recommended base script from the support article to turn off network interfaces on vpn disconnect. I'm stuck at a simple step where the python script itself does not run and returns an error on the line 3 find command . I believe I have followed all the previous steps correctly. my machine is a little unusual - it is an older unsupported Mac running Monterey using OpenCore-Legacy-Patcher, and I also have python installed via homebrew in addition to the base system package. I don't do much with python so appreciate any help!

Note #!/usr/bin/python was not found so I tried the following, both of which gave the same error below:
#!/usr/bin/python3
#!/usr/local/bin/python3

output from manually running the script:
Code: Select all
Traceback (most recent call last):
  File "/Library/Application Support/ViscosityScripts/disablenetwork.py", line 3, in <module>
    services = re.findall("\(\d+?\) (.+?)\n\(Hardware Port: (.+?), Device: (.+?)\)\n",
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/[email protected]/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/re/__init__.py", line 216, in findall
    return _compile(pattern, flags).findall(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot use a string pattern on a bytes-like object

James

User avatar
Posts: 2313
Joined: Thu Sep 04, 2008 9:27 pm

Post by James » Mon Mar 27, 2023 4:44 pm
Hi b7d33s,

The example script was written for Python 2.7. If you install Python 2.x with Homebrew (and make sure the script is point at the correct path) it'll run, or you can modify it for use under Python 3 (i.e. typically you'll need to .encode() or .decode() any bytes/strings).

Cheers,
James
Web: https://www.sparklabs.com
Support: https://www.sparklabs.com/support
Twitter: https://twitter.com/sparklabs

b7d33s

Posts: 2
Joined: Mon Mar 27, 2023 1:26 am

Post by b7d33s » Sat Apr 01, 2023 6:34 am
If this is helpful to anyone else, I poked around on the web and guided by some examples added ".decode('utf-8')" to the end of line 4. Im not a python programmer and just guessed at the encoding so this may not be the right way or may cause issues... but so far it seems to work on Monterey 12.6.3 with python 3.11.2 in limited testing.
Code: Select all
#!/usr/local/bin/python3
import subprocess, re
services = re.findall("\(\d+?\) (.+?)\n\(Hardware Port: (.+?), Device: (.+?)\)\n",
  subprocess.check_output(["/usr/sbin/networksetup", "-listnetworkserviceorder"]).decode('utf-8'))
for service in services:
  if service[1] == "Wi-Fi":
    subprocess.check_output(["/usr/sbin/networksetup", "-setairportpower", service[2], "off"])
  else:
    subprocess.check_output(["/usr/sbin/networksetup", "-setnetworkserviceenabled", service[0], "off"])

Also, I didn't see this in the instructions, but the script wasn't running at first and from the log found that "script-security 2" also needed to be added in the connection advanced settings. after that it seems to work.

James

User avatar
Posts: 2313
Joined: Thu Sep 04, 2008 9:27 pm

Post by James » Thu Apr 06, 2023 7:47 am
Thanks for posting a follow-up with your fixes b7d33s - much appreciated.

Cheers,
James
Web: https://www.sparklabs.com
Support: https://www.sparklabs.com/support
Twitter: https://twitter.com/sparklabs
4 posts Page 1 of 1