Display IP Address from /var/log/wtmp

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 26
Reputation: TheFueley is an unknown quantity at this point 
Solved Threads: 0
TheFueley's Avatar
TheFueley TheFueley is offline Offline
Light Poster

Display IP Address from /var/log/wtmp

 
0
  #1
Mar 26th, 2008
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?

  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. using std::cerr;
  5. using std::ios;
  6. using std::left;
  7.  
  8. #include <utmp.h>
  9.  
  10. #include <iomanip>
  11. using std::setw;
  12.  
  13. #include <cstdlib>
  14. using std::exit;
  15.  
  16. #include <fstream>
  17. using std::ifstream;
  18. using std::ostream;
  19.  
  20. void outputLine(ostream&, const struct utmp);
  21.  
  22. int main()
  23. {
  24. //open log file
  25. ifstream logfile("wtmp", ios::in | ios::out | ios::binary);
  26.  
  27. if (!logfile)
  28. {
  29. cerr << "File Could Not Be Opened" << endl;
  30. exit(1);
  31. }
  32.  
  33. struct utmp log;
  34.  
  35. logfile.read(reinterpret_cast<char *>(&log), sizeof(struct utmp));
  36.  
  37. while (logfile && !logfile.eof())
  38. {
  39. if (log.ut_type != 0)
  40. outputLine(cout, log);
  41.  
  42. logfile.read(reinterpret_cast<char *>(&log), sizeof(struct utmp));
  43. }
  44.  
  45. return 0;
  46. }
  47.  
  48. void outputLine(ostream &output, const struct utmp record)
  49. {
  50. output << "USER: " << setw(15) << left << record.ut_user
  51. << "HOST: " << setw(25) << record.ut_host
  52. << "IP: " << setw(15) << record.ut_addr_v6 << endl;
  53. }
Last edited by TheFueley; Mar 26th, 2008 at 11:53 pm.
Rivera
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC