Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to receive snmptraps in php script, so what i've done is:


traphandle /usr/home/user/trap/l.php


$flow = fopen("php://stderr","r");
    file_put_contents("out",fread($flow,1024)."\n", FILE_APPEND);

then i start snmptrapd like this:

snmptrapd -Le -f

and generated traps like this:

snmptrap -v 1 -c public localhost '' localhost 6 1 ''

snmptrapd gives me such output

2012-01-16 14:38:49 UDP: []:11478->[]:0) TRAP, SNMP v1, community public
        SNMPv2-SMI::enterprises.3.1.1 Enterprise Specific Trap (1) Uptime: 70 days, 1:03:57.00

so it seems working... but the problem is that l.php is not executed, or there's nothing in stderr - i can't realize.

please, where's my mistake?

share|improve this question
up vote 1 down vote accepted

php://stderr is not a stream that can be read from. It is a pipe to the STDERR for the PHP process itself, which is write-only.

You need to access the data via STDIN. Rather than using the php:// wrapper to access the STDIO streams, which is known to be buggy (see the manual) you should use the special constants STDOUT, STDERR and STDIN. So for example, I might write your PHP script like this:


  // The file we will be writing to. It's best to specify a full path, to avoid
  // any confusion over the current working directory
  $outFile = '/var/myuser/snmp.out';

  // First, we'll open our outfile...
  if (!$ofp = fopen($outFile, 'a')) exit("Oh No! I couldn't open my out file!");

  // ...and write the current timestamp to it, so we are certain the script was
  // invoked. You can remove this if you want, once you are sure it is working
  fwrite($ofp, "Process started at ".date('Y-m-d H:i:s')."\n");

  // Next we'll write all the trap data to the file
  // We could use stream_copy_to_stream() for this, but I don't know if it is
  // available on your server so I won't do it here
  while (!feof(STDIN)) {
    fwrite($ofp, fread(STDIN, 1024)."\n");
  fwrite($ofp,"End Data\n");

  // We don't actually need a closing PHP tag and in many cases it is better
  // to omit it, to avoid unexpected whitespace being output.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.