Languages and Scripting

get the IP address of the current telnet session with C function

 
SOLVED
Go to solution
Joel Girot
Trusted Contributor

get the IP address of the current telnet session with C function

Hi to all,
I would like to know if there is a way I can find out the IP address of my current telnet or ssh session with C functions. HPUX 11.11, untrusted system.

Thanks
8 REPLIES 8
Simon Hargrave
Honored Contributor
Solution

Re: get the IP address of the current telnet session with C function

This information is stored in the utmp file.

From the shell you can get this information with: -

who -mu (the last field is the hostname/ip address)

If you want to do this with C calls you'll have to use the getut calls.

man 3 getut

for more details.
Joel Girot
Trusted Contributor

Re: get the IP address of the current telnet session with C function

Thanks Simon,
I will try and I will publish my result... after my holliday.

Joel

Joel Girot
Trusted Contributor

Re: get the IP address of the current telnet session with C function

Hi

The code I included shows how I do this.
I am a beginner with c language, excuse me for possible mistake...

bye

Joel
Muthukumar_5
Honored Contributor

Re: get the IP address of the current telnet session with C function

I have checked your code using HP92453-01 A.11.01.21 HP C (Bundled) Compiler.

Attached is working good.

hth.
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: get the IP address of the current telnet session with C function

Great, Its working good (tested in 11.23)

device name : pts/tb
pid : 15458
type of entry : 7
Process termination status : 0
Process exit status : 3
seconds time entry was made : 1127917725
microseconds time entry was made : 0
my device name : pts/0

user login name : root
id : ts/0
device name : pts/0
pid : 12718
type of entry : 7
Process termination status : 0
Process exit status : 0
seconds time entry was made : 1128328306
microseconds time entry was made : 517252
............
............

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
A. Clay Stephenson
Acclaimed Contributor

Re: get the IP address of the current telnet session with C function

Actually a much easier and more reliable method makes use of the getpeername() system call. Man 2 getpeername for details.
If it ain't broke, I can fix that.
Joel Girot
Trusted Contributor

Re: get the IP address of the current telnet session with C function

Clay, have you an example of the use of getpeername ?

Joel
Stephen Keane
Honored Contributor

Re: get the IP address of the current telnet session with C function

getpeername uses the socket id of a connected socket. Which socket would that be then? Or can any connected socket do?