Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-27-2004, 07:14 PM   #1
LQ Newbie
Registered: Jun 2004
Posts: 7

Rep: Reputation: 0
Reporting IP adress

With some assistance, I wrote this code in C to perform the functions of the last command. It displays some of the fields from the wtmp file. Instead of reporting the hostname, I'd like to report the IP address. From the man pages, it looks as though the host command will find IP addresses. But, do I need to use pipes to perform this action? Maybe something like read_fp = popen("host", "r"). Of course, the IP address field may be empty and in this case I can report something such as "empty" and perform no DNS lookup.

#include <utmp.h>
#include <utmpx.h>
#include <stdio.h>
#include <iostream>

#define WTMP_FILENAME "/var/log/wtmp"

int main()

        struct utmp record;
        FILE *fd = fopen(WTMP_FILENAME, "r");

        if (fd == NULL) {
                return 1;
        while (fread(&record, sizeof record, 1, fd) != 0) {
                printf("%s: %s\t: %s\t", record.ut_line, record.ut_user, record.ut_id);
        return 0;
Old 06-27-2004, 11:34 PM   #2
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
How about using the IP address in the utmp struct.
             struct utmp {
                short ut_type;              /* type of login */
                pid_t ut_pid;               /* pid of login process */
                char ut_line[UT_LINESIZE];  /* device name of tty - "/dev/" */
                char ut_id[4];              /* init id or abbrev. ttyname */
                char ut_user[UT_NAMESIZE];  /* user name */
                char ut_host[UT_HOSTSIZE];  /* hostname for remote login */
                struct exit_status ut_exit; /* The exit status of a process
                                               marked as DEAD_PROCESS. */
                long ut_session;            /* session ID, used for windowing*/
                struct timeval ut_tv;       /* time entry was made.  */
                int32_t ut_addr_v6[4];      /* IP address of remote host.  */
                char pad[20];               /* Reserved for future use.  */


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Using 2 computers with 1 Ip adress dasy2k1 Linux - Networking 3 12-01-2005 03:16 PM
no ip adress yet.... backgrounding??? ronyx Linux - Networking 2 10-03-2004 07:36 PM
View IP-adress pernestor Linux - Networking 2 09-29-2004 08:16 AM
IP adress info MACSRULE Linux - Newbie 4 04-09-2004 05:04 PM
Mac and ip adress kater0000 Linux - Networking 15 04-05-2004 06:04 PM > Forums > Non-*NIX Forums > Programming

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

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