-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicOsCmdParser.class.php
160 lines (147 loc) · 5.18 KB
/
BasicOsCmdParser.class.php
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class BasicOsCmdParser{
public function df(){
exec('df', $stdout);
$result = array();
foreach($stdout as $key => $value){
if($key != 0){
$dfLine = preg_split('#\s+#', $value);
$result[$dfLine[0]] = array('fs' => $dfLine[0],
'size' => $dfLine[1],
'used' => $dfLine[2],
'used_percent' => $dfLine[4],
'free' => $dfLine[3],
'mount_point' => $dfLine[5]
);
}
}
return $result;
}
public function loadAverage(){
exec('uptime', $stdout);
$uptimeLine = preg_split('#\s+#', $stdout[0]);
$result = array('lavg1m' => rtrim($uptimeLine[8], ','),
'lavg5m' => rtrim($uptimeLine[9], ','),
'lavg15m' => rtrim($uptimeLine[10], ','));
return $result;
}
public function uptime(){
exec('uptime', $stdout);
$uptimeLine = preg_split('#\s+#', $stdout[0]);
$result = array('uptime' => rtrim($uptimeLine[3], ','));
return $result;
}
public function ps(){
exec('ps aux', $stdout);
$result = array();
foreach($stdout as $key => $value){
if($key != 0){
$psLine = preg_split('#\s+#', $value, 11);
$result[$psLine[1]] = array('user' => $psLine[0],
'pid' => $psLine[1],
'cpu_percent' => $psLine[2],
'mem_percent' => $psLine[3],
'vsz' => $psLine[4],
'rss' => $psLine[5],
'tty' => $psLine[6],
'stat' => $psLine[7],
'start' => $psLine[8],
'time' => $psLine[9],
'command' => $psLine[10]);
}
}
return $result;
}
public function passwdInfo(){
exec('cat /etc/passwd', $stdout);
$result = array();
foreach($stdout as $user){
$parsedUser = explode(':', $user);
$result[$parsedUser[0]] = array('username' => $parsedUser[0],
'password' => $parsedUser[1],
'uid' => $parsedUser[2],
'gid' => $parsedUser[3],
'user_info' => $parsedUser[4],
'home' => $parsedUser[5],
'shell' => $parsedUser[6]);
}
return $result;
}
public function shadowInfo(){
exec('cat /etc/shadow', $stdout);
$result = array();
foreach($stdout as $user){
$parsedUser = explode(':', $user);
$result[$parsedUser[0]] = array('username' => $parsedUser[0],
'password' => $parsedUser[1],
'lastchanged' => $parsedUser[2],
'min_day' => $parsedUser[3],
'password_expire' => $parsedUser[4],
'warn' => $parsedUser[5],
'inactive' => $parsedUser[6],
'account_expire' => $parsedUser[7]);
}
return $result;
}
public function netstat(){
exec('netstat -taupen', $stdout);
$result = array();
foreach($stdout as $key => $netstat){
if($key != 0){
$parsedNetstat = preg_split('#\s+#', $netstat);
$result[] = array('proto' => $parsedNetstat[0],
'recv-q' => $parsedNetstat[1],
'send-q' => $parsedNetstat[2],
'local_address' => $parsedNetstat[3],
'remote_address' => $parsedNetstat[4],
'state' => ($parsedNetstat[0] == 'tcp' || $parsedNetstat == 'tcp6') ? $parsedNetstat[5] : '',
'user' => ($parsedNetstat[0] == 'tcp' || $parsedNetstat == 'tcp6') ? $parsedNetstat[6] : $parsedNetstat[5],
'inode' => ($parsedNetstat[0] == 'tcp' || $parsedNetstat == 'tcp6') ? $parsedNetstat[7] : $parsedNetstat[6],
'pid' => ($parsedNetstat[0] == 'tcp' || $parsedNetstat == 'tcp6') ? $parsedNetstat[8] : $parsedNetstat[7]);
}
}
return $result;
}
public function nameserver(){
exec('cat /etc/resolv.conf | grep nameserver', $stdout);
$result = array();
foreach($stdout as $ns){
$parsedNs = explode(' ', $ns);
$result[] = $parsedNs[1];
}
return $result;
}
public function hostnameFqdn(){
exec('hostname -f', $stdout);
return $stdout[0];
}
public function hostname(){
exec('hostname -f', $stdout);
return $stdout[0];
}
public function hosts(){
exec('cat /etc/hosts', $stdout);
foreach($stdout as $line){
if($line[0] != '#'){
$parsedLine = preg_split('#\s+#', $line, 2);
$names = explode(' ', $parsedLine[1]);
$result[] = array('ip' => $parsedLine[0],
'name' => $names);
}
}
return $result;
}
public function mount(){
exec('mount', $stdout);
foreach($stdout as $line){
$parsedLine = explode(' ', $line);
$attributes = explode(',', trim($parsedLine[5], '()'));
$result[] = array('name' => $parsedLine[0],
'mount_point' => $parsedLine[2],
'type' => $parsedLine[4],
'attributes' => $attributes);
}
return $result;
}
}
?>