-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscoverCommand.php
220 lines (190 loc) · 7.75 KB
/
DiscoverCommand.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
namespace App\Commands;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use LaravelZero\Framework\Commands\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DiscoverCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'netfind';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Discover network devices';
protected static $requirements = [
'nmcli', 'sed', 'ip', 'nmap'
];
public function __construct()
{
parent::__construct();
$this->addArgument('device',InputArgument::IS_ARRAY,
"A space separated list of network devices to discover"
);
$this->addOption('delay','d',InputOption::VALUE_REQUIRED,
"Time in milliseconds between discoveries",1000
);
}
protected static function getMissingRequirements():?array {
$missing = [];
foreach (self::$requirements as $requirement) {
$exists = `command -v $requirement`;
if (!$exists) {
$missing[] = $requirement;
}
}
return empty($missing) ? null : $missing;
}
protected static function getExistingNetworks():array {
$interfaces = explode("\n",trim(`nmcli device status | sed "1 d"`));
array_walk($interfaces,function(&$item) {
$item = explode("|",preg_replace("/\s{2,}/","|",trim($item)));
if ($item[1] == 'loopback') {
$item = null;
}
});
return array_filter($interfaces);
}
protected function selectInterfaces():array {
$interfaces = self::getExistingNetworks();
if ($selected_interfaces = array_unique($this->argument('device'))) {
foreach($selected_interfaces as $interface) {
if (!in_array($interface,Arr::pluck($interfaces,0))) {
return [];
}
}
} else {
if ($this->input->isInteractive()) {
$this->title("Select Networks");
}
$choices = array_map(function ($interface) {
return preg_replace("/\s+/", " ", sprintf("%s %s (%s; %s)",
$interface[0],
$interface[3] !== '--'
? " [{$interface[3]}]"
: "",
$interface[1],
$interface[2]
));
}, $interfaces);
$selected_interfaces = $this->choice(
"Select one or more network interfaces, separated by comma",
$choices,
array_search('eth0', Arr::pluck($interfaces, 0)) ?: 0,
10,
true
);
array_walk($selected_interfaces, function (&$interface) use ($interfaces, $choices) {
$interface = $interfaces[array_search($interface, $choices)][0];
});
}
return $selected_interfaces;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$missingRequirements = self::getMissingRequirements();
if ($missingRequirements) {
$this->error(sprintf(
"Netfind requires %s to be installed in order to run properly. Aborting.",
$missingRequirements[0]
));
return 1;
}
$interfaces = $this->selectInterfaces();
if (empty($interfaces)) {
$this->error(sprintf("There was no valid interface selected. Aborting."));
return 1;
}
$this->info(sprintf("Selected interface(s): %s.", join(', ',$interfaces)));
$devices = [];
while (true) {
foreach ($devices as &$device) {
$device["status"] = sprintf("<error>down</error>");
}
if (trim(`whoami`) !== 'root') {
$this->warn("To be able to collect mac address and manufacturer info this program must run as root!");
sleep(10);
}
$routers = [];
foreach ($interfaces as $interface) {
$routers[$interface] = explode("\n",
`route -n | grep "$interface" | awk '{print $2}' | grep -v "0.0.0.0"`
);
$matches = null;
if (preg_match('/inet ([0-9\.\/]+)/', trim(`ip address show dev $interface | grep inet`), $matches)) {
$results = explode("\n", trim(`nmap -P -sP {$matches[1]}`));
while (!str_starts_with(current($results), "Nmap done:")) {
if (empty(current($results))) {
exit;
}
if (str_starts_with(current($results), "Starting Nmap")) {
next($results);
continue;
}
preg_match("/Nmap scan report for" .
"\s?(.*) \(?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{0,3}\.[0-9]{1,3})\)?/",
current($results), $matches);
$hostname = $matches[1];
$ip = $matches[2];
$snmp = trim(`nmap -sU -p 161 $ip | grep 161/udp | awk '{print $2}'`) === 'open';
$router = in_array($ip,$routers[$interface]);
next($results);
preg_match("/Host is (\w+)/", current($results), $matches);
$status = $matches[1];
next($results);
if (preg_match('/MAC Address: ([0-9A-F:]+) \((.*)\)/', current($results), $matches)) {
$mac = $matches[1];
$manufacturer = $matches[2];
next($results);
$key = $mac;
} else {
$mac = "Unknown";
$manufacturer = "Unknown";
$key = $ip;
}
if (!array_key_exists($mac, $devices)) {
$devices[$key] = [
"mac" => $mac,
"discovered" => Carbon::now()->format('Y-m-d H:i:s'),
"status" => $status,
"interface" => $interface,
"ip" => $ip,
"hostname" => $hostname,
"manufacturer" => $manufacturer,
"snmp" => $snmp,
"router" => $router
];
} else {
$devices[$key]["status"] = $status == "up"
? $status
: sprintf("<error>%s</error>",$status);
$devices[$key]["ip"] = $ip;
$devices[$key]["hostname"] = $hostname;
$devices[$key]["snmp"] = $snmp;
}
}
}
}
system('clear');
$this->info(Carbon::now()->format('Y-m-d H:i:s'),OutputInterface::VERBOSITY_VERBOSE);
$this->table([
"Mac Address","Discovered at","Status","Interface","Ip Address","Hostname","Manufacturer","SNMP","Router"
],$devices);
$this->info("Press CTRL+C to terminate execution");
usleep($this->option('delay') * 1000);
}
}
}