The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calling a script from a level above where the script resides SummitElse UNIX for Advanced & Expert Users 2 10-26-2007 06:27 AM
calling a script from another madhu_aqua14 UNIX for Dummies Questions & Answers 2 02-27-2007 03:16 AM
Box A's perl script calling box B's shell script new2ss Shell Programming and Scripting 1 09-13-2006 03:17 AM
calling dos2unix on shell script from within the script vino Shell Programming and Scripting 4 04-08-2005 12:38 AM
Calling C from within a csh script barisgultekin Shell Programming and Scripting 2 05-24-2002 06:21 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-28-2008
Registered User
 

Join Date: Feb 2008
Posts: 5
How to identify the calling script?

Hi,

I have two scripts ( /tmp/k1.sh and /tmp/k2.sh ).
k1.sh calls the k2.sh .
For security reasons, I must be sure that the k2.sh is being called by the k1.sh .
Is it possible for the k2.sh identify that it's been called by the k1.sh?
I mean, identify the complete path of the k1.sh ( /tmp/k1.sh ).

Thanks for any help,

Cremm.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-28-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Quote:
Originally Posted by crematoriumm View Post
Hi,

I have two scripts ( /tmp/k1.sh and /tmp/k2.sh ).
k1.sh calls the k2.sh .
For security reasons, I must be sure that the k2.sh is being called by the k1.sh .
Is it possible for the k2.sh identify that it's been called by the k1.sh?
I mean, identify the complete path of the k1.sh ( /tmp/k1.sh ).

Thanks for any help,

Cremm.
Interesting.

Couple of ways.

1. Why dont you embed k2.sh in k1.sh ? After all k2 serves k1 alone.

2. Just before you call k2.sh, export a unique variable from k1.sh. When you start k2.sh, check for that new export. If not there, then act accordingly.

3. Use the features from ps. You can list the ps output in a tree format. From k2.sh get your own pid and your parents pid ($PPID), traverse up the list 1 step to get your parent's PID and CMD. Compare that CMD with the complete path of k1.sh or compare just the pid value. If it matches, proceed. This works well if the path to k1.sh remains the same throughout.

There may be other ways as well... nothing strikes me now.

But none of these solutions stand (except for sol1) if someone writes their own k1.sh.

Last edited by vino; 02-28-2008 at 06:30 AM.
Reply With Quote
  #3 (permalink)  
Old 02-28-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 954
Try the following

Code:
ps -ef | grep $PPID  | grep -v grep | grep -v awk | awk '{ print $7 }'
Reply With Quote
  #4 (permalink)  
Old 02-28-2008
Registered User
 

Join Date: Feb 2008
Posts: 5
Quote:
Originally Posted by vino View Post
Interesting.

Couple of ways.

1. Why dont you embed k2.sh in k1.sh ? After all k2 serves k1 alone.

2. Just before you call k2.sh, export a unique variable from k1.sh. When you start k2.sh, check for that new export. If not there, then act accordingly.

3. Use the features from ps. You can list the ps output in a tree format. From k2.sh get your own pid and your parents pid ($PPID), traverse up the list 1 step to get your parent's PID and CMD. Compare that CMD with the complete path of k1.sh or compare just the pid value. If it matches, proceed. This works well if the path to k1.sh remains the same throughout.

There may be other ways as well... nothing strikes me now.

But none of these solutions stand (except for sol1) if someone writes their own k1.sh.
Vino,

I can't use the solutions 1 and 2 for security reasons, but the third solution sounds to be great.

I did this:

The code:

#!/bin/ksh
var1=`echo $$`
echo "k2.sh pid => " $var1 ## Just for checking. Will be erased...
echo "Listing processes with the k2.sh pid" ## Just for checking. Will be erased...
ps -ef | grep $var1 ## Just for checking. Will be erased...
v=`ps -ef | grep $var1 | sort +1 -2 | cut -d" " -f4 | head -1`
echo "k2.sh parent process => "$v ## Just for checking. Will be erased...
echo "Listing processes with the k2.sh parent process" ## Just for checking. Will be erased...
ps -ef | grep $v ## Just for checking. Will be erased...
echo "The parent process..." ## Just for checking. Will be erased...
ps -ef | grep $v | sort | head -1 | cut -d" " -f13

The result:

server01:/var/tmp$ /var/tmp/k1.sh
k2.sh pid => 29483
Listing processes with the k2.sh pid
user01 29484 29483 1 12:43:59 pts/38 0:00 ps -ef
user01 29483 29482 0 12:43:59 pts/38 0:00 /bin/ksh /var/tmp/k2.sh
k2.sh parent process => 29482
Listing processes with the k2.sh parent process
user01 29482 28411 0 12:43:59 pts/38 0:00 /bin/ksh /var/tmp/k1.sh
user01 29483 29482 0 12:43:59 pts/38 0:00 /bin/ksh /var/tmp/k2.sh
The parent process...
/var/tmp/k1.sh

Can you see any security hole in it?

Thanks a lot for your help!!

Cremm.
Reply With Quote
  #5 (permalink)  
Old 02-28-2008
Registered User
 

Join Date: Feb 2008
Posts: 5
Quote:
Originally Posted by fpmurphy View Post
Try the following

Code:
ps -ef | grep $PPID  | grep -v grep | grep -v awk | awk '{ print $7 }'
fpmurphy,

your solution didn't work for me. Check it out.....

server01:/var/tmp$ /var/tmp/k1.sh
pts/27
0:00
0:00
pts/101
?
?
?
pts/15
pts/15
pts/27

Thanks anyway for your help,

Cremm.
Reply With Quote
  #6 (permalink)  
Old 02-28-2008
Registered User
 

Join Date: Feb 2008
Posts: 5
Making it simpler, after reading Vino's answer slowly....


v=$PPID
ps -ef | grep $v | sort | head -1 | cut -d" " -f14


[]s,

Cremm.
Reply With Quote
  #7 (permalink)  
Old 02-28-2008
Registered User
 

Join Date: Feb 2008
Posts: 5
Quote:
Originally Posted by crematoriumm View Post
fpmurphy,

your solution didn't work for me. Check it out.....

server01:/var/tmp$ /var/tmp/k1.sh
pts/27
0:00
0:00
pts/101
?
?
?
pts/15
pts/15
pts/27

Thanks anyway for your help,

Cremm.
Making it....

ps -ef | grep $PPID | sort | head -1 | awk '{ print $NF }'

server01:/var/tmp$ /var/tmp/k1.sh
/var/tmp/k1.sh

[]s,

Cremm.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0