Escape net-snmp unformatted strings, try 2 (#13584)

* Escape net-snmp unformatted strings, try 2

* Add some tests
This commit is contained in:
Tony Murray
2021-11-30 00:12:42 -06:00
committed by GitHub
parent 4a99b798b5
commit 9d2633bf09
3 changed files with 9 additions and 2 deletions

View File

@@ -132,7 +132,7 @@ class SnmpResponse
if (Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
// unformatted string from net-snmp, remove extra escapes
$values[$oid] = stripslashes(trim($value, "\\\" \n\r"));
$values[$oid] = trim(stripslashes($value), "\" \n\r");
} else {
$values[$oid] = trim($value);
}

View File

@@ -46,7 +46,7 @@ class SnmpFetch extends LnmsCommand
$spec = $this->argument('device spec');
$device_ids = Device::query()->when($spec !== 'all', function (Builder $query) use ($spec) {
return $query->where('device_id', $spec)
->orWhere('hostname', 'regexp', $spec);
->orWhere('hostname', 'regexp', "^$spec$");
})->pluck('device_id');
if ($device_ids->isEmpty()) {

View File

@@ -47,6 +47,13 @@ class SnmpResponseTest extends TestCase
$this->assertEquals(['' => 'IF-MIB::ifDescr'], $response->values());
$this->assertEquals('IF-MIB::ifDescr', $response->value());
$this->assertEquals(['IF-MIB::ifDescr'], $response->table());
// unescaped strings
$response = new SnmpResponse("Q-BRIDGE-MIB::dot1qVlanStaticName[1] = \"default\"\nQ-BRIDGE-MIB::dot1qVlanStaticName[9] = \"\\\\Surrounded\\\\\"");
$this->assertTrue($response->isValid());
$this->assertEquals('default', $response->value());
$this->assertEquals(['Q-BRIDGE-MIB::dot1qVlanStaticName[1]' => 'default', 'Q-BRIDGE-MIB::dot1qVlanStaticName[9]' => '\\Surrounded\\'], $response->values());
$this->assertEquals(['Q-BRIDGE-MIB::dot1qVlanStaticName' => [1 => 'default', 9 => '\\Surrounded\\']], $response->table());
}
public function testMultiLine(): void