?
Solved

Telnet eviction script ofr AIX and scheduleing with cron

Posted on 1998-02-19
19
Medium Priority
?
879 Views
Last Modified: 2013-12-23
We are running AIX and need a script to kill a telnet connection by user name.  The syntax needs to be EVICT <Login Name>.  The script needs to go find all entrys for that user and kill the sessions.  There will be two that need to be killed.
  The first will be "imsrun" which needs a kill -15
  The second is the shell which will need a kill -9

The manual way we do this is:

We Type:
ps -futest

Output:
  UID  PID     PPID   C      STIME          TTY    TIME  CMD
test  69054   93078  0  07:54:18  pts/13  0:43  imsrun LOGON
test  93078   53140  0  07:54:15  pts/13  0:00  -bsh

We Type:
kill -15 69054
kill -9 93078


That kills the session.  It would also be nice to kill the telnet daemon if possible.

The second part is a script that gets the list of currently logged in users and calls the first script to kill each one except several that need to stay logged in.  I also need to know how to schedule this for 23:00 using cron.

Thanks for the help.
0
Comment
Question by:unitymtg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 8
19 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1583143
Here are two /bin/sh scripts which should do the job:

First, evict.sh

#!/bin/sh

echo "Evicting $1"
PROCS=`ps -fu $1`
for i in "$PROCS"
do
        PID2KILL=`echo "$i" | awk '{ print $2}'`
        for j in $PID2KILL
        do
                if [ "$j" != "PID" ]
                then
                        echo  "kill -9 $j"
                fi
        done
done


and then kill-users.sh

!/bin/sh

USERS=`who`
EXCEPT=root

for i in "$USERS"
do
        NAME=`echo "$i" | awk '{ print $1 }'`
        NAME=`echo "$NAME" | sort -u`
        for j in $NAME
        do
                if [ $j != $EXCEPT ]
                then
                        evict.sh $j
                fi
        done
done

To schedule this at 2300, dump your current crontab like this:

crontab -l >my_crontab

edit my_crontab and add a line like:

0 23 * * * /path/to/kill-users.sh

then load it into your crontab with crontab <my_crontab

That should do it...
0
 

Author Comment

by:unitymtg
ID: 1583144
Two questions before I accept the answer.  1. Where does the kill -15 to the imsrun process come into play? 2. How do I add more users to the exception list?

Thanks,
Rob
0
 
LVL 32

Expert Comment

by:jhance
ID: 1583145
!/bin/sh

USERS=`who`
EXCEPT=root joe bob fred

for i in "$USERS"
do
        NAME=`echo "$i" | awk '{ print $1 }'`
        NAME=`echo "$NAME" | sort -u`
        for j in $NAME
        do
            DOKILL=1
            for k in $EXCEPT
            do
                      if [ $j == $k ]
                      then
                              DOKILL=0
                      fi
            done

            if [ $DOKILL == 1 ]
            then
                  evict.sh $j
            fi
        done
done

As far as killing imsrun with "-15" you could do this:


#!/bin/sh

echo "Evicting $1"
PROCS=`ps -fu $1`
for i in "$PROCS"
do
        PID2KILL=`echo "$i" | awk '{ print $2}'`
      PROC=`echo "$i" | awk '{ print $8}'`
        for j in $PID2KILL
        do
                if [ "$j" != "PID" ]
                then
                  if [ $PROC = "imsrun" ]
                  then
                        kill -15 $j
                  else
                              kill -9 $j
                  fi
                fi
        done
done
0
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 32

Expert Comment

by:jhance
ID: 1583146
Let me also suggest a good book:

Unix Shell Programming by Kochan and Wood.  Hayden Books, ISBN 0-672-48448-X
0
 

Author Comment

by:unitymtg
ID: 1583147
Thanks for the help.  I'll try it tonight and accept your answer if it works.

Thanks,
Rob
0
 

Author Comment

by:unitymtg
ID: 1583148
Thanks, I'll get the book.  The script almost works.  This last problem again relates to the kill -15 or kill -9.  PID2KILL and PROC contain a list of the processes (plus the header) and the CMD repectively.  The script steps through the PID2KILL variable but not the PROC variable so the  if [ $PROC = "imsrun" ]  statement is alway false.  $PROC contains:

CMD
imsrun LOGON
-bsh

.  How do we step through the PROC variable at the same time that we step through the PID2KILL or tie the current PID to the current PROC?

Thanks,
Rob
0
 
LVL 32

Expert Comment

by:jhance
ID: 1583149
I had a typo in the following if [ $PROC = "imsrun" ] line:

if [ $PROC == "imsrun" ]


0
 

Author Comment

by:unitymtg
ID: 1583150
FOund one logic problem.  If the shell was the first process encountered, the shell will be killed before the imsrun process.  I will therefore run through the PID2KILL list twice, first for the imsrun processes, then for the shells.
 Therefore, I decided to get the imsrun part working first.  I entered the following code into a script in the bin directoryy called "evict"

#!/bin/sh

echo "Evicting $1"
PROCS=`ps -fu $1`
for i in "$PROCS"
  do
    PID2KILL=`echo "$i" | awk '{ print $2}'`
    PROC=`echo "$i" | awk '{ print $8}'`
    for j in "$PID2KILL"
      do
        if [ "$j" != "PID" ]; then
          if [ $PROC == "imsrun" ]; then
            echo "Killing imsrun process# $j"
            kill -15 $j
          fi
        fi
    done
done  

When I run a second telnet session and logon as ga1, then go back to my root user and type "evict ga1", I get the following message:

/usr/bin/evict[12]: imsrun: 0403-012 A test command parameter is not valid.

0
 
LVL 32

Expert Comment

by:jhance
ID: 1583151
/usr/bin/evict[12]: imsrun: 0403-012 A test command parameter is not valid.

The test command is the part in "[]" of:

 if [ $PROC == "imsrun" ];

Since "imsrun" is OK, you might put an "echo $PROC" before this and see that is in the variable.
0
 

Author Comment

by:unitymtg
ID: 1583152
I get a line of output before the error that says "CMD -bsh imsrun"
0
 

Author Comment

by:unitymtg
ID: 1583153
If I encolse the $PROC in "" then I don't get the error, put =="imsrun" is never try because the list is not being stepped through.
0
 

Author Comment

by:unitymtg
ID: 1583154
Sorry about the last comment.  What I was trying to say was that if I enclose the $PROC in "" and remove one of the commas, I do not get an error, but the =="imsrun" portion is never true because the $PROC variable is never being stepped through.
0
 

Author Comment

by:unitymtg
ID: 1583155
Sorry about the last comment.  What I was trying to say was that if I enclose the $PROC in "" and remove one of the commas, I do not get an error, but the =="imsrun" portion is never true because the $PROC variable is never being stepped through.
0
 

Author Comment

by:unitymtg
ID: 1583156
Sorry again, I meant if I remove one of the "="
0
 
LVL 32

Expert Comment

by:jhance
ID: 1583157
Sorry about being slow to get back with you.  I was out of town all day and just got back.  Let me work on this tomorrow and post a correction then.  I'm not at my AIX machine and can't check it for sure...
0
 
LVL 32

Expert Comment

by:jhance
ID: 1583158
I just can't get this script to work right under AIX!!  I can get it working under LINUX and SunOS but not on the IBM.  Do you have PERL available?  If so, I'll post a PERL version that works fine.
0
 

Author Comment

by:unitymtg
ID: 1583159
 I don't know, but I will find out.  In the mean time, I will reopen the question.  If someone is able to help, I'll give you the 400 points and give them 50 or something.  Hope that's OK with you.
  If someone else can get the script working, or knows if PERL comes with AIX 4.2.6 I would appriciate help.

THanks,
Rob
0
 
LVL 32

Accepted Solution

by:
jhance earned 1600 total points
ID: 1583160
OK, I think I've got this now...


#!/bin/sh
IFS="["

echo "Evicting $1"
PROCS=`ps -fu $1 | sed 's/$/[/g' | sed 's/  / /g'`
for i in $PROCS
do
        PID2KILL=`echo $i | awk '{ printf("%s", $2)}'`
        PROC=`echo $i | awk '{ printf("%s", $8)}'`
        if [ $PID2KILL != "PID" ]; then
                if [ $PROC = "imsrun" ]; then
                        echo "kill -15 $PID2KILL"
                        kill -15 $PID2KILL
                else
                        echo "kill -9 $PID2KILL"
                        kill -9 $PID2KILL
                fi
        fi
done
0
 

Author Comment

by:unitymtg
ID: 1583161
I got the following script working.  Thank you immensely for your help.

Rob

#!/bin/sh

echo "Evicting $1..."
PROCS=`ps -fu $1 | sed 's/$/[/g' | sed 's/ /[/g'`

for i in $PROCS; do
  IMSPID=`echo $i | awk -F [ '{print $8}'`
  SHLPID=`echo $i | awk -F [ '{print $10}'`
  PROC=`echo $i | awk -F [ '{print $19}'`

  if [ "$PROC" = "imsrun" ]; then
    echo "Killing imsrun process #$IMSPID..."
    kill -15 $IMSPID
    echo "Killing shell process #$SHLPID..."
    kill -9 $SHLPID
  fi
done

0

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had an issue with InstallShield not being able to use Computer Browser service on Windows Server 2012. Here is the solution I found.
Make the most of your online learning experience.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question