Skip to content

Instantly share code, notes, and snippets.

@negima1976
Created November 27, 2012 09:05
Show Gist options
  • Select an option

  • Save negima1976/4153253 to your computer and use it in GitHub Desktop.

Select an option

Save negima1976/4153253 to your computer and use it in GitHub Desktop.
ss_tcp_connections.php
--- ss_tcp_connections_old.php 2012-11-28 02:59:13.074710576 +0900
+++ ss_tcp_connections_new.php 2012-11-28 02:46:47.472714312 +0900
@@ -1,25 +1,30 @@
<?php
-/* do NOT run this script through a web browser */
+require_once 'Console/CommandLine.php';
-if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
- die("<br><strong>This script is only meant to run at the command line.</strong>");
+$parser = new Console_CommandLine(array(
+ 'description' => 'ss_tcp_connection_new.php command line',
+ 'version' => '1.0.0'
+));
+
+$parser->addOption('opt', array(
+ 'short_name' => '-p',
+ 'help_name' => 'param'
+));
+
+$parser->addArgument('param', array(
+ 'multiple' => true,
+ 'description' => 'snmpnetstat parameter'
+));
+
+try {
+ $result = $parser->parse();
+} catch (Exception $e) {
+ $parser->displayError($e->getMessage());
}
-$no_http_headers = true;
-
-/* display No errors */
-error_reporting(0);
-
-var_dump($_SERVER["argv"])."\n";
-
-if (!isset($called_by_script_server)) {
- array_shift($_SERVER["argv"]);
- var_dump($_SERVER["argv"])."\n";
- print call_user_func_array("ss_tcp_connections", $_SERVER["argv"])."\n";
-}
+$res = ss_tcp_connections($result->args["param"][1], $result->args["param"][2]);
function ss_tcp_connections ($hostname, $snmp_auth) {
- echo "hostname=".$hostname."\n";
$snmp = explode(":", $snmp_auth);
$snmp_version = $snmp[0];
$snmp_port = $snmp[1];
@@ -46,7 +51,7 @@
# setup shell command with options for version 2c and version 3
$cmd = '/usr/bin/snmpnetstat -v '.($snmp_version == 2 ? '2c' : '1').' '.($snmp_version == 3 ? '-u '.$snmp_auth_username.' -A '.$snmp_auth_password.' -a '.$snmp_auth_protocol.' -X '.$snmp_priv_passphrase.' -x '.$snmp_priv_protocol.' -n '.$snmp_context : '-c '.$snmp_community ).' -t '.$snmp_timeout.' -Cn -Cp tcp '.$hostname.':'.$snmp_port;
-
+
# initialize all the vars we'll use
$ret = '';
$_estab = $_listen = $_timewait = $_timeclose = $_finwait1 = $_finwait2 = $_synsent = $_synrecv = $_closewait = 0;
@negima1976
Copy link
Author

オプション名は適当です。
とりあえずつけただけです

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