LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-27-2016, 09:17 PM   #1
luofeiyu
Member
 
Registered: Aug 2015
Posts: 237

Rep: Reputation: Disabled
How to display the content of /var/log/wtmp without error codes?


cat /var/log/wtmp
:0:0root:0*H�W�J
*H�W��
~~~shutdown3.16.0-4-amd64-H�W��~~~reboot3.16.0-4-amd64¸�W#L�tty1tty1�\H�W���tty1tty1LOGIN�\H�W��5~~~runlevel3.16.0-4-amd64jH�W��,:0:0debian8:0wH�W�� :0:0debian8:0wH�W

j:0:0root:0�H�W��r

How to display the content of /var/log/wtmp without error codes?
 
Old 08-27-2016, 09:21 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
last, man last
 
Old 08-27-2016, 09:28 PM   #3
luofeiyu
Member
 
Registered: Aug 2015
Posts: 237

Original Poster
Rep: Reputation: Disabled
It can be displayed with `last` command.
If i want to write a program to make the string to display normally,how to do?
:0:0root:0*H�W�J
*H�W��
~~~shutdown3.16.0-4-amd64-H�W��~~~reboot3.16.0-4-amd64¸�W#L�tty1tty1�\H�W���tty1tty1LOGIN�\H�W��5~~~runlevel3.16.0-4-amd64jH�W��,:0:0debian8:0wH�W�� :0:0debian8:0wH�W

j:0:0root:0�H�W��r
 
Old 08-27-2016, 10:14 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Well, that wasn't the question, but according to man wtmp:

Code:
The file is a sequence of utmp structures, declared as follows in <utmp.h> (note that this is  only  one
       of several definitions around; details depend on the version of libc)
So you will need to write your code to work with utmp structures and display the results.

Begin with...
Code:
#include <utmp.h>
 
Old 08-28-2016, 08:57 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Here's my ~10 year old code to do that.
Code:
#Released to the public domain 28 Aug, 2016. R. K. Nichols
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <utmp.h>
#include <time.h>

#include <arpa/inet.h>

#define UTMP "/var/run/utmp"
#define WTMP "/var/log/wtmp"

char *types[] ={
    "EMPTY", "RUNLVL", "BOOT", "NEWTIME",
    "OLDTIME", "INIT", "LOGIN", "USER",
    "DEAD", "ACCT"
};

int
main(int argc, char *argv[])
{
    int r1, r2;
    struct in_addr inet;
    char *fname;
    FILE *uf;
    struct utmp u;
    struct tm *t;
    time_t tseconds;
    char mbuf[256];
    extern char *tzname[2];
    extern long timezone;

    if(argc > 1) {
	if(strcmp(argv[1], "-u") == 0)  fname = UTMP;
	else if(strcmp(argv[1], "-w") == 0)  fname = WTMP;
	else  fname = argv[1];
    }
    else  fname = WTMP;
    uf = fopen(fname, "r");
    if(uf == NULL) {
	snprintf(mbuf, sizeof(mbuf), "ERROR[%s]: %s", argv[0], fname);
	perror(mbuf);
	exit(1);
    }
    puts("TYPE      PID DEVICE     IID  USER      TIME                          FROM");
    while(fread((void*)&u, sizeof(u), 1, uf) == 1) {
	switch(u.ut_type) {
	case RUN_LVL:
	    r1 = u.ut_pid>>8 & 255;
	    r2 = u.ut_pid & 255;
	    printf("%-7s  %c->%c ", types[u.ut_type],
		   r1 ? r1 : ' ', r2 ? r2 : ' ');
	    break;
	default:
	    printf("%-7s %5d ", 
	       (u.ut_type < sizeof(types)/sizeof(types[0])) ? types[u.ut_type] : "??",
		   u.ut_pid);
	}
	tseconds = u.ut_tv.tv_sec;
	printf("%-10.*s %-4.4s %-8.*s ",
	       UT_LINESIZE, u.ut_line, u.ut_id, UT_NAMESIZE, u.ut_user);
	t = localtime(&tseconds);
	if(t->tm_isdst > 0)  timezone -= 3600;
	printf("%2d/%02d %02d:%02d:%02d.%03ld %s (%s%02ld%02ld)",
	       t->tm_mon+1, t->tm_mday,
	       t->tm_hour, t->tm_min, t->tm_sec, ((long)u.ut_tv.tv_usec + 500)/1000,
	       tzname[t->tm_isdst > 0],
	       (timezone < 0) ? "" : "-", timezone/3600, (timezone/60)%60);
	if(u.ut_addr) {
	    inet.s_addr = u.ut_addr;
	    printf(" %s\n", inet_ntoa(inet));
	}
	else  putchar('\n');
    }
    if(ferror(uf)) {
	perror(fname);
	exit(1);
    }
    return(0);
}

Last edited by rknichols; 08-28-2016 at 09:00 AM. Reason: Add public domain notice
 
  


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
/var/log/wtmp mrant Linux - Security 4 07-14-2013 08:41 PM
/var/log/wtmp vs. /var/run/utmp masenko703 Linux - Newbie 4 10-05-2012 03:41 AM
/var/log/wtmp Permissions berzerked Red Hat 3 10-05-2011 02:49 PM
/var/log/wtmp ??? fatrandy13 Linux - Software 3 12-13-2004 09:07 AM
/var/log/wtmp praveenv Linux - Newbie 5 08-23-2004 02:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:37 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