1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
#include "sysinfo.h"
// ------------------------------------------------------------------------------------------------
SysInfo::SysInfo(){}
// ------------------------------------------------------------------------------------------------
SysInfo::~SysInfo(){}
// ------------------------------------------------------------------------------------------------
const QString SysInfo::getInfo(const QString& infoName){
qxtLog->debug() << "[sysinfo] requested " << infoName;
if (infoName == QString("mac"))
return getMACAddress();
else if (infoName == QString("ip"))
return getIPAddress();
else if (infoName == QString("all"))
return getAllInfos();
else if (infoName == QString("mbserial"))
return getMainboardSerial();
else if (infoName == QString("json"))
return getNames();
/* unknown code */
qxtLog->debug() << "[sysinfo] unknown requested";
return "info_error";
}
// ------------------------------------------------------------------------------------------------
const QString SysInfo::getMACAddress(){
// Returns MAC address of eth0 for now
QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0"));
if (!qni.isValid()){
qxtLog->debug() << "[sysinfo] MAC Address: No interface matching \"eth0\" found.";
return "no_eth0";
}
//eth0_index = qni.index();
return qni.hardwareAddress();
}
// ------------------------------------------------------------------------------------------------
const QString SysInfo::getIPAddress(){
// Again for eth0 only at the moment.
// TODO: this doesn't quite work yet...
QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0"));
QList<QHostAddress> addrlist = qni.allAddresses();
// This is does not return the right IP atm...
foreach(QHostAddress addr, addrlist){
if (addr.protocol() == QAbstractSocket::IPv4Protocol && addr != QHostAddress::LocalHost){
return addr.toString();
}
}
// still here?
qxtLog->debug() << "[sysinfo] IP Address: ip_error";
return "ip_error";
}
// ------------------------------------------------------------------------------------------------
const QByteArray SysInfo::getNames(){
QVariantMap foo;
foo.insert("name", "foo");
foo.insert("type", 123);
QVariantMap fooo;
fooo.insert("name", "boo");
fooo.insert("type", 321);
QVariantList jsonV;
jsonV << foo << fooo;
QJson::Serializer serializer;
QByteArray json = serializer.serialize(jsonV);
qxtLog->debug() << json;
return json;
}
// ------------------------------------------------------------------------------------------------
QString SysInfo::getAllInfos(){
QVariantMap infos;
infos.insert("mac", getMACAddress());
infos.insert("ip", getIPAddress());
infos.insert("whoami", getScriptOutput("whoami"));
//infos.insert("pwd", getScriptOutput("pwd"));
//QJson::Serializer serializer;
QByteArray json = serializer.serialize(infos);
qxtLog->debug() << json;
return json;
}
// ------------------------------------------------------------------------------------------------
const QString SysInfo::getMainboardSerial(){
QString out = "";
struct sysfs_class_device *class_device = sysfs_open_class_device("dmi","id");
struct dlist *attrlist = sysfs_get_classdev_attributes(class_device);
struct sysfs_device *device = sysfs_get_classdev_device(class_device);
if(attrlist != NULL){
struct sysfs_attribute *attr = NULL;
dlist_for_each_data(attrlist, attr, struct sysfs_attribute) {
QVariantMap a;
if(QString(attr->name) == QString("board_serial")){
out = QString(attr->value);
}
}
qxtLog->debug()<< "[sysinfo] Mainboard Serial: " + out;
return out;
}
qxtLog->debug()<< "[sysinfo] Mainboard Serial: attrlist is null! return: mainboard_serial_error";
sysfs_close_class_device(class_device);
return "mainboard_serial_error";
}
// ------------------------------------------------------------------------------------------------
QString SysInfo::getScriptOutput(QString cmd)
{
QProcess *process = new QProcess();
qxtLog->debug()<<"[sysinfo] Script Output: try to open: "<<cmd;
process->start(cmd, QIODevice::ReadOnly);
if (!process->waitForStarted() )
qxtLog->debug()<<"[sysinfo] Script Output: process couldn't get opened";
QString output;
process->waitForFinished();
QTextStream *txt_stream = new QTextStream(process);
while(!txt_stream->atEnd() )
{
qxtLog->debug()<<"[sysinfo] Script Output: read output: ";
QString tmp_str = txt_stream->readLine();
output += tmp_str;
qxtLog->debug()<< "[sysinfo] Script Output: " <<tmp_str;
}
qxtLog->debug()<<"[sysinfo] Script Output: process finished: ";
return output;
}
|