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
Wrap
PHP Script
|
2014-03-02
|
5KB
|
182 lines
<?
if( 0 ) {
print "<pre>";
#$sw = new TSwitch('10.57.124.9','teremok','mypass88');
$sw = new TSwitch('192.168.2.66','admin','mypass888');
#print $sw->DoCommand('show config current_config include "config access_profile profile_id 4"');
#print $sw->DoCommand("a");
print_r( $sw->AclGetList() );
#print_r( $sw->AclAddIp('133.133.133.133',5) );
#print_r( $sw->AclDeleteIp('133.133.133.133') );
#print_r( $sw->AclInit() );
#print_r( $sw->AclLockPort(5) );
print_r( $sw->Nop('ping') );
#print_r( $sw->AclGetFreeNumber() );
#print_r( $sw->GetAclByIP('159.224.175.34') );
$sw->Logout();
}
class TSwitch {
var $ip = null;
var $model = null;
var $telnet = null;
var $connected = 0;
var $permitProfile = 6;
var $denyProfile = 10;
function TSwitch($switchinfo,$user,$pass) {
require_once "include/PHPTelnet.php";
$this->telnet = new PHPTelnet();
$this->ip = $switchinfo['ip'];
$result = $this->telnet->Connect($this->ip,$user,$pass);
if ($result != 0) { return 'error'; }
$this->connect = 1;
$this->model = $switchinfo['model'];
$this->acl_deny_profile = $switchinfo['acl_deny_profile'];
$this->billing_ports = $switchinfo['billing_ports'];
}
#
# Σ╠╤ ╧▐╔╙╘╦╔ ╘┼╥═╔╬┴╠┴
#
function Nop() {
return $this->telnet->Nop( 'ping' );
}
#
# ≡╧╠╒▐╔╘╪ ╙╨╔╙╧╦ ßπ∞ ─╠╤ ╦╧╬╦╥┼╘╬╧╟╧ ╨╥╧╞╔╠╤
#
function AclGetList($canmore=0) {
if (!function_exists('_GetAclList_cb'))
{
function _GetAclList_cb($v) {
if( stripos( $v, 'add' ) === false ) { return false; }
if( stripos( $v, 'access_id' ) === false ) { return false; }
return true;
}
}
$res = '';
if( $this->model == 'DGS-3100' ) {
$res = $this->telnet->DoCommand("show configuration running include access_profile\r\na\x7f");
} elseif( $this->model == 'DGS-3120-24SC' ) {
$res = $this->telnet->DoCommand("show config current_config include \"access_profile\"\r\na\x08");
}
$res = explode( "\n", $res );
$res = array_filter( $res, '_GetAclList_cb' );
return $res;
}
#
# ≡╧╠╒▐╔╘╪ ╬╧═┼╥ ╨╥┴╫╔╠┴ ╨╧ ╔╨, ╔╠╔ 0 ┼╙╠╔ ╘┴╦╧╟╧ ╬┼╘
#
function AclGetByIP($ip) {
$list = $this->AclGetList();
#print_r($list);
foreach( $list as $s ) {
#print "-=$s=- |||$ip|||<br>";
if( strpos( $s, $ip ) && (strpos( $s, 'auto' )===FALSE) ) {
#print " found ";
#print strpos( $s, $ip );
#print strpos( $s, 'auto' );
preg_match( '/access_id\s+(\d+)\s+ip/', $s, $m );
return $m[1];
}
}
return 0;
}
#
# ≡╧╠╒▐╔╘╪ ╙╫╧┬╧─╬┘╩ ╬╧═┼╥ ─╠╤ ╨╥┴╫╔╠┴
#
function AclGetFreeNumber() {
$list = $this->AclGetList();
$num = 1;
//print_r($list);
foreach( $list as $s ) {
#print "AclGetByIP: examine $s<br>\n";
if( !preg_match( "/profile_id\s+$this->acl_deny_profile\s+add\s+access_id\s+(\d+)\s+ip/", $s, $m ) ) {
continue;
}
//print "==> $s\n";
if( $m[1] < 1 ) { continue; }
if( $num != $m[1] ) {
return $num;
}
$num++;
}
return $num;
}
function AclBlockIP($ip,$port) {
$number = 'auto_assign';
if( $this->model == 'DGS-3120-24SC' ) {
$number = $this->AclGetFreeNumber();
}
$cmd = "config access_profile profile_id $this->acl_deny_profile add access_id $number ip source_ip $ip port $port deny";
//print $cmd;
$this->telnet->DoCommand( $cmd );
$this->telnet->WaitFor("Success",40);
}
function AclUnblockIP($ip) {
$num = $this->AclGetByIP($ip);
if( $num ) {
$this->telnet->DoCommand("config access_profile profile_id $this->acl_deny_profile delete access_id $num");
}
}
function AclAddIP($ip,$port) {
$num = $this->AclGetFreeNumber();
#print "num is $num ";
#print ("config access_profile profile_id 4 add access_id $num ip source_ip $ip port $port permit");
$this->telnet->DoCommand("config access_profile profile_id 4 add access_id $num ip source_ip $ip port $port permit");
}
function AclDeleteIP($ip) {
$num = $this->AclGetByIP($ip);
#print "num is $num";
$this->telnet->DoCommand("config access_profile profile_id 4 delete access_id $num");
#$this->telnet->DoCommand("delete !!!");
}
function AclLockPort($port) {
return $this->telnet->DoCommand("config access_profile profile_id 10 add access_id $port ip source_ip 0.0.0.0 port $port deny");
}
function AclUnlockPort($port) {
return $this->telnet->DoCommand("config access_profile profile_id 10 delete access_id $port");
}
function AclInit() {
## access to billing
$this->DoCommand("create access_profile profile_id 3 ip destination_ip_mask 255.255.255.255");
$this->DoCommand("config access_profile profile_id 3 add access_id 1 ip destination_ip 195.182.202.3 port 1-24 permit");
## access to internet
$this->DoCommand("create access_profile profile_id 4 ip source_ip_mask 255.255.255.255");
## deny other traf
$this->DoCommand("create access_profile profile_id 10 ip source_ip_mask 0.0.0.0");
}
function Logout() {
#if( !isset( $ip ) ) { return 0; }
$this->telnet->DoCommand('logout');
$this->telnet->Disconnect(0);
}
function DoCommand( $cmd ) {
#if( $ip ) { return 0; }
#print "$cmd<br>";
return $this->telnet->DoCommand( $cmd );
}
}
?>