benclerc/fortinet-fortiosapi

用于与Fortigate防火墙(FortiOS)API交互的PHP库。

6.4.5 2023-06-06 16:33 UTC

This package is auto-updated.

Last update: 2024-09-06 19:54:09 UTC


README

此库是自动生成的,如果您想支持新版本,请提交问题。

用于与Fortigate防火墙(FortiOS)API(CMDB(配置)、日志和监控)交互的PHP库。此库可以检索、创建、更新和删除防火墙上的配置。

您可以在Fortinet开发者网站上找到所有支持的方法,浏览信息需要账号。

目录

入门

  1. 获取 Composer
  2. 使用composer安装此库 composer require benclerc/fortinet-fortiosapi
  3. 将以下内容添加到应用程序的主PHP文件中 require 'vendor/autoload.php';
  4. 使用防火墙的主机名、用户名和密码实例化Config类 $configConnection = new \Fortinet\FortiOSAPI\Config('123.123.123.123', 'admin', 'password');
  5. 使用之前创建的Config对象实例化FortiOSAPI对象 $firewallConf = new \Fortinet\FortiOSAPI\Configuration($configConnection);
  6. 开始使用此库 $staticRoutes = $firewallConf->getAllRouterStatic();

文档

您可以在此处找到完整的文档。

Config类

用法

此Config类用于准备实例化和使用FortiOSAPI...类所需的必备配置信息。在构造函数中,您必须传递

  1. 防火墙的主机名(FQDN)或IP地址
  2. 有效的用户名
  3. 有效的用户密码

可选参数

  • 超时时间:5000ms。使用setTimeout()更改。
  • SSL验证对方选项:TRUE。使用setSSLVerifyPeer()更改。
  • SSL验证主机选项:2。使用setSSLVerifyHost()更改。
  • API版本:2。使用setAPIVersion()更改。

示例

// Basic configuration
$configConnection = new \Fortinet\FortiOSAPI\Config('123.123.123.123', 'admin', 'password');

// Configuration for very slow firewalls/long requests
$configConnection = new \Fortinet\FortiOSAPI\Config('123.123.123.123', 'admin', 'password');
$configConnection->setTimeout(20000);

// Unsecure configuration
$configConnection = new \Fortinet\FortiOSAPI\Config('123.123.123.123', 'admin', 'password');
$configConnection->setSSLVerifyPeer(FALSE)->setSSLVerifyHost(FALSE);

// Special API version
$configConnection = new \Fortinet\FortiOSAPI\Config('123.123.123.123', 'admin', 'password');
$configConnection->setAPIVersion(1);

// The class logins to the firewall when being instanciated hence the try/catch statement.
// Here I use the class Configuration for the example but it the same for Log and Monitor classes.
try {
	$firewallConf = new \Fortinet\FortiOSAPI\Configuration($configConnection);
} catch (Exception $e) {
	echo('Handle error : '.$e->getMessage());
}

配置、日志和监控类

用法

这些类使用异常来处理错误,对于正常的执行,您应该在try/catch语句中实例化和请求方法。

示例

// Get one particular static route
try {
	$res = $firewallConf->getRouterStatic(1);
	echo('Gateway is : '.$res->results[0]->gateway);
} catch (Exception $e) {
	echo('Handle error : '.$e->getMessage());
}

// Get routes using filters
try {
	// Will return fields dst and status of default gateways
	$res = $firewallConf->getAllRouterStatic(NULL, NULL, NULL, NULL, NULL, NULL, ['dst', 'status'], ['dst==0.0.0.0 0.0.0.0']);
	// For obvious reasons, it would be a charm to use the new PHP 8.0 syntax to call the method :
	// $firewallConf->getAllRouterStatic(format: ['dst', 'status'], filter: ['dst==0.0.0.0 0.0.0.0'])
} catch (Exception $e) {
	echo('Handle error : '.$e->getMessage());
}


// Set a static route
// Define the route
$staticRoute = new stdClass;
$staticRoute->status = 'enable';
$staticRoute->dst = '1.1.1.1 255.255.255.255';
$staticRoute->src = '198.168.1.0 255.255.255.0';
$staticRoute->gateway = '198.168.1.254';
$staticRoute->device = 'lan';
$staticRoute->distance = 20;

// Send the request to the firewall
try {
	$res = $firewallConf->addRouterStatic($staticRoute);

	if ($res->status == 'success') {
		echo('Route added');
	} else {
		echo('Route adding failed');
	}
} catch (Exception $e) {
	echo('Handle error : '.$e->getMessage());
}

事务

此库还支持事务:开始事务,创建、更新、删除,并根据结果提交或中止您的更改。

// Start transaction
$firewallConf->startTransaction();

// Create many IP objects
$error = FALSE;
for ($i=1; $i < 50; $i++) {
	// Create body object
	$ip = new stdClass;
	$ip->name = 'IP'.$i;
	$ip->type = 'subnet';
	$ip->subnet = '10.1.'.$i.'.0/24';

	// Create object on the firewall
	try {
		$firewallConf->addFirewallAddress($ip);
		echo("[SUCCESS] Created IP ".$ip->name.".\n");
	} catch (Exception $e) {
		echo("[ERROR] Unable to create IP : ".$ip->name.". Details : ".$e->getMessage()."\n");
		$error = TRUE;
	}
}

// Check error
if ($error === FALSE) {
	// No errors, commit
	$firewallConf->commitTransaction();
} else {
	// Errors, abort and rollback
	$firewallConf->abortTransaction();
}