TheFueley 0 Light Poster

How would I display an IP address from /var/log/wtmp? I know I could use utmpdump, but that's not what I'm going for. I have included bits/utmp.h and can display other members of the struct, but when i try to display the ut_addr_v6 member, I get a random hex address instead. Any thoughts?

#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
using std::ios;
using std::left;

#include <utmp.h>

#include <iomanip>
using std::setw;

#include <cstdlib>
using std::exit;

#include <fstream>
using std::ifstream;
using std::ostream;

void outputLine(ostream&, const struct utmp);

int main()
{
	//open log file
	ifstream logfile("wtmp", ios::in | ios::out | ios::binary);
	
	if (!logfile)
	{
		cerr << "File Could Not Be Opened" << endl;
		exit(1);
	}
	
	struct utmp log;
	
	logfile.read(reinterpret_cast<char *>(&log), sizeof(struct utmp));
	
	while (logfile && !logfile.eof())
	{
		if (log.ut_type != 0)
			outputLine(cout, log);
		
		logfile.read(reinterpret_cast<char *>(&log), sizeof(struct utmp));
	}
	
	return 0;
}

void outputLine(ostream &output, const struct utmp record)
{
	output << "USER: " << setw(15) << left << record.ut_user
		<< "HOST: " << setw(25) << record.ut_host
		<< "IP: " << setw(15) << record.ut_addr_v6 << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.