|
Home > Archive > Unix Programming > April 2006 > How to obtain Telnet&SSH client's IP address
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to obtain Telnet&SSH client's IP address
|
|
|
| Hi all,
I wrote a free Math tool and put on our Unix boxes, and people could
log on the servers to use it. Inside the tool, I want to implement a
basic logging function to keep a record of who is using the tool:
information I want to collect is userid and IP address.
If the users log in through SSH, some Unix systems (such as AIX)
have environment variable SSH_CLIENT, which is the IP address of
the client, but some systems (like Solaris) don't have it.
If the users log in via Telnet, I don't see any environment variables
useful for IP addrss.
I was wondering if there is a easy way to obtain SSH&Telnet client's
IP address.
Btw, I am not writing client/server program, otherwise it is easy to
get IP address.
Thanks a lot.
James
| |
|
| Forgot to say I am using C for the programming. -James
| |
| Fletcher Glenn 2006-04-27, 7:55 am |
| james wrote:
> Forgot to say I am using C for the programming. -James
>
Have you tried using getpeername() ?
--
Fletcher Glenn
| |
| Barry Margolin 2006-04-27, 7:55 am |
| In article <2uT%f.69780$dW3.44560@newssvr21.news.prodigy.com>,
Fletcher Glenn <fletcher@removethisfoglight.com> wrote:
> james wrote:
>
> Have you tried using getpeername() ?
How could he use that? His stdin/stdout is connected to a pty, not the
network socket. The socket is in the sshd/telnetd process, at the other
end of the pty.
There are two typical ways to do this:
1. If the daemon sets an environment variable, like SSH_CLIENT, you can
use this.
2. The daemon will usually write the client hostname or address into the
utmp entry for your login. You can see this with the "who" command.
The C program can look up your utmp entry itself.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
|
| Thanks a lot. "utmp" solves the problem. piece of code here
struct utmp *user;
user->ut_type=USER_PROCESS;
strcpy(user->ut_line,ttyname(0)+strlen("/dev/")); /*remove /dev/ */
setutent();
user=getutline(user);
fprintf(stdout, "name is %s; pid is %d; host is %s; line is %s\n",
user->ut_user,
user->ut_pid, user->ut_host, user->ut_line);
endutent();
|
|
|
|
|