允许python mac应用程序运行sudo脚本

允许python mac应用程序运行sudo脚本,python,macos,shell,applescript,py2app,Python,Macos,Shell,Applescript,Py2app,我正在制作一个小应用程序,它允许我通过状态栏切换连接到vpn并运行一个小的路由shell脚本。为了构建这个应用程序,我使用了一个名为rumps和py2app的库来生成mac应用程序 我制作了以下python脚本来处理vpn+shell脚本的启动: # -*- coding: utf-8 -*- import rumps from subprocess import call class MyVPNStatusBarApp(rumps.App): @rumps.clicked("My

我正在制作一个小应用程序,它允许我通过状态栏切换连接到vpn并运行一个小的路由shell脚本。为了构建这个应用程序,我使用了一个名为rumps和py2app的库来生成mac应用程序

我制作了以下python脚本来处理vpn+shell脚本的启动:

# -*- coding: utf-8 -*-

import rumps
from subprocess import call

class MyVPNStatusBarApp(rumps.App):

    @rumps.clicked("MyVPN ON")
    def vpn_on(self, _):
        script_on = False
        try_number = 0
        call("scutil --nc start \"MyVPN\"", shell=True)
        while script_on == False:
            if call("scutil --nc show \"MyVPN\" | grep  -q \"Connected\"", shell=True) == 0:
                call("/usr/bin/osascript -e \'do shell script \"./web_routes.sh args 2>&1 etc\" with administrator privileges\'", shell=True)
                rumps.notification(
                    "VPN Status", "VPN + Internet Access", 'Granted')
                script_on = True
            elif try_number == 20:
                print 'TIME OUT'
                return     
            else:
                time.sleep(0.1)
                try_number += 1 

    @rumps.clicked("MyVPN OFF")
    def vpn_off(self, _):
        call("scutil --nc stop \"MyVPN\"", shell=True)
        rumps.notification(
            "VPN Status", "VPN OFF", 'Internet should work')


if __name__ == "__main__":
    MyVPNStatusBarApp("VPN").run()
我的py2app安装文件如下所示:

from setuptools import setup

APP = ['main.py']
DATA_FILES = []
OPTIONS = {
    'argv_emulation': True,
    'plist': {
        'LSUIElement': True,
    },
    'packages': ['rumps'],
}

setup(
    app=APP,
    name='MyVPN',
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)
问题是,当我在shell中运行main.py时,应用程序能够运行shell脚本。但是,当我制作捆绑应用程序时,该应用程序似乎无法运行shell脚本,即使在询问我管理员密码后也是如此


有人知道可能是什么问题吗?

所以问题是shell脚本没有与生成的应用捆绑在一起。我通过在导入之后添加:

rumps.debug_mode(True)
在shell中运行生成的应用程序,如下所示:

./dist/MyVPN.app/Contents/MacOS/MyVPN
解决方案是使用以下命令生成应用程序:

> sudo python setup.py py2app --resources web_routes.sh

这样就可以将shell脚本与应用程序捆绑在一起。

您是否尝试将
sudo
添加到从脚本调用的命令中?还可以尝试以root用户身份运行捆绑应用程序<代码>sudo open-a