LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-18-2011, 02:02 PM   #1
linuxdev817
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Rep: Reputation: Disabled
Custom Shell - Get Remote User's IP


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.
 
Old 08-18-2011, 03:01 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Different logins for the same user will be on different pty (/dev/pts/*) devices.

who
user1 :0 2011-06-08 12:30
user1 pts/1 2011-06-08 12:31 (:0.0)
user1 pts/3 2011-08-18 08:43 (billybob.example.net)
user1 pts/5 2011-08-17 14:05 (:0.0)
user1 pts/2 2011-08-18 08:42 (billybob.example.net)
user1 pts/4 2011-08-17 14:03 (:0.0)
 
Old 08-18-2011, 03:15 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
the 'ss' command might help here.
 
Old 08-18-2011, 03:34 PM   #4
linuxdev817
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-18-2011, 05:06 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by linuxdev817 View Post
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.
Code:
#!/bin/bash
export LANG=C
export LC_ALL=C
tty=$(tty)
who | awk -v "tty=$tty" '
        BEGIN {
                sub(/^\/dev\/+/, "", tty)
        }
        ($2 == tty) {
                remote = $6
                sub(/^\(+/, "", remote)
                sub(/\)+$/, "", remote)
                print remote
        }
'
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.
 
Old 08-18-2011, 10:46 PM   #6
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
ssh sessions in log files

Remember you have log files that you may be able to use. On my debian system the ssh login information is kept in /var/log/auth.log

Code:
Aug 18 23:47:10 monarch sshd[20923]: Accepted password for root from 127.0.0.1 port 33438 ssh2
 
Old 08-19-2011, 08:19 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by linuxdev817 View Post
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
 
Old 08-19-2011, 09:03 AM   #8
linuxdev817
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Nominal Animal View Post
The tty command will tell you the terminal the user is attached to, so this should do the trick.
Code:
#!/bin/bash
export LANG=C
export LC_ALL=C
tty=$(tty)
who | awk -v "tty=$tty" '
        BEGIN {
                sub(/^\/dev\/+/, "", tty)
        }
        ($2 == tty) {
                remote = $6
                sub(/^\(+/, "", remote)
                sub(/\)+$/, "", remote)
                print remote
        }
'
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.
 
Old 08-19-2011, 09:09 AM   #9
linuxdev817
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
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
The shell is written in C and the connections are via ssh.
 
Old 08-19-2011, 09:16 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by linuxdev817 View Post
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
 
Old 08-19-2011, 09:18 AM   #11
linuxdev817
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
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
sshd accepts the connections.
 
Old 08-19-2011, 09:29 AM   #12
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by linuxdev817 View Post
sshd accepts the connections.
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
 
Old 08-19-2011, 10:33 AM   #13
linuxdev817
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
I am a little confused because:

ctermid() = /dev/tty
system("tty") = /dev/pts/0

Why are these different?
 
Old 08-19-2011, 10:50 AM   #14
linuxdev817
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
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?
 
1 members found this post helpful.
Old 08-19-2011, 11:05 AM   #15
linuxdev817
LQ Newbie
 
Registered: Aug 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Code:
char* my_tty = ttyname(0);
if(strstr(my_tty, "/dev/") != NULL)
{
   my_tty += 5;
}

struct utmp* ut_entry;
while((ut_entry = getutent()) != NULL)
{
    if(strstr(ut_entry->ut_line, my_tty) != NULL)
    {
    	printf("host: %s\n id: %s\n line: %s\n npid: %d\n session: %d\n type: %d\n user: %s\n\n", ut_entry->ut_host, ut_entry->ut_id, ut_entry->ut_line, ut_entry->ut_pid, ut_entry->ut_session, ut_entry->ut_type, ut_entry->ut_user);
    }
}
 
1 members found this post helpful.
  


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] Custom Shell output help CapnStank Programming 1 10-18-2010 11:08 AM
How to call Shell Script on a remote server with remote servers env variables need Linux - Server 1 10-14-2009 08:37 PM
Firefox custom chrome and -remote MardukKurios Linux - Software 0 11-22-2005 04:39 PM
Custom shell menu? Darkfin Linux - Newbie 1 04-24-2005 09:37 AM

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

All times are GMT -5. The time now is 01:46 AM.

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