LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Finding what IP address a process starts (https://www.linuxquestions.org/questions/linux-general-1/finding-what-ip-address-a-process-starts-34193/)

Stephanie 10-30-2002 01:50 PM

Finding what IP address a process starts
 
This is what my friend sent me today, and I thought maybe one of you smart guys out there could help me and him:

I've got a script that clobbers my process...no biggie, I mofied the script to restart the process. However, sometimes (i.e. when I'm not the one babysititng it) it doesn't restart properly. I'm trying to make sure this is the absolute case and that the process isn't dying on its own (it's not supposed to). I've got trackkers in the script to track when it is run and by whom, only problem is it's typically run by a group account. How do I find out the IP of the terminal running the script? I.E., there is a whoami to find out the user ID, but I want a similar command to insert the IP so I know the actual HUMAN that ran the script since Our IP's are static....

I want :
echo "Script ran at `date` by `whoami` " >>log.log

to become:
echo "Script ran at `date` by `whoami` from `**IP ADDRESS COMMAND`" >>log.log

so it prints in the log:
Script ran at 10 pm sun oct 27 2002 by johndoe from 255.255.255.255

crabboy 10-30-2002 03:31 PM

Use the tty command to find out what terminal the user came in on, then you can lookup the terminal in the output of the 'w' command to find the IP.

Stephanie 10-31-2002 12:01 PM

Could this beused to help create a log file?

And what tty command? I did not know there was even that.

crabboy 10-31-2002 01:07 PM

As I understand it you have a shell script that several people may run from different PCs (IP addresses) but may be logged into the Linux box with the same account. You want to track the account name (whoami) and IP address of the PC that launched the script to ultimately start some process? Am i warm?

If I am mostly correct, here is what I described in my post above.

The output of the tty command looks like this:
Code:

# tty
/dev/pts/1

The output of the 'w' command looks like:
Code:

# w
crabboy  pts/1    10.122.2.102:0  24Oct02  0.00s  0.32s  0.05s  w
crabboy  pts/2    10.122.2.102:0  24Oct02 24:06m  0.16s  0.16s  bash
crabboy  pts/3    10.122.2.102:0  Tue15  25:25m  0.01s  0.01s  bash

To get what you need, you'd need something like this:
Code:

#!/bin/sh

TTY=`tty | sed 's&/dev/&&'`

USER_IP=`w | grep ${TTY} | awk ' { printf "User [%s] Logged in from [%s]", $1, $2 } '`

echo $USER_IP >> logfile



All times are GMT -5. The time now is 12:12 PM.