LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1197|回复: 10

关于验证root权限的问题

[复制链接]
发表于 2006-8-15 23:45:08 | 显示全部楼层 |阅读模式
我写了一个脚本,执行它需要有root权限,我想要的效果是一个普通用户双击这个脚本,然后弹出一个对话框让用户输入root密码,如果正确,则运行脚本,希望高手可以指点一下,我是把从对话框获得的密码存储在一个变量里,但是下面就不知道怎么做了。
发表于 2006-8-16 11:48:32 | 显示全部楼层
you might need something like this:
  1. [[ $USER != "root" ]] && exec gksu -u root ./test.sh
  2. echo "run as $USER"
复制代码
This little script itself is called test.sh. when invoked by non-root user, it calls gksu to ask for root pass and kill itself by creating another copy in the current shell. (meaning kill the one from non-root user and keep going with the one from the root user...)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-16 14:23:36 | 显示全部楼层
谢谢你的回复,你知道有的系统软件需要用户输入管理员密码方可使用,我就想要这种效果,我写了一个expect小脚本比如取名叫pass,如下:
#!/usr/bin/expect

set pass [lindex $argv 0]
spawn sudo -s
expect ":"
send "$pass\r"
expect eof
return

现在我 有一个脚本叫admin.sh,首先用zenity让用户输入密码,保存在$password变量里,然后调用pass脚本:pass $password,接下来就执行正常的操作,可是pass脚本没能让admin.sh 获得管理员权限,请问问题出在哪?
回复 支持 反对

使用道具 举报

发表于 2006-8-16 14:35:08 | 显示全部楼层
It did but just in the subshell, when expect return to the admin.sh parent shell, authority was gone. you way you did wouldn't work unless you can kill the current one and exec a new one from root. You should try using the way I showed you which is the default method for lots of admin programs.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-16 14:45:26 | 显示全部楼层
£是不是Å须要用gksuÙ个软ö包阿 ,ïåô的æÆ一点吗,我不太明白,kill the current one and exec a new one from root是在admin.sh里执行吗,能不能给一下代码,谢谢阿
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-16 14:54:06 | 显示全部楼层
我刚才Å了gksu软ö包,然后î改了desktop文öexec=gksu -u root /usr/bin/admin.sh,然后我¹û了ûßÜ单Ìø”的项的ö候,整个屏幕—了下来,并且ú°了一个英文提示框,让我输入密码,我输入后,就可以执行脚本了,可是我不太喜欢这种方法,第一,提示框界面是英文的,对用户不友好,第二,别的系统软件在提示输入管理员密码的时候,屏幕也没有暗下来呀,呵呵,我的要求是不是很过分,请多包含,其实我就想作一个和系统软件一样的效果,请指点
回复 支持 反对

使用道具 举报

发表于 2006-8-16 15:03:38 | 显示全部楼层
no problem. the GUI isn't that important. you can use zenity if you want. the most important is to create a new copy of itself instead of 'fork'. The way you did was just return to the normal user shell context. So you need "exec". You will need to play with it. Try 'exec pass $password' and in the pass expect program, call 'spawn ssh -s admin.sh'. This way, admin.sh will be called from root. Also, in order to bypass the pass call from admin.sh when root is running it, you need a wrapper check around it.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-16 15:27:40 | 显示全部楼层
#!/usr/bin/expect

set pass [lindex $argv 0]
spawn ssh -s /usr/bin/admin.sh
expect ":"
send "$pass\r"
expect eof
return



在adminÌÙ样执行?
pass1="`zenity --title "÷“入¡理员Æ码" --text "÷“入Æ码:" --entry --hide-text`"
exec pass $pass1

ï是不起作用阿
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-16 16:07:03 | 显示全部楼层
而且我执行ssh -s /usr/bin/admin.sh的ö候ú错,错ïá息如下:
You must specify a subsystem to invoke.
回复 支持 反对

使用道具 举报

发表于 2006-8-16 23:20:35 | 显示全部楼层
if its a local program, why use ssh? try 'su root -c cmd'
btw, your char encoding seems having some problems.. I saw a lots garbled chars...
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表