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

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

# -*- 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)


并在外壳中运行生成的应用程序,如下所示:

./dist/MyVPN.app/Contents/MacOS/MyVPN


解决方案是使用以下命令生成应用程序:

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


通过这种方式,shell脚本可以与应用程序捆绑在一起。

关于python - 允许python mac应用运行sudo脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34316259/

10-13 07:27
查看更多