Baanboard.com

Baanboard.com (http://www.baanboard.com/baanboard/index.php)
-   Code & Utilities (http://www.baanboard.com/baanboard/forumdisplay.php?f=33)
-   -   CODE: Show which session a user is running (http://www.baanboard.com/baanboard/showthread.php?t=1936)

NPRao 6th May 2002 20:44

Pat gave me this idea to make a shell script to get the output in a formatted way... hope everyone finds it useful...

I found that it does take some execution time so have patience!

I hope I am ok with this copyright documentation stuff for the coding...

Code:


#*************************************************
# Platform : UNIX
# Copyright 2002 - by N. Prashanth Rao
# All Rights Reserved
# Permission to use, copy, modify, and distribute this software
# and its documentation for any purpose and without fee is
# hereby granted, provided that the above copyright notice
# appear in all copies and that both that copyright notice and this
# permission notice appear in supporting documentation. 
# N. Prashanth Rao and Baanboard.com DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS  SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS, IN NO EVENT SHALL N. Prashanth Rao nor
# Baanboard.com BE LIABLE FOR ANY  SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES  WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS,  WHETHER
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
# THE USE  OR PERFORMANCE OF THIS SOFTWARE.
#*************************************************

# Shell script to verify if a user is executing a particular session
# Author: N. Prashanth Rao

clear
if test $# -ne 1
then
        echo "Command Usage: $0 Session-code"
        exit
fi
for i in `ps -ef | grep bshell | grep -v "grep bshell"|awk '{print $2}'`
do
        bshcmd6.2 -s -p -u5 -w5 $i | grep $1 > /dev/null
        if test $? -eq 0
        then
                userid=`ps -ef | grep $i | cut -f1 -d" " | uniq`
                echo "Session " $1 "in use by " $userid
        fi
done


frajer 11th February 2010 14:28

Only a few years :) passed since that code was written.
I found it usefull. Also I added another functionality: print all sessions that are run (open) by a particular user.
I hope NPRao doesn't mind it :)

Code:


#*************************************************
# Platform : UNIX
# Copyright 2002 - by N. Prashanth Rao
# All Rights Reserved
# Permission to use, copy, modify, and distribute this software
# and its documentation for any purpose and without fee is
# hereby granted, provided that the above copyright notice
# appear in all copies and that both that copyright notice and this
# permission notice appear in supporting documentation.
# N. Prashanth Rao and Baanboard.com DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS  SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS, IN NO EVENT SHALL N. Prashanth Rao nor
# Baanboard.com BE LIABLE FOR ANY  SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES  WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS,  WHETHER
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
# THE USE  OR PERFORMANCE OF THIS SOFTWARE.
#*************************************************

# Shell script to verify if a user is executing a particular session
# Author: N. Prashanth Rao
# Add-on: to verify which sessions are run (open) by a part.user
# Author: FraJer

clear
if test $# -ne 2
then
        echo "\nCommand Usage: $0 [s Session-code|u User]\n"
        exit
fi


case $1 in
  s )  echo "\nSession " $2 "is in use by:"
        for i in `ps -ef | grep bshell | grep -v "grep bshell"|awk '{print $2}'`
        do
                bshcmd6.1 -s -p -u5 -w5 $i | grep $2 > /dev/null
                if test $? -eq 0;then
                        userid=`ps -ef | grep $i | awk '{print $1}' | uniq`
                        echo $userid
                fi
        done
        ;;
  u )  grep $2 /etc/passwd > /dev/null
        if [[ $? != 0 ]]; then echo "\nUser "$2" does not exist. Exiting ...\n";exit;fi
        userbsh=`ps -ef|grep $2|grep bshell| grep -v "grep bshell"|awk '{print $2}'`
        echo "\nUser " $2 "runs the following sessions:"
        bshcmd6.1 -s -p -u 5 -w 5 $userbsh | grep object | awk -F\: '{print $2}' | \
          egrep -v "dskbrowser|dskprogman|stpstdlib|stpmenu|stpdisplay" | sort -u
        ;;
  * )  echo "\nError - read Command Usage again."\n
esac


NPRao 12th February 2010 04:23

Franci,

I don't mind. It's great to see someone using the utility I built after years :)
I noticed you were checking the users exists from the local Unix server's password file. It might not work if the servers were in NIS like our installations.

Here is the code segment which checks both.
Code:


##########################################################
#/bin/ksh
#set -vx
# Author: N. Prashanth Rao
# Check User Id exists on local Unix Server or NIS setup.
# 11-Feb'2010
##########################################################
userid=$1
grep $userid /etc/passwd > /dev/null
if [[ $? != 0 ]]; then
        local_user=0
else
        local_user=1
fi
/usr/bin/ypcat passwd | grep $userid > /dev/null
if [[ $? != 0 ]]; then
        nis_user=0
else
        nis_user=1
fi
if [[ $local_user -eq 1 ]] then
        echo "Local User found"
fi
if [[ $nis_user -eq 1 ]] then
        echo "NIS User found"
fi
if [ ${local_user} -eq 1 -o  ${nis_user} -eq 1 ]
then
        echo "User Id "$userid " exists"
fi
if [ ${local_user} -eq 0 -a  ${nis_user} -eq 0 ]
then
        echo "User Id "$userid " does not exist"
fi
##########################################################


bhushanchanda 13th June 2013 22:13

Any similar thing for Windows?
 
Hi,

Waking up this thread after 3 years I would like to ask if it can be implemented in Windows with command prompt? Or its use is limited to Unix shell?

~Vamsi 13th June 2013 23:33

Yes - it can be done.

bhushanchanda 14th June 2013 10:40

Hi Vamsi,

Any short suggestions?

~Vamsi 17th June 2013 22:35

Bhushan,

What have you tried so far and where are you stuck?

bhushanchanda 18th June 2013 09:49

Error in init_user
 
Hi Vamsi,

I am not familiar with shell script but I tried to compare the shell commands with dos commands and found that the logic is to get the processes running currently and the use the bshell.* file to get the info required.

Accordingly, I tried to execute this command:-

bshcmd -p <bshell_pid>

But it gave me error:-

Quote:

bshcmd: User file for user 'baan' does not exist. Last searched location '${BSE}
/lib/user/ubaan'
bshcmd: Current user is not a valid Baan user.
Error in init_user
I tried to search the related thread and tried everything, like

1) Using SET USER = baan
2) Setting environment variable to my {BSE}\bin
3) Logging in from baan account
4) There was no file named ubaan in my \lib\user folder so I copied it from \lib\user\u folder (where all my u files reside) and paste it in \lib\user folder but the same results.

So, I am stuck here.

bhushanchanda 18th June 2013 10:22

Solved!
 
Hi,

I have solved the issue. The next problem is I am unable to redirect the output to a file.

I used this:-

bshcmd -p <bshell_pid> > C:\file.txt

It produces the file, but its empty.

Is there any other way?

~Vamsi 18th June 2013 17:54

Quote:

Originally Posted by bhushanchanda (Post 182459)
Hi,

I have solved the issue.

What was the issue? How did you solve it - please document so that others don't trip over the same.

Typically with Baan running on Windows server there is a batch file named fillenv.bat in $BSE/bin folder. You need to execute that before running any other Baan related utilities to work.

Most Baan commands have an option of -qo which redirects the output to a file. Looks like bshcmd does not have one.

You can see the output on screen but you cannot capture it into a file when using redirection. This requires us to go back to basics. There are two streams that get sent to screen. One is stdout and the other stderr.

The redirector > will send the stdout to the file.

Perhaps bshcmd is writing to stderr? The one way to test this hypothesis is to capture the stderr. This can be captured with 2>

So try bshcmd -p <bshell_pid> 2> C:\file.txt

bhushanchanda 18th June 2013 19:12

2> works!
 
Hi Vamsi,

Yes, you are right. I just executed command fillenv before executing bshcmd -p <bshell_pid> .

And guess what,

bshcmd -p <bshell_pid> 2> C:\test.txt worked!

It gave me what I required. Will carry on with the further things. Thanks, never new 2> can be used. What I read about it was, we can use 2>&1 so, I did gave a try to 2>&1 but didnt work.

bhushanchanda 19th June 2013 10:50

PID of specific user?
 
Hi Vamsi,

I am halfway, I need to know, how can I know the pid of a specific user?

I guess my first step will be to find the pid of the user, for which I need to know, the currently used session/session's.

I tried to google it, but came with no conclusions.

I tried TASKLIST with various options like /F but still no success with that.

~Vamsi 19th June 2013 18:40

Try running the command shell as Administrator before doing anything else.

As an aside get ConEmu from https://code.google.com/p/conemu-maximus5/

bhushanchanda 19th June 2013 20:43

Knowing pid of Baan User
 
Thanks Vamsi,

The emultor seems good. Actually I was able to run all commands successfully (yes I was using Admin account ), but my question was, if I was to know which session a Baan User is running, I need to know his/her PID right? So, the question was how can I get the PID of that particular Baan User?

I didnt get any such function in prog guide. Can I get that using bshcmd command?

~Vamsi 19th June 2013 21:02

Refer back to the way that NPRao is coding. Try to convert his code to Windows as a first step.

tasklist /FI "USERNAME eq <username>" /FI "IMAGENAME eq ntbshell.exe" /NH /FO table

I haven't yet seen any batch script that you are developing. So I don't know what you are upto.

Here is an example of getting PID from tasklist
http://stackoverflow.com/questions/9...tarted-process


All times are GMT +2. The time now is 05:04.


vB.Sponsors
©2001-2017 - Baanboard.com - Baanforums.com