ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have developed a custom shell for Linux. I am trying to find a way to determine a user's IP when they login from a remote machine using something like putty. What is the best way to do this? I know who will provide an IP that I could parse, but that presents a problem when two users login using the same account.
The problem is that the same user can login from multiple locations. I won't know how to differentiate them from the current active connections using the same username.
The problem is that the same user can login from multiple locations. I won't know how to differentiate them from the current active connections using the same username.
The tty command will tell you the terminal the user is attached to, so this should do the trick.
For remote users, it will display the host name or IP address; for local users, it will output something like :display (X terminal client). I think it will be empty for local console users.
Last edited by Nominal Animal; 08-18-2011 at 05:08 PM.
Reason: Replaced tabs with spaces, so you can copy-paste the command.
I have developed a custom shell for Linux. I am trying to find a way to determine a user's IP when they login from a remote machine using something like putty. What is the best way to do this? I know who will provide an IP that I could parse, but that presents a problem when two users login using the same account.
What is this shell written in and how is the connection made? sshd sets $SSH_CLIENT and $SSH_CONNECTION when logging in remotely.
Kevin Barry
For remote users, it will display the host name or IP address; for local users, it will output something like :display (X terminal client). I think it will be empty for local console users.
This is displaying blank for remote and local users for some reason. However, I see what your point is with using tty. I can always cross-reference the tty output with who to find out the IP.
The shell is written in C and the connections are via ssh.
So does the shell accept connections or does sshd? In the first case you have connection information via accept, and in the second you can use char *client = strdup(getenv("SSH_CLIENT")); before you get to the interactive stage.
Kevin Barry
So does the shell accept connections or does sshd? In the first case you have connection information via accept, and in the second you can use char *client = strdup(getenv("SSH_CLIENT")); before you get to the interactive stage.
Kevin Barry
If you don't want to depend on $SSH_CLIENT, you could also use getutxline (searching for ctermid), which might be a cleaner solution than who.
Kevin Barry
ttyname() also reports the current tty. I can use getutline() and compare ttyname to the utmp->ut_line to find all of the information I will need. Does this seem like the best approach?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.