LinuxSir.cn,穿越时空的Linuxsir!

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

【求助】如何截取“ctrl-回车”,如何才能在关闭终端时同时结束脚本进程?怎样才能让脚

[复制链接]
发表于 2005-5-17 13:52:24 | 显示全部楼层 |阅读模式
目的在于让test用户ssh到服务器,不允许使用cmd,只能用脚本提供的dialog对话框进行操作。
对话框中用mysql进行身份验证,并对不同用户建立不同的可选命令列表以供使用。
通过禁用ctrl-c ctrl-d ctrl-\禁止用户退出脚本。

遇到以下问题:
1.当用“ctrl-回车”时可强行退出脚本,并导致终端异常。
(用本地终端和putty登陆时正常,用SecureCRT出现错误)                     

2.由于用户只能用关闭SecureCRT终端的方式退出,所以导致脚本的进程还在运行,该怎么处理啊?           



FC2 kernel  2.6.9-1.667

请大家帮忙参谋参谋~,
怎样才能捕捉ctrl-回车,怎样才能在用户关闭SecureCRT终端时同时结束脚本进程??
由于刚开始自学脚本,只考虑到完成功能,肯定有很多写的很可笑的地方,请见谅
脚本中写的不好的地方请各位高手指点,请大家不要讽刺我

已在/home/test/.bash_profile文件中写入/script/test.sh
[PHP]
#!/bin/sh
#/scripts/test.sh
# kill 所有dialog
pkill -9 dialog
#将ctrl-c ctrl-d ctrl-\设置为不使用字符
stty susp ^@#$
stty intr ^@$#
stty quit ^*#$
: ${DIALOG=dialog}
rm -f ./testfile*
rm -f ./temp*
loginpassn=1

function loginuser ()
{
#创建login dialog
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
$DIALOG --title "LOGIN" --clear \
        --inputbox "\n\n            Please Input UserName:\n" 16 51 2> $tempfile
retval=$?
case $retval in
  0)
    usr=`cat $tempfile`
    echo "USE test;" > ./temp
    echo "SELECT user FROM user WHERE user='"$usr"';"  >> ./temp
    mysql -h 10.0.0.1 -u test -p"test" < ./temp > ./testfile
    LINE=`cat ./testfile |wc -l`
    case $LINE in
      2)
#若返回纪录表示用户名存在,引导password dialog,其他任何情况均重新引导login dialog
        loginpass
        ;;
      *)
        #loginusern=$(( $loginusern + 1 ))
        #if [ $loginusern -le 3 ]
        #then
          loginuser
        #fi
        ;;
    esac
  ;;
  1)
    loginuser
#    echo "Cancel pressed."
    ;;
  255)
    if test -s $tempfile ; then
      cat $tempfile
    else
       loginuser
#      echo "ESC pressed."
    fi
    ;;
esac
}

function loginpass ()
{
: ${DIALOG=dialog}

tempfile1=`tempfile 2>/dev/null` || tempfile1=/tmp/test$$
trap "rm -f $tempfile1" 0 1 2 5 15

$DIALOG --title "You can try pass 3 times, Now is $loginpassn" --clear \
        --insecure \
        --passwordbox "\n\n           Your Login name is: $usr \n           Now Input Password:\n" 16 51 2> $tempfile1

retval1=$?

case $retval1 in
  0)
           pd=`cat $tempfile1`
            echo "USE test;" > ./temp1
            echo "SELECT passwd FROM user WHERE passwd=md5('"$pd"');"  >> ./temp1
            mysql -h 10.0.0.1 -u test -p"test" < ./temp1 > ./testfile1
            LINE=`cat ./testfile1 |wc -l`
            case $LINE in
              2)
#若输入密码与mysql中相同,则建立该用户的menulist
                menulist
                ;;
              *)
#允许三次输入密码错误
                loginpassn=$(( $loginpassn + 1 ))
                if [ $loginpassn -le 3 ]
                then
                  loginpass
                fi
                ;;
            esac

   ;;
  1)
     loginuser
#    echo "Cancel pressed."
     ;;
  255)
    if test -s $tempfile1 ; then
      cat $tempfile1
    else
      loginuser
#      echo "ESC pressed."
    fi
    ;;
  esac
}

function menulist ()
{
#呵呵,这部分写的太乱了,只是根据用户创建其能使用的命令项
echo "USE test;" > ./temp
    echo "SELECT * FROM userscmd WHERE user='"$usr"' ;"  >> ./temp
    mysql -h 10.0.0.1 -u test -p"test" < ./temp > ./testfile2
sed '1d' ./testfile2 | sed 's/\t/\n/g' | sed '1d' | sed '/^$/d' > ./testfile2-1

LINETEXTFILE=`cat ./testfile2-1 |wc -l`
ii=1
rm -f ./aatest
touch ./aatest
while [ $LINETEXTFILE -gt 0 ]
do
tempcmd=`sed -n ''"$ii"'p' ./testfile2-1`
echo "USE test;" > ./temp
    echo "SELECT cmdtitle FROM cmd WHERE cmd='"$tempcmd"' ;"  >> ./temp
    mysql -h 10.0.0.1 -u test -p"test" < ./temp > ./testfile3
aatemp=`sed '1d' ./testfile3`
echo "$aatemp  \"\" " >> ./aatest
ii=$(( $ii + 1 ))
LINETEXTFILE=$(( $LINETEXTFILE - 1 ))
done
tomenubox=`cat ./aatest`

: ${DIALOG=dialog}

exec 3>&1
value=`dialog --backtitle "$usr Tools List" --title "$usr Tools List"  --default-item Dialog --menu "The allow tools is in the list:" 20 60 11 $tomenubox  2>&1 1>&3`
retvala=$?
exec 3>&-

case $retvala in
  0)
    echo "$value chosen."
    echo "USE test;" > ./temp
    echo "SELECT cmdline FROM cmd WHERE cmdtitle='"$value"' ;"  >> ./temp
    mysql -h 10.0.0.1 -u test -p"test" < ./temp > ./testfile4
    cmdtemp=`sed '1d' ./testfile4`
    echo "#!/bin/sh" > ./tempcmdfile
    echo "$cmdtemp" >> ./tempcmdfile
    chmod 700 ./tempcmdfile


    : ${DIALOG=dialog}
#显示所选择命令项的执行结果
    ./killall tempcmdfile
    ./tempcmdfile >tempcmdfile.out &

    $DIALOG --title "TAIL BOX" \
            --tailbox tempcmdfile.out 24 70

    case $? in
      0)
        menulist
#        echo "OK"
        ;;
      255)
        menulist
#       echo "ESC pressed."
        ;;
    esac

    ./killall tempcmdfile

    ;;
  1)
    echo "Cancel pressed.";;
  2)
    echo "Help pressed ($value)";;
  255)
    if test -n "$value" ; then
      echo "$value"
    else
      loginuser
#      echo "ESC pressed."
    fi
    ;;
esac

}


loginuser

if [ $loginpassn -gt 3 ]
then  
  echo "Login Password input error"
else
  loginuser
fi

[/PHP]
 楼主| 发表于 2005-5-18 14:04:07 | 显示全部楼层
在用户关闭SecureCRT终端时同时结束脚本进程
我没想到更好的办法,只能在下次登陆时删除仍在运行但未使用的进程,不过总算目的也达到了

ctrl-回车的问题那位给想想办法 :help
[PHP]
#!/bin/sh
#delpid
loginusr=`whoami`
ps aux |grep $loginusr | gawk '{if($7~/?/) print $0}' | gawk '{if($8~/S/) print $0}' |grep 17 | gawk '{print $2}' > ./tempfile
ps aux |grep $loginusr | gawk '{if($7~/?/) print $0}' | gawk '{if($8~/R/) print $0}' |grep dialog | gawk '{print $2}' >> ./tempfile
lines=`cat ./tempfile | wc -l`
i=1
while [ $lines -ge $i ]
do
out=`sed -n ''"$i"'p' ./tempfile`
kill -9 $out
i=`expr $i + 1`
[/PHP]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-18 15:58:37 | 显示全部楼层
已解决,将test.sh最后一段修改为死循环  
虽然不是好办法但是功能总是达到了

[PHP]left=10
while test $left != 0
do

loginuser
if [ $loginpassn -gt 3 ]
then
  echo "Login Password input error"
else
  loginuser
fi
done
~[/PHP]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-19 13:25:54 | 显示全部楼层
发现新问题~
强行断开远程终端连接时dialog进程仍然在运行且占用90%以上的cpu
怎样才能解决这样的问题啊 :ask
回复 支持 反对

使用道具 举报

发表于 2005-5-19 13:40:51 | 显示全部楼层
连接断开时用w 还能看得到吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-19 14:35:40 | 显示全部楼层
:ask
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-19 14:37:07 | 显示全部楼层

苦恼啊~

用w看不到,用ps看得到

连接时:
[root@localhost ~]# w
14:25:35 up 37 min,  6 users,  load average: 2.38, 1.31, 1.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    :0.0             13:51    3:04   0.17s  0.01s make install
root     pts/1    :0.0             14:04    7:52   0.15s  0.15s bash
testuser pts/2    test.test.com 14:25   11.00s  0.01s  0.01s -bash
root     pts/3    test.test.com 14:25    0.00s  0.01s  0.00s w


断开连接时:
[root@localhost~]# w
14:25:45 up 37 min,  5 users,  load average: 2.32, 1.33, 1.06
root     pts/0    :0.0             13:51    3:14   0.16s  0.01s make install
root     pts/1    :0.0             14:04    8:02   0.15s  0.15s bash
root     pts/3    test.test.com 14:25    0.00s  0.01s  0.00s w

连接时:
[root@localhost ~]# ps aux |grep dialog |grep testuser
testuser 29955  0.0  0.2  5216 1396 pts/4    S+    14:30   0:00 dialog --title LOGIN --clear --inputbox ??            Please Input UserName:? \n 16 51

断开连接时:
[root@localhost ~]# ps aux |grep dialog |grep testuser
testuser 31559 99.9  0.2  4516 1100 ?        R    14:34   0:03 dialog --title LOGIN --clear --inputbox ??            Please Input UserName:? \n 16 51
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-23 10:58:02 | 显示全部楼层
寻求帮助~
:help
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-26 09:05:48 | 显示全部楼层
怎样才能让脚本自杀?
是判断ps中自己进程的tty为?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-26 09:07:23 | 显示全部楼层
怎样才能让脚本自杀?
是判断ps中自己进程的tty为?
但是怎样才能够让这个脚本及其调用的其它命令进程一起自杀呢?
:help  :help  :help  :help  :help  :help  :help  :help
回复 支持 反对

使用道具 举报

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

本版积分规则

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