Просмотр исходного кода

bugfix: html - fixed snmp walk interface port status
new: switched from perl module Net::SSH::Expect to Net::OpenSSH

root 4 лет назад
Родитель
Сommit
66b2351088
5 измененных файлов с 130 добавлено и 183 удалено
  1. 3 0
      .gitignore
  2. 23 5
      docs/Readme.ru.md
  3. 9 8
      html/admin/devices/switchstatus.php
  4. 64 103
      html/inc/common.php
  5. 31 67
      scripts/Rstat/cmd.pm

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+/scripts/cfg/config
+/html/cfg/config.php
+/html/js/

+ 23 - 5
docs/Readme.ru.md

@@ -1,7 +1,9 @@
тсановка для CentOS 8:
+Установка
 
 
 1. Включаем дополнительные репозитории:
 1. Включаем дополнительные репозитории:
 
 
+для CentOS 8:
+
 yum install dnf-plugins-core
 yum install dnf-plugins-core
 yum config-manager --set-enabled powertools
 yum config-manager --set-enabled powertools
 yum config-manager --set-enabled extras
 yum config-manager --set-enabled extras
@@ -9,11 +11,21 @@ dnf install epel-release elrepo-release
 
 
 2. Ставим пакеты:
 2. Ставим пакеты:
 
 
+Centos:
+
 dnf install httpd php php-common perl mariadb-server git fping net-snmp-utils \
 dnf install httpd php php-common perl mariadb-server git fping net-snmp-utils \
 php-mysqlnd php-bcmath php-intl php-mbstring php-pear-Date php-pear-Mail php-snmp perl-Net-Patricia \
 php-mysqlnd php-bcmath php-intl php-mbstring php-pear-Date php-pear-Mail php-snmp perl-Net-Patricia \
 perl-NetAddr-IP perl-Config-Tiny perl-Net-DNS perl-DateTime perl-Proc-Daemon perl-Net-Netmask \
 perl-NetAddr-IP perl-Config-Tiny perl-Net-DNS perl-DateTime perl-Proc-Daemon perl-Net-Netmask \
 perl-Text-Iconv perl-DateTime-Format-DateParse perl-Net-SNMP perl-Net-Telnet perl-Net-IPv4Addr \
 perl-Text-Iconv perl-DateTime-Format-DateParse perl-Net-SNMP perl-Net-Telnet perl-Net-IPv4Addr \
-perl-DBI perl-DBD-MySQL perl-Parallel-ForkManager -y
+perl-DBI perl-DBD-MySQL perl-Net-OpenSSH perl-Parallel-ForkManager -y
+
+Ubuntu:
+apt install apache2 git fping perl mariadb-server php php-mysql php-bcmath php-intl \
+php-mbstring php-date php-mail php-snmp \
+libnet-patricia-perl libnetaddr-ip-perl libconfig-tiny-perl libnet-dns-perl libdatetime-perl \
+libnet-netmask-perl libtext-iconv-perl libnet-snmp-perl libnet-telnet-perl libdbi-perl \
+libdbd-mysql-perl libparallel-forkmanager-perl libproc-daemon-perl libdatetime-format-dateparse-perl \
+libnetwork-ipv4addr-perl libnet-openssh-perl
 
 
 3. Качаем исходники и раскидываем по каталогам:
 3. Качаем исходники и раскидываем по каталогам:
 
 
@@ -69,11 +81,10 @@ edit: /var/www/html/cfg/config.php & /usr/local/scripts/cfg/config
 
 
 7. Настраиваем апач и php:
 7. Настраиваем апач и php:
 
 
+Centos:
 sed -i 's/short_open_tag = Off/short_open_tag = On/' /etc/php.ini
 sed -i 's/short_open_tag = Off/short_open_tag = On/' /etc/php.ini
-
 #set timezone
 #set timezone
 sed -i 's/;date.timezone =/date.timezone = Europe\/Moscow/' /etc/php.ini
 sed -i 's/;date.timezone =/date.timezone = Europe\/Moscow/' /etc/php.ini
-
 #enable php
 #enable php
 sed -i 's/#LoadModule mpm_prefork_module/LoadModule mpm_prefork_module/' /etc/httpd/conf.modules.d/00-mpm.conf
 sed -i 's/#LoadModule mpm_prefork_module/LoadModule mpm_prefork_module/' /etc/httpd/conf.modules.d/00-mpm.conf
 sed -i 's/LoadModule mpm_event_module/#LoadModule mpm_event_module/' /etc/httpd/conf.modules.d/00-mpm.conf
 sed -i 's/LoadModule mpm_event_module/#LoadModule mpm_event_module/' /etc/httpd/conf.modules.d/00-mpm.conf
@@ -81,6 +92,13 @@ sed -i 's/LoadModule mpm_event_module/#LoadModule mpm_event_module/' /etc/httpd/
 systemctl enable httpd
 systemctl enable httpd
 systemctl start httpd
 systemctl start httpd
 
 
+Ubuntu:
+sed -i 's/short_open_tag = Off/short_open_tag = On/' /etc/php/7.4/apache2/php.ini
+sed -i 's/;date.timezone =/date.timezone = Europe\/Moscow/' /etc/php/7.4/apache2/php.ini
+
+systemctl enable apache2
+systemctl start apache2
+
 cp docs/addons/sudoers.d/apache /etc/sudoers.d/apache
 cp docs/addons/sudoers.d/apache /etc/sudoers.d/apache
 
 
 8. Cron & logrotate
 8. Cron & logrotate
@@ -95,7 +113,7 @@ cp docs/logrotate/scripts /etc/logrotate.d/scripts
 
 
 ######################################### DHCP Server at Linux ###############################################################
 ######################################### DHCP Server at Linux ###############################################################
 
 
-Можно исопльзовать dhcp-сервер как на миркотике, так и на сервере с Linux. Имхо, dnsmasq - предпочтительнее. 
+Можно использовать dhcp-сервер как на миркотике, так и на сервере с Linux. Имхо, dnsmasq - предпочтительнее.
 
 
 dnf install dnsmasq -y
 dnf install dnsmasq -y
 
 

+ 9 - 8
html/admin/devices/switchstatus.php

@@ -72,8 +72,9 @@ print "<br>\n";
 print "<b>Состояние портов ".$switch['device_name']." - ".$switch['ip']."</b><br>\n";
 print "<b>Состояние портов ".$switch['device_name']." - ".$switch['ip']."</b><br>\n";
 
 
 if ($switch['snmp_version']>0) {
 if ($switch['snmp_version']>0) {
-        $snmp_ok = check_snmp_access($switch['ip'], $switch['community'], $switch['snmp_version']);
-	if ($snmp_ok) {
+    $snmp_ok = check_snmp_access($switch['ip'], $switch['community'], $switch['snmp_version']);
+    $modules_oids = NULL;
+    if ($snmp_ok) {
 	    global $cisco_modules;
 	    global $cisco_modules;
             if ($switch['snmp_version'] == 2) {
             if ($switch['snmp_version'] == 2) {
 	        $modules_oids = snmp2_real_walk($switch['ip'], $switch['community'], $cisco_modules);
 	        $modules_oids = snmp2_real_walk($switch['ip'], $switch['community'], $cisco_modules);
@@ -82,7 +83,7 @@ if ($switch['snmp_version']>0) {
 	        $modules_oids = snmprealwalk($switch['ip'], $switch['community'], $cisco_modules);
 	        $modules_oids = snmprealwalk($switch['ip'], $switch['community'], $cisco_modules);
 	        }
 	        }
 	    }
 	    }
-        } else { $snmp_ok = 0; }
+    } else { $snmp_ok = 0; }
 
 
     print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
     print "<table class=\"data\" cellspacing=\"1\" cellpadding=\"4\">\n";
     print "<tr>\n";
     print "<tr>\n";
@@ -115,9 +116,9 @@ if ($switch['snmp_version']>0) {
         if (isset($switch['ip']) and ($switch['ip'] != '') and $snmp_ok) {
         if (isset($switch['ip']) and ($switch['ip'] != '') and $snmp_ok) {
             $port_state_detail = get_port_state_detail($row['snmp_index'], $switch['ip'], $switch['community'], $switch['snmp_version'], $switch['fdb_snmp_index']);
             $port_state_detail = get_port_state_detail($row['snmp_index'], $switch['ip'], $switch['community'], $switch['snmp_version'], $switch['fdb_snmp_index']);
             list ($poper, $padmin, $pspeed, $perrors) = explode(';', $port_state_detail);
             list ($poper, $padmin, $pspeed, $perrors) = explode(';', $port_state_detail);
-            if (preg_match('/up/i', $poper)) { $cl = "up";  }
-            if (preg_match('/down/i', $poper)) {
-                if (preg_match('/down/i', $padmin)) { $cl = "shutdown"; } else { $cl = "down"; }
+            if ($poper == 1 ) { $cl = "up";  }
+            if ($poper >= 2 ) {
+                if ($padmin >=2) { $cl = "shutdown"; } else { $cl = "down"; }
                 }
                 }
             }
             }
 	print "<td class=\"$cl\" style='padding:0'><input type=checkbox name=d_port_index[] value=".$row['snmp_index']." ></td>\n";
 	print "<td class=\"$cl\" style='padding:0'><input type=checkbox name=d_port_index[] value=".$row['snmp_index']." ></td>\n";
@@ -215,10 +216,10 @@ if ($switch['snmp_version']>0) {
         if (isset($padmin) and ! $row['uplink'] and ! $row['skip'] and ! $switch['is_router']) {
         if (isset($padmin) and ! $row['uplink'] and ! $row['skip'] and ! $switch['is_router']) {
                 print "<td class=\"data\">";
                 print "<td class=\"data\">";
                 if ($switch['vendor_id'] != 9) {
                 if ($switch['vendor_id'] != 9) {
-                    if (preg_match('/down/i', $padmin)) {
+                    if ($padmin >=2) {
                         print "<button name='port_on[]' value='{$row['snmp_index']}'>Enable port</button>";
                         print "<button name='port_on[]' value='{$row['snmp_index']}'>Enable port</button>";
                 	}
                 	}
-                    if (preg_match('/up/i', $padmin)) {
+                    if ($padmin ==1) {
                         print "<button name='port_off[]' value='{$row['snmp_index']}'>Shutdown port</button>";
                         print "<button name='port_off[]' value='{$row['snmp_index']}'>Shutdown port</button>";
                 	}
                 	}
             	    } else {
             	    } else {

+ 64 - 103
html/inc/common.php

@@ -28,7 +28,7 @@ $cisco_descr = '.1.3.6.1.2.1.1.1.0';
 $cisco_modules = '.1.3.6.1.2.1.47.1.1.1.1.7';
 $cisco_modules = '.1.3.6.1.2.1.47.1.1.1.1.7';
 $cisco_sfp_sensors = '.1.3.6.1.4.1.9.9.91.1.1.1.1.4';
 $cisco_sfp_sensors = '.1.3.6.1.4.1.9.9.91.1.1.1.1.4';
 $cisco_sfp_precision = '.1.3.6.1.4.1.9.9.91.1.1.1.1.3';
 $cisco_sfp_precision = '.1.3.6.1.4.1.9.9.91.1.1.1.1.3';
-$cisco_vlan_oid = 'SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.2';
+$cisco_vlan_oid = '.1.3.6.1.4.1.9.9.9.46.1.3.1.1.2';
 
 
 $ifmib_ifindex  = '.1.3.6.1.2.1.2.2.1.1';
 $ifmib_ifindex  = '.1.3.6.1.2.1.2.2.1.1';
 $ifmib_ifdescr  = '.1.3.6.1.2.1.2.2.1.2';
 $ifmib_ifdescr  = '.1.3.6.1.2.1.2.2.1.2';
@@ -92,8 +92,10 @@ $admin_email = "admin";
 $sender_email = "root";
 $sender_email = "root";
 $send_email = 0;
 $send_email = 0;
 
 
-$mac_table_str_oid = 'SNMPv2-SMI::mib-2.17.7.1.2.2.1.2';
-$mac_table_str_oid2 = 'SNMPv2-SMI::mib-2.17.4.3.1.2';
+
+
+$mac_table_str_oid = '.1.3.6.1.2.1.17.4.3.1.2';
+$mac_table_str_oid2 = '1.3.6.1.2.1.17.7.1.2.2.1.2';
 
 
 function get_user_ip()
 function get_user_ip()
 {
 {
@@ -1866,9 +1868,7 @@ function check_snmp_access($ip, $community, $version)
         $version = '2';
         $version = '2';
     }
     }
     $result = get_snmp($ip, $community, $version, '.1.3.6.1.2.1.1.1.0');
     $result = get_snmp($ip, $community, $version, '.1.3.6.1.2.1.1.1.0');
-    if (! $result) {
-        return;
-    }
+    if (!isset($result)) { return; }
     return 1;
     return 1;
 }
 }
 
 
@@ -1907,11 +1907,9 @@ function get_cisco_sensors($ip, $community, $version, $mkey)
     $index = get_last_digit($mkey);
     $index = get_last_digit($mkey);
     global $cisco_sfp_sensors;
     global $cisco_sfp_sensors;
     global $cisco_sfp_precision;
     global $cisco_sfp_precision;
-    list ($pattern, $result) = explode(':', get_snmp($ip, $community, $version, $cisco_sfp_sensors . "." . $index));
-    list ($pattern, $prec) = explode(':', get_snmp($ip, $community, $version, $cisco_sfp_precision . "." . $index));
-    if (! isset($prec)) {
-        $prec = 1;
-    }
+    $result = parse_snmp_value(get_snmp($ip, $community, $version, $cisco_sfp_sensors . "." . $index));
+    $prec = parse_snmp_value(get_snmp($ip, $community, $version, $cisco_sfp_precision . "." . $index));
+    if (! isset($prec)) { $prec = 1; }
     $result = round(trim($result) / (10 * $prec), 2);
     $result = round(trim($result) / (10 * $prec), 2);
     return $result;
     return $result;
 }
 }
@@ -1919,16 +1917,14 @@ function get_cisco_sensors($ip, $community, $version, $mkey)
 function get_snmp_ifname1($ip, $community, $version, $port)
 function get_snmp_ifname1($ip, $community, $version, $port)
 {
 {
     global $ifmib_ifname;
     global $ifmib_ifname;
-    $port_name = '';
-    list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifindex . "." . $port));
+    $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifname . "." . $port));
     return $port_name;
     return $port_name;
 }
 }
 
 
 function get_snmp_ifname2($ip, $community, $version, $port)
 function get_snmp_ifname2($ip, $community, $version, $port)
 {
 {
     global $ifmib_ifdescr;
     global $ifmib_ifdescr;
-    $port_name = '';
-    list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
+    $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
     return $port_name;
     return $port_name;
 }
 }
 
 
@@ -1996,40 +1992,28 @@ function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_o
         global $eltex_sfp_sn;
         global $eltex_sfp_sn;
         global $eltex_sfp_freq;
         global $eltex_sfp_freq;
         global $eltex_sfp_length;
         global $eltex_sfp_length;
-        list ($pattern, $sfp_vendor) = explode(':', get_snmp($ip, $community, $version, $eltex_sfp_vendor . "." . $port));
+        $sfp_vendor = parse_snmp_value(get_snmp($ip, $community, $version, $eltex_sfp_vendor . "." . $port));
         if (isset($sfp_vendor)) {
         if (isset($sfp_vendor)) {
             $sfp_status_temp = $eltex_sfp_status . "." . $port . ".5";
             $sfp_status_temp = $eltex_sfp_status . "." . $port . ".5";
             $sfp_status_volt = $eltex_sfp_status . "." . $port . ".6";
             $sfp_status_volt = $eltex_sfp_status . "." . $port . ".6";
             $sfp_status_circut = $eltex_sfp_status . "." . $port . ".7";
             $sfp_status_circut = $eltex_sfp_status . "." . $port . ".7";
             $sfp_status_tx = $eltex_sfp_status . "." . $port . ".8";
             $sfp_status_tx = $eltex_sfp_status . "." . $port . ".8";
             $sfp_status_rx = $eltex_sfp_status . "." . $port . ".9";
             $sfp_status_rx = $eltex_sfp_status . "." . $port . ".9";
-            list ($pattern, $temp) = explode(':', get_snmp($ip, $community, $version, $sfp_status_temp));
-            list ($pattern, $volt) = explode(':', get_snmp($ip, $community, $version, $sfp_status_volt));
-            list ($pattern, $circut) = explode(':', get_snmp($ip, $community, $version, $sfp_status_circut));
-            list ($pattern, $tx) = explode(':', get_snmp($ip, $community, $version, $sfp_status_tx));
-            list ($pattern, $rx) = explode(':', get_snmp($ip, $community, $version, $sfp_status_rx));
-            list ($pattern, $sfp_sn) = explode(':', get_snmp($ip, $community, $version, $eltex_sfp_sn . "." . $port));
-            list ($pattern, $sfp_freq) = explode(':', get_snmp($ip, $community, $version, $eltex_sfp_freq . "." . $port));
-            if (! isset($sfp_freq) or $sfp_freq == 65535) {
-                $sfp_freq = 'unspecified';
-            }
-            list ($pattern, $sfp_length) = explode(':', get_snmp($ip, $community, $version, $eltex_sfp_length . "." . $port));
+            $temp = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_temp));
+            $volt = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_volt));
+            $circut = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_circut));
+            $tx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_tx));
+            $rx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_rx));
+            $sfp_sn = parse_snmp_value(get_snmp($ip, $community, $version, $eltex_sfp_sn . "." . $port));
+            $sfp_freq = parse_snmp_value(get_snmp($ip, $community, $version, $eltex_sfp_freq . "." . $port));
+            if (! isset($sfp_freq) or $sfp_freq == 65535) {  $sfp_freq = 'unspecified';  }
+            $sfp_length = parse_snmp_value(get_snmp($ip, $community, $version, $eltex_sfp_length . "." . $port));
             $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
             $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
-            if (isset($sfp_status_temp) and $temp > 0.1) {
-                $status .= 'Temp: ' . $temp . " C";
-            }
-            if (isset($sfp_status_volt) and $volt > 0.1) {
-                $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
-            }
-            if (isset($sfp_status_circut) and $circut > 0.1) {
-                $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
-            }
-            if (isset($sfp_status_tx) and $tx > 0.1) {
-                $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
-            }
-            if (isset($sfp_status_rx) and $rx > 0.1) {
-                $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
-            }
+            if (isset($sfp_status_temp) and $temp > 0.1) { $status .= 'Temp: ' . $temp . " C"; }
+            if (isset($sfp_status_volt) and $volt > 0.1) { $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V'; }
+            if (isset($sfp_status_circut) and $circut > 0.1) { $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA'; }
+            if (isset($sfp_status_tx) and $tx > 0.1) { $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm'; }
+            if (isset($sfp_status_rx) and $rx > 0.1) { $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm'; }
             $status .= '<br>';
             $status .= '<br>';
             return $status;
             return $status;
         }
         }
@@ -2040,9 +2024,9 @@ function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_o
         global $cisco_descr;
         global $cisco_descr;
         global $cisco_modules;
         global $cisco_modules;
         // get interface names
         // get interface names
-        list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifname . "." . $port));
+        $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifname . "." . $port));
         if (empty($port_name)) {
         if (empty($port_name)) {
-            list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
+            $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
             }
             }
         // search module indexes
         // search module indexes
         $port_name = preg_quote(trim($port_name), '/');
         $port_name = preg_quote(trim($port_name), '/');
@@ -2114,9 +2098,9 @@ function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_o
         global $huawei_sfp_biascurrent;
         global $huawei_sfp_biascurrent;
         
         
         // get interface names
         // get interface names
-        list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifname . "." . $port));
+        $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifname . "." . $port));
         if (empty($port_name)) {
         if (empty($port_name)) {
-            list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
+            $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
     	    }
     	    }
         // search module indexes
         // search module indexes
         $port_name = preg_quote(trim($port_name), '/');
         $port_name = preg_quote(trim($port_name), '/');
@@ -2126,32 +2110,22 @@ function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_o
             if (isset($matches[0])) {
             if (isset($matches[0])) {
                 $module_id = get_last_digit($key);
                 $module_id = get_last_digit($key);
                 unset($result);
                 unset($result);
-                list ($pattern, $result) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_vendor . "." . $module_id));
-                if (isset($result)) {
-                    $sfp_vendor = $result;
-                }
+                $result = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_vendor . "." . $module_id));
+                if (isset($result)) { $sfp_vendor = $result; }
                 unset($result);
                 unset($result);
-                list ($pattern, $result) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_speed . "." . $module_id));
+                $result = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_speed . "." . $module_id));
                 if (isset($result)) {
                 if (isset($result)) {
                     list ($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
                     list ($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
-                    if ($sfp_type == 0) {
-                        $sfp_type = 'MultiMode';
-                    }
-                    if ($sfp_type == 1) {
-                        $sfp_type = 'SingleMode';
-                    }
+                    if ($sfp_type == 0) { $sfp_type = 'MultiMode'; }
+                    if ($sfp_type == 1) { $sfp_type = 'SingleMode'; }
                 }
                 }
 
 
-                list ($pattern, $volt) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_volt . "." . $module_id));
-                list ($pattern, $circut) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_biascurrent . "." . $module_id));
-                list ($pattern, $tx) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_opttx . "." . $module_id));
-                list ($pattern, $rx) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_optrx . "." . $module_id));
-		if (!isset($tx)) {
-            	    list ($pattern, $tx) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_tx . "." . $module_id));
-            	    }
-            	if (!isset($rx)) {
-                    list ($pattern, $rx) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_rx . "." . $module_id));
-		    }
+                $volt = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_volt . "." . $module_id));
+                $circut = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_biascurrent . "." . $module_id));
+                $tx = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_opttx . "." . $module_id));
+                $rx = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_optrx . "." . $module_id));
+		if (!isset($tx)) { $tx = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_tx . "." . $module_id)); }
+            	if (!isset($rx)) { $rx = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_rx . "." . $module_id)); }
                 if (isset($sfp_vendor)) {  $status .= ' Name:' . $sfp_vendor.'<br>';  }
                 if (isset($sfp_vendor)) {  $status .= ' Name:' . $sfp_vendor.'<br>';  }
 //                if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
 //                if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
 //                if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
 //                if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
@@ -2257,14 +2231,10 @@ function get_port_poe_state($vendor_id, $port, $ip, $community, $version)
     $result = '';
     $result = '';
     $c_state = get_snmp($ip, $community, $version, $poe_status);
     $c_state = get_snmp($ip, $community, $version, $poe_status);
     if (!empty($c_state)) {
     if (!empty($c_state)) {
-        list ($pattern, $p_state) = explode(':', $c_state);
+        $p_state = parse_snmp_value($c_state);
         if ($vendor_id == 9) {
         if ($vendor_id == 9) {
-            if ($p_state == 1) {
-                return 2;
-            }
-            if ($p_state > 1) {
-                return 1;
-            }
+            if ($p_state == 1) { return 2; }
+            if ($p_state > 1) { return 1; }
         }
         }
         return $p_state;
         return $p_state;
     }
     }
@@ -2386,7 +2356,7 @@ function get_port_poe_detail($vendor_id, $port, $ip, $community, $version)
     if (isset($poe_power)) {
     if (isset($poe_power)) {
         $c_power = get_snmp($ip, $community, $version, $poe_power);
         $c_power = get_snmp($ip, $community, $version, $poe_power);
         if (isset($c_power)) {
         if (isset($c_power)) {
-            $p_power = snmp_value($c_power);
+            $p_power = parse_snmp_value($c_power);
             if ($vendor_id == 9) {
             if ($vendor_id == 9) {
                 $p_power = round($p_power / 10, 2);
                 $p_power = round($p_power / 10, 2);
             } else {
             } else {
@@ -2400,7 +2370,7 @@ function get_port_poe_detail($vendor_id, $port, $ip, $community, $version)
     if (isset($poe_current)) {
     if (isset($poe_current)) {
         $c_current = get_snmp($ip, $community, $version, $poe_current);
         $c_current = get_snmp($ip, $community, $version, $poe_current);
         if (isset($c_current)) {
         if (isset($c_current)) {
-            $p_current = snmp_value($c_current);
+            $p_current = parse_snmp_value($c_current);
             if ($p_current > 0) {
             if ($p_current > 0) {
                 $result .= ' C: ' . $p_current . ' mA';
                 $result .= ' C: ' . $p_current . ' mA';
             }
             }
@@ -2409,7 +2379,7 @@ function get_port_poe_detail($vendor_id, $port, $ip, $community, $version)
     if (isset($poe_volt)) {
     if (isset($poe_volt)) {
         $c_volt = get_snmp($ip, $community, $version, $poe_volt);
         $c_volt = get_snmp($ip, $community, $version, $poe_volt);
         if (isset($c_volt)) {
         if (isset($c_volt)) {
-            $p_volt = snmp_value($c_volt);
+            $p_volt = parse_snmp_value($c_volt);
             if ($vendor_id == 2 or $vendor_id == 8) {
             if ($vendor_id == 2 or $vendor_id == 8) {
                 $p_volt = round($p_volt / 1000, 2);
                 $p_volt = round($p_volt / 1000, 2);
             }
             }
@@ -2428,7 +2398,7 @@ function get_port_poe_detail($vendor_id, $port, $ip, $community, $version)
     if (isset($poe_class)) {
     if (isset($poe_class)) {
         $c_class = get_snmp($ip, $community, $version, $poe_class);
         $c_class = get_snmp($ip, $community, $version, $poe_class);
         if (isset($c_class)) {
         if (isset($c_class)) {
-            $p_class = snmp_value($c_class);
+            $p_class = parse_snmp_value($c_class);
             if ($p_class > 0 and $p_power > 0) {
             if ($p_class > 0 and $p_power > 0) {
                 $result .= ' Class: ' . ($p_class - 1);
                 $result .= ' Class: ' . ($p_class - 1);
             }
             }
@@ -2460,17 +2430,6 @@ function set_snmp($ip, $community, $version, $oid, $field, $value)
     return $result;
     return $result;
 }
 }
 
 
-function snmp_value($value)
-{
-    if (preg_match('/:/', $value)) {
-        list ($pattern, $result) = explode(':', $value);
-        $result = str_replace('"', '', $result);
-    } else {
-        $result = $value;
-    }
-    return trim($result);
-}
-
 function set_port_state($vendor_id, $port, $ip, $community, $version, $state)
 function set_port_state($vendor_id, $port, $ip, $community, $version, $state)
 {
 {
     // port -> snmp_index!!!
     // port -> snmp_index!!!
@@ -2566,27 +2525,29 @@ function get_port_state_detail($port, $ip, $community, $version)
     $admin = $port_admin_status_oid . $port;
     $admin = $port_admin_status_oid . $port;
     $speed = $port_speed_oid . $port;
     $speed = $port_speed_oid . $port;
     $errors = $port_errors_oid . $port;
     $errors = $port_errors_oid . $port;
-
     $result = '';
     $result = '';
-
     $c_state = get_snmp($ip, $community, $version, $oper);
     $c_state = get_snmp($ip, $community, $version, $oper);
-    list ($pattern, $p_state) = explode(':', $c_state);
+    $p_state = parse_snmp_value($c_state);
     $c_admin = get_snmp($ip, $community, $version, $admin);
     $c_admin = get_snmp($ip, $community, $version, $admin);
-    list ($pattern, $p_admin) = explode(':', $c_admin);
-    if (preg_match('/up/i', $p_state)) {
-        $c_speed = get_snmp($ip, $community, $version, $speed);
-    } else {
-        $c_speed = 'INT:0';
-    }
-    list ($pattern, $p_speed) = explode(':', $c_speed);
+    $p_admin = parse_snmp_value ($c_admin);
+    if ($p_state == 1) { $c_speed = get_snmp($ip, $community, $version, $speed); } else { $c_speed = 'INT:0'; }
+    $p_speed = parse_snmp_value($c_speed);
     $c_errors = get_snmp($ip, $community, $version, $errors);
     $c_errors = get_snmp($ip, $community, $version, $errors);
-    list ($pattern, $p_errors) = explode(':', $c_errors);
-
-    $result = $p_state . ";" . $p_admin . ";" . trim($p_speed) . ";" . trim($p_errors);
-
+    $p_errors = parse_snmp_value($c_errors);
+    $result = $p_state . ";" . $p_admin . ";" . $p_speed . ";" . $p_errors;
     return $result;
     return $result;
 }
 }
 
 
+function parse_snmp_value($value) {
+if (empty($value)) { return NULL; }
+list ($p_type, $p_value) = explode(':', $value);
+$p_value = trim($p_value);
+$p_value= preg_replace('/^\"/','',$p_value);
+$p_value= preg_replace('/\"$/','',$p_value);
+$p_value = trim($p_value);
+return $p_value;
+}
+
 function dec_to_hex($mac)
 function dec_to_hex($mac)
 {
 {
     if (! isset($mac)) {
     if (! isset($mac)) {

+ 31 - 67
scripts/Rstat/cmd.pm

@@ -15,7 +15,7 @@ use Data::Dumper;
 use Rstat::config;
 use Rstat::config;
 use Rstat::main;
 use Rstat::main;
 use Net::Telnet;
 use Net::Telnet;
-use Net::SSH::Expect;
+use Net::OpenSSH;
 
 
 @ISA = qw(Exporter);
 @ISA = qw(Exporter);
 @EXPORT = qw(
 @EXPORT = qw(
@@ -341,78 +341,50 @@ if ($switch_auth{$device->{vendor_id}}{proto} eq 'ssh') {
     if (!$device->{port}) { $device->{port} = '22'; }
     if (!$device->{port}) { $device->{port} = '22'; }
     log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by ssh...");
     log_info("Try login to $device->{device_name} $device->{ip}:$device->{port} by ssh...");
     eval {
     eval {
-        $t  = Net::SSH::Expect->new (
-            host=>$device->{ip},
-            port=>$device->{port},
-            user=>$device->{login},
-            password=>$device->{password},
-            timeout=>1,
-            raw_pty=>1,
-            ssh_option=>'-o PubkeyAcceptedKeyTypes=+ssh-dss \
-            -o KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1 \
-            -o HostKeyAlgorithms=+ssh-dss \
-            -o LogLevel=quiet \
-            -o UserKnownHostsFile=/dev/null \
-            -o StrictHostKeyChecking=no'
-            );
-
-        $t->run_ssh() or die "SSH process couldn't start: $!";
-
-        my $retry_count = 0;
-        my $max_retry_count = 30;
-        my $rc;
-        while(1){
-            $rc = eval{$t->login($switch_auth{$device->{vendor_id}}{login},$switch_auth{$device->{vendor_id}}{password},0);};
-            last if defined $rc;
-            return if $retry_count >= $max_retry_count;
-            $retry_count++;
-            sleep 1;
-            }
-
-        if ($rc !~ /$switch_auth{$device->{vendor_id}}{prompt}/) { return; }
+	$t = Net::OpenSSH->new($device->{ip},
+	    user=>$device->{login},
+	    password=>$device->{password},
+	    port=>$device->{port},
+	    timeout=>10,
+	    master_opts => [ 
+	    -o => "StrictHostKeyChecking=no", 
+	    -o => "PubkeyAcceptedKeyTypes=+ssh-dss", 
+	    -o => "KexAlgorithms=+diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1",
+	    -o => "HostKeyAlgorithms=+ssh-dss",
+	    -o => "LogLevel=quiet",
+	    -o => "UserKnownHostsFile=/dev/null"
+	    ]
+	    );
 
 
         if (exists $switch_auth{$device->{vendor_id}}{enable}) {
         if (exists $switch_auth{$device->{vendor_id}}{enable}) {
-            $t->send($switch_auth{$device->{vendor_id}}{enable}."\n\r");
-#            $t->print($device->{enable_password});
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
+            $t->system($switch_auth{$device->{vendor_id}}{enable}."\n\r");
             }
             }
-
         if ($device->{vendor_id} eq '2') {
         if ($device->{vendor_id} eq '2') {
-            $t->send("terminal datadump\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
-            $t->send("no logging console\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
+            $t->system("terminal datadump");
+            $t->system("no logging console");
             }
             }
         if ($device->{vendor_id} eq '5') {
         if ($device->{vendor_id} eq '5') {
-            $t->send("terminal page-break disable\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
+            $t->system("terminal page-break disable");
             }
             }
         if ($device->{vendor_id} eq '6') {
         if ($device->{vendor_id} eq '6') {
-            $t->send("terminal length 0\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
+            $t->system("terminal length 0");
             }
             }
         if ($device->{vendor_id} eq '9') {
         if ($device->{vendor_id} eq '9') {
-            $t->send("/system note set show-at-login=no\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
+            $t->system("/system note set show-at-login=no");
             }
             }
         if ($device->{vendor_id} eq '16') {
         if ($device->{vendor_id} eq '16') {
-            $t->send("terminal width 0\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
+            $t->system("terminal width 0");
             }
             }
         if ($device->{vendor_id} eq '17') {
         if ($device->{vendor_id} eq '17') {
-            $t->send("more displine 50\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
-            $t->send("more off\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
+            $t->system("more displine 50");
+            $t->system("more off");
             }
             }
         if ($device->{vendor_id} eq '38') {
         if ($device->{vendor_id} eq '38') {
-            $t->send("disable cli prompting\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
-            $t->send("disable clipaging\n\r");
-            $t->waitfor("/$switch_auth{$device->{vendor_id}}{prompt}/",1);
+            $t->system("disable cli prompting");
+            $t->system("disable clipaging");
             }
             }
         };
         };
-    if ($@) { log_error("Login to $device->{device_name} ip: $device->{ip} by telnet aborted: $@"); } else { log_info("Login to $device->{device_name} ip: $device->{ip} by ssh success!"); }
+    if ($@) { log_error("Login to $device->{device_name} ip: $device->{ip} by ssh aborted: $@"); return; } else { log_info("Login to $device->{device_name} ip: $device->{ip} by ssh success!"); }
     }
     }
 
 
 return $t;
 return $t;
@@ -447,20 +419,12 @@ if ($proto eq 'ssh') {
             next;
             next;
             }
             }
         log_session('Send:'.$run_cmd);
         log_session('Send:'.$run_cmd);
-        $session->send($run_cmd."\n\r");
-        my $chunk;
-        while ($chunk = $session->peek(1)) {
-            my $ret =$session->eat($chunk);
-            if (ref($ret) eq 'ARRAY') {
-                push(@result,@{$ret});
-                } else {
-                my @norm_text = split(/\n/,$ret);
-                foreach my $row (@norm_text) { push(@result,trim($row)); }
-                }
-            }
+        my @row = $session->capture($run_cmd);
+	chomp(@row);
+        push(@result,@row);
         select(undef, undef, undef, 0.25);
         select(undef, undef, undef, 0.25);
         }
         }
-#    log_session('Get:'.Dumper(\@result));
+    log_session('Get:'.Dumper(\@result));
     };
     };
     if ($@) { log_error("Abort: $@"); return 0; };
     if ($@) { log_error("Abort: $@"); return 0; };
     }
     }