Skip to content

SNMP::__construct() fails to resolve hostname with warning "Unknown failure while resolving hostname" #20752

Open

Description

@dshovchko

Description

The following code:

<?php

$session = new SNMP(SNMP::VERSION_2C, '127.0.0.1', 'public');
var_dump($session);

Resulted in this output:

PHP Warning:  SNMP::__construct(): Unknown failure while resolving '127.0.0.1' in /tmp/snmp_create_session.php on line 3
object(SNMP)#1 (8) {
  ["info"]=>
  array(3) {
    ["hostname"]=>
    string(0) ""
    ["timeout"]=>
    int(-1)
    ["retries"]=>
    int(-1)
  }
  ["max_oids"]=>
  NULL
  ["valueretrieval"]=>
  int(0)
  ["quick_print"]=>
  bool(false)
  ["enum_print"]=>
  bool(false)
  ["oid_output_format"]=>
  int(0)
  ["oid_increasing_check"]=>
  bool(false)
  ["exceptions_enabled"]=>
  int(0)
}

But I expected this output instead:

object(SNMP)#1 (8) {
  ["info"]=>
  array(3) {
    ["hostname"]=>
    string(9) "127.0.0.1"
    ["timeout"]=>
    int(-1)
    ["retries"]=>
    int(-1)
  }
  ["max_oids"]=>
  NULL
  ["valueretrieval"]=>
  int(0)
  ["quick_print"]=>
  bool(false)
  ["enum_print"]=>
  bool(false)
  ["oid_output_format"]=>
  int(0)
  ["oid_increasing_check"]=>
  bool(true)
  ["exceptions_enabled"]=>
  int(0)
}

The problem reproduces on 8.5.1, 8.4.16, and 8.4.15.

PHP Version

PHP 8.4.15 (cli) (built: Dec 11 2025 18:42:43) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.15, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.15, Copyright (c), by Zend Technologies

PHP 8.5.1 (cli) (built: Dec 21 2025 12:04:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.1, Copyright (c), by Zend Technologies

Operating System

Ubuntu 24.04.3 LTS

Activity

  1. ndossche commented on Dec 21, 2025

    @ndossche
    Member

    Doesn't reproduce on my system.
    Does this reproduce consistently for you? Do you have any special networking configuration that may interfere with this?

  2. devnexen commented on Dec 21, 2025

    @devnexen
    Member

    I do not have my usual ubuntu machine with me, however your script has the expected output on my mac with php 8.5.1.
    So for whatever reason, netsnmp_session_init() fails for you, I may have a look next week unless someone does it faster.

  3. devnexen commented on Dec 21, 2025

    @devnexen
    Member

    ah well sorry Niels :)

  4. dshovchko commented on Dec 21, 2025

    @dshovchko
    Author

    I have Cact (https://www.cacti.net/)i running on PHP 8.3.27 on my VPS. I decided to try upgrading to 8.4.15 and got an error in the web interface saying that it was impossible to retrieve data via SNMP. But the console agent written in C++ worked and collected data. I took an interest in this case and found that the problem was in PHP with the SNMP constructor. One of the features of the VPS is that it does not have IPv6, only IPv4. What other details do you need?

  5. azotov commented on Jan 19, 2026

    @azotov

    Got this problem on FreeBSD 14.3 after upgrading net-snmp from 5.9.4 to 5.9.5.2 and recompiling php-snmp. Upgrading php from 8.4.16 to 8.5.1 did not resolve the problem.

    I have found at least one bug in snmp.c in snmp_session_init. In my building environment (after upgrading net-snmp to 5.9.5.2) HAVE_GETADDRINFO is defined but HAVE_IPV6 is not defined. As a result session->peername variable is never "filled properly" after resetting it to '\0'.

    Deleting #if defined(HAVE_GETADDRINFO) && defined(HAVE_IPV6) and corresponging #endif partially solved the problem - SNMP::__construct() stopped returning the error "Unknown failure..". But SNMP::walk() for this session returned the error "Could not open snmp connection: Unknown host (127.0.0.1) (Can't assign requested address)". Trying snmprealwalk() with same parameters did not return any errors.

    I recommend checking entire php-snmp code for compatibility with new net-snmp 5.9.5.2 version.

  6. devnexen commented on Jan 19, 2026

    @devnexen
    Member

    Thanks for the hints @azotov I ll have a look soon-ish

  7. self-assigned this
    on Jan 19, 2026
  8. safelock1 commented on Feb 5, 2026

    @safelock1

    Maybe my problem is related to yours?:

    #21132

  9. devnexen commented on Feb 6, 2026

    @devnexen
    Member

    partially at least very likely yes.

  10. PM2D commented on Oct 27, 2026

    @PM2D

    Just cant wait for fix anymore, my scripts which using php-snmp not working since winter with net-snmp 5.9.5.2, and suddenly stopped working with net-snmp 5.9.4 now with php 8.5.12 after upgrade from php 8.5.11 (but looks like php upgrade is unrelated).
    Guess since that much time has passed exp/snmp is dead and i need to rewrite these scripts somehow.

  11. safelock1 commented on Oct 27, 2026

    @safelock1

    php 8.5.12 work ok (i'm tested) with net-snmp 5.9.4 (NET-SNMP version: 5.9.4.pre2) - i'm tested with my system

  12. dshovchko commented on Oct 27, 2026

    @dshovchko
    Author

    php 8.5.12 SNMP is still not working for me.
    NET-SNMP version: 5.9.4.pre2 on both Ubuntu 24.04 and 26.04.
    I am using a workaround for Cacti by disabling php-snmp.

  13. PM2D commented on Oct 27, 2026

    @PM2D

    Yes, managed to make net-snmp 5.9.4 working, but i needed to find and rebuild it manually because that version was completely removed from gentoo in march 2026. Was just keeping it in my system since then without touching so maybe some binary incompatibilities showed up. Can't use workarounds because scripts were writted with ext/snmp at first place, already tried to rewrite them using net-snmp cli tools but it not that trivial and was working slower with many snmp v1 queries. Ext/snmp somehow is faster than some other libraries (like python).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions