专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

一个使su能forward X的脚本

百度收藏 QQ搜藏

作者:yongjian
虽然现在多用sudo来执行root权限,但有时在某些情况下还是会用到su, 特别是在远程没有sudo配置的环境。这样,在转换到另外一个用户后(多数是root), X authentication的信息就丢失了。自己写了一个脚本可以使到su到另外一个用户后仍能保持X权限, 运行X apps.


代码:
#!/bin/sh
#
# -------------------------------------------------------------------------
# Script Name: sussh
# Author: yongjian Xu - i3dmaster
# Updated: 04/25/2005
#
# Description:
# Allows one to su to another user within a SSH session and still be
# able to launch X11 apps.
# -------------------------------------------------------------------------

# Check to see if a user id was supplied on the command line
case $# in
        1) uid="$1" ;;
        *) echo "Usage: $0 <username>"
        exit 1 ;;
esac

# Check if $DISPLAY is set
if [[ $DISPLAY = "" ]]; then
        echo "The DISPLAY environment variable must be defined."
        echo "You may need to turn on X11 forwarding in your SSH client."
        exit 1
fi

if [[ $(echo $DISPLAY | grep "localhost") != "" ]]; then
        DISPORT=$(echo $DISPLAY | awk -F: '{print $2}'| awk -F. '{print $1}')
        NEWDISPORT="$(uname -n)/unix:$DISPORT"
else
        NEWDISPORT=$DISPLAY
fi

XAUTH="/usr/bin/X11/xauth"        # Set the location of xauth
ARG=$($XAUTH list $NEWDISPORT)   # Get the X11 magic cookie

su - $uid -c 
"$XAUTH add $ARG; \
 DISPLAY=$DISPLAY; \
 SSH_TTY=$SSH_TTY; \
 SSH_CLIENT='$SSH_CLIENT'; \
 export DISPLAY SSH_TTY SSH_CLIENT; \
 $SHELL"

exit 0 

上一篇:自动ftp脚本,可以用来更新named.root文件 下一篇:百度MP3下载工具

power by soyo123 2007-2008