I'm currently successfully reading out several properties on our switches over SNMP with php. Now i'm looking at making the resulting output of snmpget and snmpwalk actually usefull for the consumers of our API's.
Problem is that the responses look like this: INTEGER: up(1) and INTEGER: 10103 ...
Is there any convention/standard on how to parse this response format or is the response vendor specific for each device we are trying to read?
Is there by any chance already a PHP library, function or extension that can cast these responses in php native variables or at least something usefull that we can work with?
UPDATE:
I've found out a few new things namely that there are indeed several libraries in php that can parse binary ASN.1 strings which basically are BER encoded strings if i'm right. Problem is that i can't seem to find a way to get the binary data from the devices with php ...
You can simply use this function at the beginning of your script :
snmp_set_quick_print(TRUE);
It will returns only the value you are searching for, without the leading "INTEGER" or so ;)
Hope this helps !
I'm not sure about your particular PHP methods, but the difference between your two INTEGER examples is likely to be whether your system has an SNMP MIB corresponding to the OID (e.g. to determine that 1 means "up").
If you only want the integers, you should be able to pass a parameter to your get or walk command. For example, net-snmp's snmpget or snmpwalk commands will take -Oe to remove symbolic labels. From the manpage:
$ snmpget -c public -v 1 localhost ipForwarding.0
IP-MIB::ipForwarding.0 = INTEGER: forwarding(1)
$ snmpget -c public -v 1 -Oe localhost ipForwarding.0
IP-MIB::ipForwarding.0 = INTEGER: 1
If you are parsing net-snmp output, I recommend reading the snmpcmd man page as it has a lot of output options that will interest you especially the display of other types such as timeticks and strings.
If you do want to retrieve SNMP in PHP you could look at how Cacti does it.
Related
Does anyone have any experience with mac address values via SNMP get in php where it seems to take off the leading 0's?
I'm not sure if there is an easy way to make sure they don't get stripped when I return a value.
For example
IF-MIB::ifPhysAddress.1 = STRING: 0:80:ea:8c:a:e1
Should give me
00:80:ea:8c:0a:e1
Thanks
Your PHP SNMP client is formatting the returned MIB object value incorrectly.
If you have the source code it is probably a trivial fix.
use -O OUTOPTS
Toggle various defaults controlling output display:
0: print leading 0 for single-digit hex characters
snmpget -v2c -c public IF-MIB::ifPhysAddress.1 -O 0
I have a need to convert source data that I can't control into normalization form C. I am currently doing it, but by calling an external program (uconv). This is what my code snippet looks like:
$malayalam_books = preg_split("/\n/", shell_exec("uconv -f utf8 -t utf8 -x nfc book-names.txt"));
It works well, but obviously making calls to the system is not recommended. I know that PHP supports the ICU libraries, but it's so convoluted how to do a simple thing like this...
I've since discovered that the normalizer_normalize PECL function can handle this natively in PHP 5 >= 5.3.0.
Hello fellow Stackoverflowians.
I'm looking to extract information from a cache key from a Kyoto Tycoon cabinet file, specifically from the output from the Kyoto Cabinet utility kchashmgr (see: http://fallabs.com/kyotocabinet/util.html#kchashmgr )
I am trying to extract the expiration date from the output of the kchashmgr get from the binary/hexadecimal output.
kchashmgr get /path/to/kyto/tycoon/casket.kch cache_key
What is the output format of the above so from command-line shell utility I can do this:
expirationTimestamp=$(kchashmgr get /path/to/kyto/tycoon/casket.kch cache_key | something)
Initially I thought I could gunzip it (required ZLIB for it installation), but that obviously did not work. I also tried using the -px switch to see if that helped.
NOTE: I am looking for a shell solution that would, presumably, be faster than using a PHP script.
Thanks for your time on this subject.
P.S.
[yramirez#losthost ~ ]$ kcutilmgr conf -v # version of Kyoto Cabinet
1.2.76
Currently I need to merge that 50+ PDF files into 1 PDF. I am using PDFTK. Using the guide from: http://www.johnboy.com/blog/merge-multiple-pdf-files-with-php
But it is not working. I have verified the following:
I have tried the command to merge 2 pdfs from my PHP and it is working.
I have echo the final command and copied that command and paste into command prompt and run manually and all the 50 PDFs are successfully merged.
Thus exec in my PHP and the command to merge 50 PDFs are both correct but it is not working when done together in PHP. I have also stated set_time_limit(0) to prevent any timeout but still not working.
Any idea what's wrong?
You can try to find out yourself:
print exec(str_repeat(' ', 5000) . 'whoami');
I think it's 8192, at least on my system, because it fails with strings larger than 10K, but it still works with strings shorter than 7K
I am not sure if there is a length restriction on how long a single command can be but I am pretty sure you can split it accross multiple lines with "\" just to check if thats the problem. Again I dont think it is... Is there any error output when you try to run the full command with PHP and exec, also try system() instead of exec().
PDFTK versions prior to 1.45 are limited to merge 26 files cuz use "handles"
/* Collate scanned pages sample */
pdftk A=even.pdf B=odd.pdf shuffle A B output collated.pdf
as you can see "A" and "B" are "handles", but should be a single upper-case letter, so only A-Z can be used, if u reach that limit, maybe you script outputs an error like
Error: Handle can only be a single, upper-case letter
but in 1.45 this limitation was removed, changelog extract
You can now use multi-character input handles. Prior versions were
limited to a single character, imposing an arbitrary limitation on
the number of input PDFs when using handles. Handles still must be all
upper-case ASCII.
maybe you only need update your lib ;)
Is there a PHP lib that provides similar functionality as the argparse module of Python? PHP's getopt certainly doesn't cut it.
What I need:
required param check and automatic error msg generation.
correct exit status on error ( > 0 if param parse error).
error msg's to STDERR.
help msg generation of all accepted params.
param type checking is a bonus.
Basically how a *NIX CLI script should behave.
The most comprehensive library i know of (and the only one i've actually used) is Console_CommandLine. I have not used argparse so I can't tell you if it is as featured or compare them.
You can use Zend_Console_GetOpt.
Docopt, an altogether beautiful command parser, has a PHP version.