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...
#*************************************************
# 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 :)
#*************************************************
# 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.
##########################################################
#/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
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
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:-
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
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
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
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
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
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/9486960/windows-batch-scripting-get-pid-of-just-started-process
bhushanchanda
23rd June 2013, 09:27
Hi Vamsi,
It was not that easy to do the same in Windows (may be coz I am not a command line expert). But, finally its done :)
Thanks for your support!
Created a new thread (http://www.baanboard.com/baanboard/showthread.php?p=182566#post182566) for Windows Version.
NPRao
14th July 2018, 23:06
16 years later if the thread/utility is still relevant, here is the Windows version.
###############################################################################
# PowerShell Script to find which Bshell connection is using a Session code.
# Author - N. Prashanth Rao
###############################################################################
# Platform : Windows 7, 10.
# Copyright 2018 - 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.
###############################################################################
# Define Input Parameters
# Mandatory - Baan/LN Input Session or 3-GL Object code.
###############################################################################
# Pre-requisites:
# The BSE, BSE_LOG, BSE_TMP are set as System Environment Variables on the LN
# Application Server.
# PowerShell Version 5 or above is installed.
###############################################################################
PARAM([Parameter(Mandatory=$true)][string]$SESSIONCODE)
################################################################################
Clear-Host
$BSHCMD = (Get-ChildItem Env:BSE).Value + "bin\bshcmd.exe"
$OUTPUTFILE = New-TemporaryFile
#$bshell_ids = (get-process -name ntbshell).Id
$PROCINFO = (get-process ntbshell -includeusername)
write-host "Bshell Process Id, User using the Session"$SESSIONCODE -ForegroundColor Red -BackgroundColor Yellow
foreach($counter in $PROCINFO) {
$PARAMS = "-s -p -u5 -w5 " + $counter.Id
# Start-Process $BSHCMD -ArgumentList $PARAMS -NoNewWindow -Wait -RedirectStandardOutput $OUTPUTFILE -RedirectStandardError $OUTPUTFILE2
Start-Process $BSHCMD -ArgumentList $PARAMS -NoNewWindow -Wait -RedirectStandardError $OUTPUTFILE
$SessionFound = Select-String -Pattern $SESSIONCODE -Path $OUTPUTFILE
if ($SessionFound.Count -gt 0) {
write-host $counter.Id $counter.UserName
}
Clear-Content $OUTPUTFILE
}
Remove-Item $OUTPUTFILE.FullName -Force
################################################################################
mark_h
17th July 2018, 15:02
I would say it is still relevant.