home *** CD-ROM | disk | FTP | other *** search
/ ftp.skm.com.ua / 2014.11.ftp.skm.com.ua.tar / ftp.skm.com.ua / pub / tswitch.php < prev   
PHP Script  |  2014-03-02  |  5KB  |  182 lines

  1. <?
  2.  
  3.     if( 0 ) { 
  4.     print "<pre>";
  5.     #$sw = new TSwitch('10.57.124.9','teremok','mypass88');
  6.     $sw = new TSwitch('192.168.2.66','admin','mypass888');
  7.  
  8.     #print $sw->DoCommand('show config current_config include "config access_profile profile_id 4"');
  9.     #print $sw->DoCommand("a");
  10.     print_r( $sw->AclGetList() );
  11.     #print_r( $sw->AclAddIp('133.133.133.133',5) );
  12.     #print_r( $sw->AclDeleteIp('133.133.133.133') );
  13.     #print_r( $sw->AclInit() );
  14.     #print_r( $sw->AclLockPort(5) );
  15.     print_r( $sw->Nop('ping') );
  16.     #print_r( $sw->AclGetFreeNumber() );
  17.     #print_r( $sw->GetAclByIP('159.224.175.34') );
  18.  
  19.     $sw->Logout();
  20.     }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.     class TSwitch {
  27.         var $ip = null;
  28.         var $model = null;
  29.         var $telnet = null;
  30.         var $connected = 0;
  31.         var $permitProfile = 6;
  32.         var $denyProfile = 10;
  33.  
  34.         function TSwitch($switchinfo,$user,$pass) {
  35.             require_once "include/PHPTelnet.php";
  36.             $this->telnet = new PHPTelnet();
  37.             $this->ip = $switchinfo['ip'];
  38.             $result = $this->telnet->Connect($this->ip,$user,$pass);
  39.             if ($result != 0) { return 'error'; }
  40.             $this->connect = 1;
  41.             $this->model = $switchinfo['model'];
  42.             $this->acl_deny_profile = $switchinfo['acl_deny_profile'];
  43.             $this->billing_ports = $switchinfo['billing_ports'];
  44.         }
  45.  
  46.  
  47.         #
  48.         # Σ╠╤ ╧▐╔╙╘╦╔ ╘┼╥═╔╬┴╠┴
  49.         #
  50.         function Nop() {
  51.             return $this->telnet->Nop( 'ping' );
  52.         }
  53.  
  54.         #
  55.         # ≡╧╠╒▐╔╘╪ ╙╨╔╙╧╦ ßπ∞ ─╠╤ ╦╧╬╦╥┼╘╬╧╟╧ ╨╥╧╞╔╠╤
  56.         #
  57.         function AclGetList($canmore=0) {
  58.             if (!function_exists('_GetAclList_cb'))
  59.             {
  60.                 function _GetAclList_cb($v) {
  61.                     if( stripos( $v, 'add' ) === false ) { return false; }
  62.                     if( stripos( $v, 'access_id' ) === false ) { return false; }
  63.                     return true;
  64.                 }
  65.             }
  66.             $res = '';
  67.             if( $this->model == 'DGS-3100' ) {
  68.                 $res = $this->telnet->DoCommand("show configuration running include access_profile\r\na\x7f");
  69.             } elseif( $this->model == 'DGS-3120-24SC' ) {
  70.                 $res = $this->telnet->DoCommand("show config current_config include \"access_profile\"\r\na\x08");
  71.             }
  72.             $res = explode( "\n", $res );
  73.             $res = array_filter( $res, '_GetAclList_cb' );
  74.             return $res;
  75.         }
  76.  
  77.         #
  78.         # ≡╧╠╒▐╔╘╪ ╬╧═┼╥ ╨╥┴╫╔╠┴ ╨╧ ╔╨, ╔╠╔ 0 ┼╙╠╔ ╘┴╦╧╟╧ ╬┼╘
  79.         #
  80.         function AclGetByIP($ip) {
  81.             $list = $this->AclGetList();
  82.             #print_r($list);
  83.             foreach( $list as $s ) {
  84.                 #print "-=$s=- |||$ip|||<br>";
  85.                 if( strpos( $s, $ip ) && (strpos( $s, 'auto' )===FALSE) ) {
  86.                     #print " found ";
  87.                     #print strpos( $s, $ip );
  88.                     #print strpos( $s, 'auto' );
  89.                     preg_match( '/access_id\s+(\d+)\s+ip/', $s, $m );
  90.                     return $m[1];
  91.                 }
  92.             }
  93.             return 0;
  94.         }
  95.  
  96.         #
  97.         # ≡╧╠╒▐╔╘╪ ╙╫╧┬╧─╬┘╩ ╬╧═┼╥ ─╠╤ ╨╥┴╫╔╠┴
  98.         #
  99.         function AclGetFreeNumber() {
  100.             $list = $this->AclGetList();
  101.             $num = 1;
  102.             //print_r($list);
  103.             foreach( $list as $s ) {
  104.                 #print "AclGetByIP: examine $s<br>\n";
  105.                 if( !preg_match( "/profile_id\s+$this->acl_deny_profile\s+add\s+access_id\s+(\d+)\s+ip/", $s, $m ) ) {
  106.                     continue;
  107.                 }
  108.                 //print "==> $s\n";
  109.                 if( $m[1] < 1 ) { continue; }
  110.                 if( $num != $m[1] ) {
  111.                     return $num;
  112.                 }
  113.                 $num++;
  114.             }
  115.             return $num;
  116.         }
  117.  
  118.         function AclBlockIP($ip,$port) {
  119.             $number = 'auto_assign';
  120.             if( $this->model == 'DGS-3120-24SC' ) {
  121.                 $number = $this->AclGetFreeNumber();
  122.             }
  123.             $cmd = "config access_profile profile_id $this->acl_deny_profile add access_id $number ip source_ip $ip port $port deny";
  124.             //print $cmd;
  125.             $this->telnet->DoCommand( $cmd );
  126.             $this->telnet->WaitFor("Success",40);
  127.         }
  128.  
  129.         function AclUnblockIP($ip) {
  130.             $num = $this->AclGetByIP($ip);
  131.             if( $num ) {
  132.                 $this->telnet->DoCommand("config access_profile profile_id $this->acl_deny_profile delete access_id $num");
  133.             }
  134.         }
  135.  
  136.         function AclAddIP($ip,$port) {
  137.             $num = $this->AclGetFreeNumber();
  138.             #print "num is $num ";
  139.             #print ("config access_profile profile_id 4  add access_id $num  ip  source_ip $ip port $port permit");
  140.             $this->telnet->DoCommand("config access_profile profile_id 4  add access_id $num  ip  source_ip $ip port $port permit");
  141.         }
  142.  
  143.         function AclDeleteIP($ip) {
  144.             $num = $this->AclGetByIP($ip);
  145.             #print "num is $num";
  146.             $this->telnet->DoCommand("config access_profile profile_id 4  delete access_id $num");
  147.             #$this->telnet->DoCommand("delete !!!");
  148.         }
  149.  
  150.         function AclLockPort($port) {
  151.             return $this->telnet->DoCommand("config access_profile profile_id 10  add access_id $port ip source_ip 0.0.0.0 port $port deny");
  152.         }
  153.  
  154.         function AclUnlockPort($port) {
  155.             return $this->telnet->DoCommand("config access_profile profile_id 10  delete access_id $port");
  156.         }
  157.  
  158.         function AclInit() {
  159.             ## access to billing
  160.             $this->DoCommand("create access_profile profile_id 3 ip destination_ip_mask 255.255.255.255");
  161.             $this->DoCommand("config access_profile profile_id 3  add access_id 1  ip  destination_ip 195.182.202.3 port 1-24 permit");
  162.             ## access to internet
  163.             $this->DoCommand("create access_profile profile_id 4 ip source_ip_mask 255.255.255.255");
  164.             ## deny other traf
  165.             $this->DoCommand("create access_profile profile_id 10 ip source_ip_mask 0.0.0.0");
  166.         }
  167.  
  168.         function Logout() {
  169.             #if( !isset( $ip ) ) { return 0; }
  170.             $this->telnet->DoCommand('logout');
  171.             $this->telnet->Disconnect(0);
  172.         }
  173.  
  174.         function DoCommand( $cmd ) {
  175.             #if( $ip ) { return 0; }
  176.             #print "$cmd<br>";
  177.             return $this->telnet->DoCommand( $cmd );
  178.         }
  179.     }
  180.  
  181. ?>
  182.