在Gnu/Linux中使用超级用户执行程序的方法

/*********************************************************************
 * Author  : Samson
 * Date    : 06/30/2014
 * Test platform:
 *              Mint 15
 *              GNU bash, version 4.2.45
 * *******************************************************************/

很多时候,在写了一个UI后,需要在执行的时候以超级用户的级别进行运行时,那么这就要在程序进行启动的时候进行使用超级用户级别进行运行程序,如有一个名称为systemtool的工具程序需要如些运行时,此工具的执行文件放在/usr/bin目录下,那么此文件的内容为:

/usr/bin/systemtool
#!/bin/sh
magic='--calling-python-from-/bin/sh--'
"""exec" python -E "$0" "$@" """#$magic"
import os
import sys
import commands
launcher = commands.getoutput("/usr/lib/linuxmint/common/mint-which-launcher.py")
command = "%s python /usr/share/systemtool/daemon/testpygtk.py" % (launcher)
os.system(command)


而/usr/lib/linuxmint/common/mint-which-launcher.py文件的内容又如下:

#!/usr/bin/python


import os
import gettext

gettext.install("mint-common", "/usr/share/linuxmint/locale")

if os.path.exists("/usr/bin/gksu"):
    launcher = "gksu  --message \"<b>" + _("Please enter your password") + "</b>\""
elif os.path.exists("/usr/bin/kdesudo"):
    launcher = "kdesudo -i /usr/share/linuxmint/logo.png -d --comment \"<b>" + _("Please enter your password") + "</b>\""

print launcher


command的值为:gksu  --message "<b>请输入您的密码</b>" python /usr/share/systemtool/daemon/testpygtk.py


而gksu命令是以su root的权限进行运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值