473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Reading files in /var/spool/rwho/whod.*

Hello,

I'm trying to read the binary files under /var/spool/rwho/ so I'm wondering if
anyone has done that before or could give me some clues on how to read those
files. I've tried to use the binascii module without any luck.

Best regards,
-fredrik-normann-
Jul 19 '05 #1
3 1708
Fredrik Normann wrote:
Hello,

I'm trying to read the binary files under /var/spool/rwho/ so I'm
wondering if anyone has done that before or could give me some clues on
how to read those files. I've tried to use the binascii module without
any luck.


A friend of mine made this C-program, that does the trick :)

#include <protocols/rwhod.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>

#include <time.h>
#include <sys/time.h>

#include <sys/types.h>
#include <netinet/in.h>
#include <inttypes.h>

#ifdef SOL
#include <stddef.h>
#else
#include <linux/stddef.h>
#endif

int main (int argc, char **argv)
{

char *filename, *binary;
FILE *fp;
struct whod entry;
struct outmp who;
size_t ret;
size_t cnt = 0;
char *user = (char*) malloc (8);
char *timestr = (char*) malloc (512);
char *hostname = (char*) malloc (512);
int hostlen, ulen;
size_t prefix, we_len;

hostlen = 0;
ulen = 0;

struct timeval now;
struct tm *tptr;
time_t t;

binary = *argv;
ret = gettimeofday (&now, 0);
if (ret < 0) {
perror ("Error getting time of day");
exit (2);
}

if (argc < 2) {
printf ("%s: ERROR, no whod file specified\n", binary);
exit (1);
}

filename = *(argv + 1);

fp = fopen (filename, "r");
if (fp == NULL) {
perror ("Error opening file");
exit (3);
}

#ifdef SOL
ret = fread (&entry, 1, sizeof (entry), fp);
#else
ret = fread_unlocked (&entry, 1, sizeof (entry), fp);
#endif
if (ret < 0) {
perror ("Error reading file");
exit (4);
}

prefix = offsetof (struct whod, wd_we) / sizeof (struct whoent);
we_len = ret / sizeof (struct whoent) - prefix;

/* Find lengths of strings */
for (cnt = 0; cnt < we_len; cnt++) {
who = entry.wd_we[cnt].we_utmp;
strncpy (user, who.out_name, 8);
if (strlen (user) > ulen)
ulen = strlen (user);
if ((strlen (entry.wd_hostname) + strlen (who.out_line) + 1) > hostlen)
hostlen = strlen (entry.wd_hostname) + strlen (who.out_line) + 1;
}

for (cnt = 0; cnt < we_len; cnt++) {
who = entry.wd_we[cnt].we_utmp;
strncpy (user, who.out_name, 8);
strncpy (hostname, entry.wd_hostname, 512);
strcat (hostname, ":");
strncat (hostname, who.out_line, 8);

t = now.tv_sec - ntohl (entry.wd_we[cnt].we_idle);

entry.wd_we[cnt].we_idle = ntohl (entry.wd_we[cnt].we_idle) / 60;

printf ("%-*s %-*s",
ulen, user,
hostlen, hostname);
if (entry.wd_we[cnt].we_idle >= 0) {
tptr = localtime (&t);
strftime (timestr, 512, "%a %b %d %H:%m", tptr);

printf (" %s ", timestr);
printf ("%5d:%02d",
entry.wd_we[cnt].we_idle / 60,
entry.wd_we[cnt].we_idle % 60);
}
printf ("\n");
}

fclose (fp);
}
Jul 19 '05 #2
On Mon, 27 Jun 2005 13:26:12 +0200, Fredrik Normann <fr*****@ifi.uio.no>
declaimed the following in comp.lang.python:
Hello,

I'm trying to read the binary files under /var/spool/rwho/ so I'm wondering if
anyone has done that before or could give me some clues on how to read those
files. I've tried to use the binascii module without any luck.
Have you looked at the struct module?

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #3
Dennis Lee Bieber wrote:
On Mon, 27 Jun 2005 13:26:12 +0200, Fredrik Normann <fr*****@ifi.uio.no>
declaimed the following in comp.lang.python:

Hello,

I'm trying to read the binary files under /var/spool/rwho/ so I'm wondering if
anyone has done that before or could give me some clues on how to read those
files. I've tried to use the binascii module without any luck.


Have you looked at the struct module?


Thanks for the tip. A friend of mine helped me with making this small script:
#!/usr/bin/env python

"""whod file parser.

Made by: Igor V. Rafienko

This tiny script tries to grock whod files.
"""
from struct import calcsize, unpack, pack
from socket import ntohl
import time

outmp_format = '8s8s4s'
whoent_format = '%ds4s' % calcsize(outmp_format)
almost_whod = 'cc2s4s4s32s12s4s'
def make_string(s):
"""Stupid C. Chop the string off at the first '\0'."""

index = s.find('\0')
if index != -1:
return s[:index]
# fi
# end make_string
def make_time(seconds):
"""Convert from seconds since Epoch to ISO8601."""

return time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(seconds))
# end make_time
def make_int(binary):
"""Convert binary from network representation to host int."""

assert len(binary) == 4, "ints are 4 bytes long here"

if calcsize("i") == 4:
return ntohl(unpack("i", binary)[0])
elif calcsize("l") == 4:
return ntohl(unpack("l", binary)[0])
else:
raise "Dammit! no suitable integral type"
# fi
# end make_int
def parse_one_outmp(binary_data):
"""Parse an outmp struct."""

out_line, out_name, out_time = unpack(outmp_format, binary_data)
out_time = make_int(out_time)

return out_line, out_name, out_time
# end parse_one_outmp
def parse_one_whoent(binary_data):
"""Parse a whoent struct."""

outmp_part, we_idle = unpack(whoent_format, binary_data)

we_idle = make_int(we_idle)
out_line, out_name, out_time = parse_one_outmp(outmp_part)

return out_line, out_name, out_time, we_idle
# end parse_one_whoent
def parse_one_file(binary_data):
"""Parse the entire thing."""

# First we parse everything, except for the whoent-array
prefix = unpack(almost_whod, binary_data[:calcsize(almost_whod)])

print "prefix has %d elemenets" % len(prefix)
print "wd_vers:", ord(prefix[0])
print "wd_type:", ord(prefix[1])
print "wd_fill:", make_string(prefix[2])
print "wd_sendtime:", make_time(make_int(prefix[3]))
print "wd_recvtime:", make_time(make_int(prefix[4]))
print "wd_host: ", make_string(prefix[5])
load = prefix[6]
print "wd_load avg: %d, %d, %d" % tuple([make_int(x) for x in
(load[:4], load[4:8], load[8:])])
print "wd_boottime", make_time(make_int(prefix[7]))

sz = calcsize(whoent_format)
array_data = binary_data[calcsize(almost_whod):]
assert len(array_data) % sz == 0, "Aiee! corrupt chunk?"
whoent_chunks = [ array_data[sz*x:sz*(x+1)] for x in range(len(array_data)
/ sz) ]
print "%d whoent chunks" % len(whoent_chunks)

for out_line, out_name, out_time, we_idle in [parse_one_whoent(x)
for x in whoent_chunks]:
print "\tout_line:", make_string(out_line)
print "\tout_name:", make_string(out_name)
print "\tout_time:", make_time(out_time)
print "\twe_idle:", we_idle
# od
# end parse_one_file
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thomas R. Hummel | last post by:
Hello, I have a table which has a few million records. It has an IDENTITY column that serves as the primary key. In a part of our application here, a previous record may need to be copied as a...
1
by: david_0 | last post by:
What causes the query optimizer to choose a table spool\lazy spool action in the execution plan? The explanation of "optimize rewinds" makes little sense because my query never comes back to that...
2
by: jimmyfishbean | last post by:
Hi, I am using VB6, SAX (implementing IVBSAXContentHandler). I need to extract binary encoded data (images) from large XML files and decode this data and generate the appropriate images onto...
0
by: tvishwaprasad | last post by:
Hi Please help me to read the spool files in windows platform. From shekhar
2
by: sanjeet | last post by:
I am trying to load some data into CSV files but I get this error message: ORA-00922, missing or invalid option. set echo off newpage 0 space 0 pagesize 0 feed off head off trimspool on spool...
1
by: Kesavan | last post by:
I install apache2 in /usr/local/apache2 and install php5 by ./configure --with-apxs2=/usr/local/apache2/bin/ apxs PHP is successfully installed in my system. But now my .php files inside...
1
by: orajit | last post by:
I Have created following PL/sql blok DECLARE vl_n_cnt NUMBER:=0; vl_max_date varchar2(100); CURSOR c1 IS SELECT CREATED_DTM,EVENT_DTM,ACCOUNT_NUM,EVENT_COST_MNY,EVENT_TYPE_ID ...
1
by: akaley | last post by:
HI.. iam using Spool ..for loading the data into file from select statement.. iam facing some problems...Please give me u r valuable suggestion.. This is the sample code used by me.. set...
1
by: Atif | last post by:
Hi, I want to read the spool file's contents not pages but Text Data. Can any body plz help me in this regards Thanks in advance Best Regards Atif
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.