home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / shell / 5129 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  7.5 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!ukma!nsisrv!Pt!postmaster@hq.af.mil!lpeters
  2. From: lpeters@postmaster@hq.af.mil (Leslie D Peters)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: zapuser script questions
  5. Keywords: How do I remove last argument from a variable?
  6. Message-ID: <15297@hq.hq.af.mil>
  7. Date: 21 Dec 92 13:27:07 GMT
  8. References: <1992Dec16.083042.600@hillkomm.dk>
  9. Sender: news@Pt.hq.af.mil
  10. Reply-To: lpeters@marge.hq.af.mil
  11. Organization: 7th Communications Group
  12. Lines: 238
  13.  
  14. Here is a script that I hacked up to handle terminating user sessions
  15. on our systems.   The first file is the routines that perform the
  16. display and kill functions.  The second file is a front end to the
  17. first.
  18. -----CUT HERE-----     -----CUT HERE-----     -----CUT HERE-----
  19. #! /bin/ksh
  20. #
  21. # File ID: T3
  22. #
  23. # Version: 3.1
  24. # Date: 16 Dec 92
  25. # Author(s): Peters
  26. #
  27. # Purpose: to provide an option-oriented user session terminator
  28. #
  29. # Usage: T3 -l   : list current user sessions
  30. #           -d n : display process tree for session n
  31. #           -k n : terminate session n by killing Shell process
  32. #           -c n : terminate session n by killing children of the
  33. #                    Shell process first, then the Shell process
  34. #
  35. # Changes:
  36. #
  37. # V3.0: Initial release - 15 Dec 92 - ldp
  38. # V3.1: modified user interface - 16 Dec 92 - ldp
  39. #
  40. #****************************************************************
  41.  
  42. usage() {
  43.    echo " Usage: T3 -l   : list current user sessions";
  44.    echo "           -d n : display process tree for session n";
  45.    echo "           -k n : terminate session n by killing Shell process";
  46.    echo "           -c n : terminate session n by killing children of the";
  47.    echo "                    Shell process first, then the Shell process";
  48.    return;
  49. }
  50.  
  51. single_space() {
  52.    /bin/awk '{for (i=1;i<=NF;i++) printf("%s ", $i); printf("\n")}' -
  53. }
  54.  
  55. list_sessions() {
  56.    echo "Number of users: \c"
  57.    who -u | /bin/awk '$1 ~ /[a-z]+/ { print $1 }' | sort | uniq | wc -l
  58.    echo "Number of sessions: \c"
  59.    who -u | /bin/awk '$1 ~ /[a-z]+/ { print $1 }' | sort | wc -l
  60.    echo "\n              Idle          Shell   ---Login---"
  61.    echo " # User     S Time  Line     PID    Date   Time"
  62.    echo "-----------------------------------------------"
  63.    /bin/who -u | \
  64.     /bin/awk '$1 ~ /[a-z]+/ \
  65.        { printf("%s:%s:%s:%d:%s:%02d%s\n",$1,$6,$2,$7,$5,$4,$3)}' | \
  66.     sort | \
  67.       /bin/awk -F':' '\
  68.       {
  69.          session[$1]++;
  70.          x = session[$1];
  71.    
  72.          if ( $2 == "." )
  73.          {
  74.             time[$1, x] = "0:00";
  75.             tty[$1, x] = $3;
  76.             rpid[$1, x] = $4;
  77.             lhr[$1, x] = $5;
  78.             lmn[$1, x] = $6;
  79.             ldt[$1, x] = $7;
  80.          }
  81.          else
  82.          {
  83.             if ( $2 == "old" )
  84.             {
  85.                time[$1, x] = "old";
  86.                tty[$1, x] = $3;
  87.                rpid[$1, x] = $4;
  88.                lhr[$1, x] = $5;
  89.                lmn[$1, x] = $6;
  90.                ldt[$1, x] = $7;
  91.             }
  92.             else
  93.             {
  94.                time[$1, x] = $2":"$3;
  95.                tty[$1, x] = $4;
  96.                rpid[$1, x] = $5;
  97.                lhr[$1, x] = $6;
  98.                lmn[$1, x] = $7;
  99.                ldt[$1, x] = $8;
  100.             }
  101.          }
  102.       }
  103.       END {
  104.          for (i in session)
  105.          {
  106.             for (j = 1; j <= session[i]; j++)
  107.             {
  108.                printf("%-8s %d ", i, j);
  109.                printf("%s  %-6s ", time[i, j], tty[i, j]);
  110.                printf("%6d   %s %2d:%02d\n",rpid[i, j],ldt[i, j],
  111.                                             lhr[i, j], lmn[i, j]);
  112.             }
  113.          }
  114.       }' | \
  115.       sort | /bin/awk '{printf("%2d %s\n", NR, $0)}'
  116.    return 0
  117. }
  118.  
  119. kill_shell() {
  120.    SESS=$1
  121.    LINE=`list_sessions | /bin/awk '$1 == sess {print $5}' sess=$SESS`
  122.    RPID=`/bin/ps -ft $LINE | /bin/awk 'BEGIN {getline} {print}' | \
  123.       /bin/sort +4 -5 | \
  124.       /bin/awk 'BEGIN {getline; print $2}'`
  125.    # kill -9 $RPID;
  126.    echo "Shell process $RPID killed";
  127. }
  128.  
  129. kill_children() {
  130.    SESS=$1
  131.    LINE=`list_sessions | /bin/awk '$1 == sess {print $5}' sess=$SESS`
  132.    RPID=`ps -ft $LINE | /bin/awk 'BEGIN {getline} {print}' | sort +4 -5 | \
  133.       /bin/awk 'BEGIN {getline; print $2}'`
  134.    recurse $RPID | sed -e "s/-//g" | \
  135.       /bin/awk '\
  136.       { x[NR] = $1 }
  137.       END {
  138.          for (i=NR; i>1; i--)
  139.             print x[i]
  140.       }' | \
  141.       while read cpid
  142.       do
  143.          # kill -9 $cpid
  144.          echo "Child process $cpid killed"
  145.       done;
  146.    # kill -9 $RPID;
  147.    echo "Shell process $RPID killed";
  148. }
  149.  
  150. disp_process () {
  151.    TAB=
  152.    SESS=$1
  153.    LINE=`list_sessions | /bin/awk '$1 == sess {print $5}' sess=$SESS`
  154.    echo
  155.    RPID=`ps -ft $LINE | /bin/awk 'BEGIN {getline} {print}' | sort +4 -5 | \
  156.       /bin/awk 'BEGIN {getline; print $2}'`
  157.    recurse $RPID
  158. }
  159.  
  160. recurse() {
  161.    PPID=$1
  162.    ps -fp $PPID | /bin/awk 'BEGIN {getline} { print }' |&
  163.    while read -p user pid ppid c stime rest
  164.    do
  165.       if test `echo $stime | /bin/awk '{print length($1)}'` -ne 3
  166.       then
  167.          cmd=`echo $rest | single_space | cut -d' ' -f3-`
  168.          echo "($stime)  $pid $TAB$cmd"
  169.       else
  170.          day=`echo $rest | single_space | cut -d' ' -f1`
  171.          cmd=`echo $rest | single_space | cut -d' ' -f4-`
  172.          echo "($stime $day)  $pid $TAB$cmd"
  173.       fi
  174.    done
  175.    TAB=`echo "$TAB   "`
  176.    export TAB
  177.    ps -ef | /bin/awk '$3 == ppid {print $2}' ppid=$PPID | while read cpid
  178.    do
  179.       recurse $cpid
  180.    done
  181.    return
  182. }
  183.  
  184. case $# in
  185. 0)  usage; exit 1;;
  186. 1)  case $1 in
  187.     -l)  list_sessions;;
  188.     *)   usage; exit 1;;
  189.     esac;;
  190. 2)  case $1 in
  191.     -d)  disp_process $2;;
  192.     -k)  kill_shell $2;;
  193.     -c)  kill_children $2;;
  194.     *)   usage; exit 1;;
  195.     esac;;
  196. *)  usage; exit 1;;
  197. esac
  198. exit 0
  199. -----CUT HERE: T3-----     -----CUT HERE: T3-----
  200.  
  201. -----CUT HERE-----      -----CUT HERE------    ------CUT HERE-----
  202. # File ID: teuproc
  203. #
  204. # Version: 1.0
  205. # Date: 16 Dec 92
  206. # Author(s): Peters
  207. #
  208. # Purpose: provide a clean user interface to T3
  209. #
  210. #**********************************************
  211.  
  212. WDIR=/u/lpeters/work/teuproc
  213. while :
  214. do
  215.    $WDIR/T3 -l
  216.    echo "\n(D)isplay, (S)hell kill, (C)hildren kill, or (E)xit: \c"
  217.    read opt
  218.    case $opt in
  219.      e|E)  exit 0;;
  220.      d|D)  echo "\nSession to display (RETURN to abort): \c";
  221.            read sess;
  222.            if test -n "$sess"; then
  223.            T3 -d $sess; fi;;
  224.      c|C)  echo "\nSession to kill (RETURN to abort): \c";
  225.            read sess;
  226.            if test -n "$sess"; then
  227.            T3 -c $sess; fi;;
  228.      s|S)  echo "\nSession to kill (RETURN to abort): \c";
  229.            read sess;
  230.            if test -n "$sess"; then
  231.            T3 -k $sess; fi;;
  232.      *)    echo "\nIllegal option.";;
  233.    esac
  234.    echo
  235. done       
  236. -----CUT HERE: TEUPROC-----     ---CUT HERE: TEUPROC-------
  237.  
  238. I welcome comments/flames on this labor of love.
  239. -- 
  240. /============================================================================\
  241. |                                            |                               |
  242. |                    ___                     |                               |
  243. |       ___....-----'---`-----....___        |   I haven't lost my mind --   |
  244. | =========================================  |                               |
  245. |        ___`---..._______...---'___         |    it's backed up on          |
  246. |       (___)      _|_|_|_      (___)        |           tape somewhere.     |
  247. |         \\____.-'_.---._`-.____//          |                               |
  248. |           ~~~~`.__`---'__.'~~~~            |   lpeters@marge.hq.af.mil     |
  249. |                   `~~~'                    |                               |
  250. |                                            |     Les Peters                |
  251. \============================================================================/
  252.