002
18.08.2006, 23:32 Uhr
J-jayz-Z
Perl Crack ala Carte (Operator)
|
Nimm dazu sys/sysinfo.h,ist aufallen Unixen vorhanden. Hier mal eine kleine Anwendung (die Klasse kannst natürlich verwenden),die ich vor ewigkeiten mal geschrieben hab
C++: |
#include <iostream> #include <sys/sysinfo.h> #include <getopt.h> using namespace std;
namespace Info { struct sysinfo_error { string method; sysinfo_error(string thrown) { method = thrown; } };
class systeminfo { public: struct sysinfo info; void systeminfo::uptime(string param = "format") { if(sysinfo(&info) == 0) { long up = info.uptime; long sec = up % 60; long min = up / 60 % 60; long hour = up / 60 / 60 / 60 % 24; long day = up / 60 / 60 / 24 % 86400; if(param == "time") cout << up << endl; if(param == "sec") cout << sec << endl;; if(param == "min") cout << min << endl; if(param == "hour") cout << hour << endl; if(param == "day") cout << day << endl; if(param == "format") cout << day << " days " << hour << " hours " << min << " minutes " << sec << " seconds" << endl; } else throw Info::sysinfo_error("uptime"); } void systeminfo::totalram() { if(sysinfo(&info) == 0) { unsigned long totalram = info.totalram; changesize( totalram ); cout << endl; } else throw Info::sysinfo_error("totalram"); } void systeminfo::freeram() { if(sysinfo(&info) == 0) { unsigned long freeram = info.freeram; changesize( freeram ); cout << endl; } else throw Info::sysinfo_error("freeram"); } void systeminfo::bufferram() { if(sysinfo(&info) == 0) { unsigned long bufferram = info.bufferram; changesize( bufferram ); cout << endl; } else throw Info::sysinfo_error("bufferram"); } void systeminfo::sharedram() { if(sysinfo(&info) == 0) { unsigned long sharedram = info.sharedram; changesize( sharedram ); cout << endl; } else throw Info::sysinfo_error("shareram"); } void systeminfo::totalswap() { if(sysinfo(&info) == 0) { unsigned long totalswap = info.totalswap; changesize( totalswap ); cout << endl; } else throw Info::sysinfo_error("totalswap"); } void systeminfo::freeswap() { if(sysinfo(&info) == 0) { unsigned long freeswap = info.freeswap; changesize( freeswap ); cout << endl; } else throw Info::sysinfo_error("freeswap"); } void systeminfo::procs() { if(sysinfo(&info) == 0) { unsigned short procs = info.procs; cout << " " << procs << endl; } else throw Info::sysinfo_error("procs"); } void systeminfo::load() { if(sysinfo(&info) == 0) { cout << info.loads[0] << ", " << info.loads[1] << ", " << info.loads[2] << endl; } else throw Info::sysinfo_error("loads"); } void systeminfo::help(string name) { cout << "Usage: "<< name << "\t[-h] for this help message" << endl; cout << "\t\t\t[-s] for swap information" << endl; cout << "\t\t\t[-r] for RAM information" << endl; cout << "\t\t\t[-p] for your currently uptime" << endl; cout << "\t\t\t[-p] for the number of the current running processes" << endl; cout << "\t\t\t[-l] shows the load average for the last 1, 5 and 15 minutes" << endl; cout << "\t\t\t[-a] for the whole information" << endl; } void changesize(unsigned long ram) { if(ram < 1024) cout << ram << " bytes"; else if(ram > 1024 && ram < 1048576) cout << ram / 1024 << " kb"; else if(ram > 1048576 && ram < 1073741824) cout << ram / 1048576 << "," << (ram % 1048576) / 1024 << " mb"; else if(ram > 1073741824) cout << ram / 1073741824 << "," << (ram % 1073741824) / 1048576 << " gb"; } }; }
int main(int argc, char* argv[]) { Info::systeminfo* info = new Info::systeminfo; try { int option, akt_optind, option_index = 0; struct option long_options[] = { { "help", 0, 0, 0 }, { "swap", 0, 0, 0 }, { "ram", 0, 0, 0 }, { "uptime", 0, 0, 0 }, { "proc", 0, 0, 0 }, { "load", 0, 0, 0 }, { "all", 0, 0, 0 }, }; while(1) { option = getopt_long( argc, argv, "hsrupla", long_options, &option_index); if(option == EOF) break; switch( option ) { case 'h': info->help( argv[0] ); break; case 'r': cout << "total RAM: "; info->totalram(); cout << "free RAM: "; info->freeram(); cout << "buffered RAM: "; info->bufferram(); cout << "shared RAM: "; info->sharedram(); break; case 's': cout << "total SWAP: "; info->totalswap(); cout << "free SWAP: "; info->freeswap(); break; case 'u': cout << "Uptime: "; info->uptime(); break; case 'l': cout << "Load average: "; info->load(); break; case 'p': cout << "Running process: "; info->procs(); break; case 'a': cout << "Uptime: "; info->uptime(); cout << "total RAM: "; info->totalram(); cout << "free RAM: "; info->freeram(); cout << "buffered RAM: "; info->bufferram(); cout << "shared RAM: "; info->sharedram(); cout << "total SWAP: "; info->totalswap(); cout << "free SWAP: "; info->freeswap(); cout << "Load average: "; info->load(); cout << "Running process: "; info->procs(); break; } } } catch(Info::sysinfo_error err) { cerr << "Error in method " << err.method << " !" << endl; } return 0; }
|
-- perl -Mstrict -Mwarnings -e 'package blub; sub new { bless {} } sub bar {my $self=shift; $self->{bla}="66756e2d736f66742e6465"; return $self->{bla};} my $foo=blub->new();print "Hallo ";print pack("H*",$foo->bar()); print "\n"' |