[英]Allowing python mac app to run sudo script
我正在制作一個小型應用程序,它允許我通過狀態欄切換按鈕來連接到vpn並運行小型路由外殼腳本。 為了構建此應用程序,我使用了一個名為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)
並在外殼中運行生成的應用程序,如下所示:
./dist/MyVPN.app/Contents/MacOS/MyVPN
解決方案是使用以下命令生成應用程序:
> sudo python setup.py py2app --resources web_routes.sh
通過這種方式,可以將Shell腳本與應用程序捆綁在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.