LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2011, 10:18 AM   #1
vijirin
LQ Newbie
 
Registered: Sep 2005
Posts: 20

Rep: Reputation: 0
How to get the ip address of the client machine


Dear all,

I am connecting to linux server(RHEL4.7) using putty from my windows machine. Is there any way to get the ip address of my windows machine using C program in linux.


Thanks in advance.

Viji
 
Old 01-02-2011, 11:44 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

You can probably just parse the value of $SSH_CLIENT (or $SSH_CONNECTION ).
 
Old 01-02-2011, 10:00 PM   #3
vijirin
LQ Newbie
 
Registered: Sep 2005
Posts: 20

Original Poster
Rep: Reputation: 0
Hi,

Thanks a lot.

If I am connecting using ssh, it works perfect.

But if i try to connect using telnet, it will not work.

Is there any other way to get the ip?

Regards

Viji
 
Old 01-02-2011, 10:18 PM   #4
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,326
Blog Entries: 28

Rep: Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142
A ping name would normally return the ip address.
 
Old 01-03-2011, 12:00 AM   #5
an15wn
LQ Newbie
 
Registered: Dec 2010
Location: Jakarta, ID
Distribution: Fedora, Ubuntu
Posts: 18

Rep: Reputation: Disabled
Quote:
Originally Posted by vijirin View Post
But if i try to connect using telnet, it will not work.

Is there any other way to get the ip?
I think you can extract the output of who -m command. The ip address of the login terminal is provided there...
 
Old 01-03-2011, 11:40 AM   #6
vijirin
LQ Newbie
 
Registered: Sep 2005
Posts: 20

Original Poster
Rep: Reputation: 0
Hi,

Is there any system calls to get the ip of the machine?


regards

viji.R
 
Old 01-03-2011, 02:20 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Well you can look at the connection table, e.g. netstat -plnt but system calls seem odd as you are not internal to the server side process itself. What information would you expect to give to this system call? What if you have 100 concurrent connections from 65 different clients?
 
Old 01-03-2011, 08:31 PM   #8
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,326
Blog Entries: 28

Rep: Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142
Quote:
Originally Posted by vijirin View Post
I am connecting to linux server(RHEL4.7) using putty from my windows machine. Is there any way to get the ip address of my windows machine using C program in linux.
I just looked back at your original post. I'm confused.


This is what I'm envisioning after rereading the OP:


Windows with putty-------------ssh>>----------------->Linux
Machine<-------------<<ping--------------------------Machine



You say that you are connecting to the Linux box FROM the Windows machine via putty and want the ip address of the Windows machine that you are sshing FROM, since that's the only Windows machine in the story.

If you want the ip address of your Windows machine, ssh won't help you. Open a command window on the Windows windows machine and run the command ipconfig /all. That's IPconfig, not IFconfig. That will tell you the ip address and the gateway and lots of other stuff for the Windows machine.

From your ssh connection, you could try telling the Linux box to ping back at your Windows box and see what happens.

You could try a traceroute from your local Windows box to the remote Linux box to see how many hops are involved.

If your Windows machine is on a subnet, say a work machine, behind multiple routers, you are unlikely to find out a route from the Big Wide World to your Windows machine. You can use your ssh connection to tell your Linux machine to ping the Windows machine and see what happens. Unless port forwarding is properly set up, though, you won't be able to get to it. If your work IT department is worth their salt, you should not be able to ping your local work Windows machine from outside.

Please let me know whether my re-interpretation of your original request is accurate or wildly off the mark.

If you are trying to telnet into your Linux box, does it have a telnet daemon running?

http://aplawrence.com/Linux/enable_telnet.html
 
Old 01-03-2011, 09:26 PM   #9
an15wn
LQ Newbie
 
Registered: Dec 2010
Location: Jakarta, ID
Distribution: Fedora, Ubuntu
Posts: 18

Rep: Reputation: Disabled
Quote:
Originally Posted by vijirin View Post
Hi,

Is there any system calls to get the ip of the machine?


regards

viji.R
IP address of WHICH machine? Windows?
Where is your application? Linux?

As I suggested before... use the output of who -m.

Simple but needs to process the content of ./guest_ip further.
Code:
system ("who -m > ./guest_ip");
More complex but hopefully no more process needed. Just read the ./guest_ip file.
Code:
system ("who -m | sed  \"s/[^(]*(\([^)]*\))/\1/\" > ./guest_ip");
(untested using C, but the command was tested at the terminal)

This is the scenario:
Machine with ip 10.20.18.51 connect TO 10.20.18.101 via ssh.
The virtual-server ip addr is 10.20.18.101.
Code:
[anis@anis ~]$ ssh 10.20.18.101
anis@10.20.18.101's password: 
Last login: Tue Jan  4 09:23:45 2011 from 10.20.18.51
[anis@virtual-server Sims]$ ifconfig eth5
eth5      Link encap:Ethernet  HWaddr 08:00:27:7F:C2:B2  
          inet addr:10.20.18.101  Bcast:10.20.18.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe7f:c2b2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8772 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4887 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:759806 (741.9 KiB)  TX bytes:658985 (643.5 KiB)

[anis@virtual-server Sims]$ who -m 
anis     pts/0        2011-01-04 09:23 (10.20.18.51)
[anis@virtual-server Sims]$ who -m | sed  "s/[^(]*(\([^)]*\))/\1/"
10.20.18.51
[anis@virtual-server Sims]$
Good luck...
 
Old 01-26-2011, 07:22 AM   #10
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
This is quite easy to do. However, I would recommend not doing this in C as you will need to mess with Linux specific stuff like the /proc directory. Unless you have good reason doing so, you will save yourself immense maintenance and code writing by using Shell commands as `lsof` or `netstat`.

This is from Linux:
Code:
$ lsof -i tcp:22
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ssh     2783 jcook    3u  IPv6 125833      0t0  TCP eli:50512->eli:ssh (ESTABLISHED)
This is from Mac:
Code:
$ lsof -p $PPID -i tcp:22 | grep :ssh-\> | tail -n1 | awk '{print $9}' | awk -F-\> '{print $2}'
eli.config:43187
So, you can see with a little Bash foo you can get exactly what you want, it will be more resistant to change, and can be quite a bit easier on different platforms -- that support Bash of course. The Mac portion is printing out the host name of the client machine (what you want) and the port it has connected to from.

Last edited by jcookeman; 01-26-2011 at 07:24 AM. Reason: / was backward :)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dhcp-client get assigned a network address instead of a regular IP address kenneho Linux - Networking 3 11-27-2009 03:43 AM
not able to ping a machine but can do ssh to machine , wanna run vnc client amolgupta Linux - Software 4 07-28-2009 05:17 PM
how to watch linux client machine desktop(activities) from windows machine deepak rawat Linux - Networking 7 07-03-2006 04:59 PM
How to find an IP address from the MAC address of a remote machine ? jitz Linux - General 3 01-03-2006 07:55 AM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration