本模块提供装备网络管理本领,包罗查询装备IP地址、MAC地址信息等。
说明
本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。
本模块接口仅对[装备管理应用]开放,需将装备管理应用激活后调用,实现相应功能。
导入模块
- import { networkManager } from '@kit.MDMKit';
复制代码 networkManager.getAllNetworkInterfacesSync
getAllNetworkInterfacesSync(admin: Want): Array
指定装备管理应用获取全部激活的网络接口。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。 返回值:
类型说明Array返回网络接口名称数组。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- try {
- let result: Array<string> = networkManager.getAllNetworkInterfacesSync(wantTemp);
- console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`);
- } catch (err) {
- console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`);
- }
复制代码 networkManager.getIpAddressSync
getIpAddressSync(admin: Want, networkInterface: string): string
指定装备管理应用根据网络接口获取装备IP地址。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。networkInterfacestring是指定网络接口。 返回值:
类型说明string返回装备IP地址。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- try {
- let result: string = networkManager.getIpAddressSync(wantTemp, 'eth0');
- console.info(`Succeeded in getting ip address, result : ${result}`);
- } catch (err) {
- console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`);
- }
复制代码 networkManager.getMacSync
getMacSync(admin: Want, networkInterface: string): string
指定装备管理应用根据网络接口获取装备MAC地址。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。networkInterfacestring是指定网络接口。 返回值:
类型说明string返回装备MAC地址。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- try {
- let result: string = networkManager.getMacSync(wantTemp, 'eth0');
- console.info(`Succeeded in getting mac, result : ${result}`);
- } catch (err) {
- console.error(`Failed to get mac. Code: ${err.code}, message: ${err.message}`);
- }
复制代码 networkManager.isNetworkInterfaceDisabledSync
isNetworkInterfaceDisabledSync(admin: Want, networkInterface: string): boolean
指定装备管理应用查询指定网络接口是否被禁用。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。networkInterfacestring是指定网络接口。 返回值:
类型说明boolean返回指定网络接口是否被禁用,true表示该网络接口被禁用,false表示该网络接口未被禁用。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- try {
- let result: boolean = networkManager.isNetworkInterfaceDisabledSync(wantTemp, 'eth0');
- console.info(`Succeeded in querying network interface is disabled or not, result : ${result}`);
- } catch (err) {
- console.error(`Failed to query network interface is disabled or not. Code: ${err.code}, message: ${err.message}`);
- }
复制代码 networkManager.setNetworkInterfaceDisabledSync
setNetworkInterfaceDisabledSync(admin: Want, networkInterface: string, isDisabled: boolean): void
指定装备管理应用克制装备使用指定网络。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。networkInterfacestring是指定网络接口。isDisabledboolean是true表示禁用该网络接口,false表示开启该网络接口。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- try {
- networkManager.setNetworkInterfaceDisabledSync(wantTemp, 'eth0', true);
- console.info(`Succeeded in setting network interface disabled`);
- } catch (err) {
- console.error(`Failed to set network interface disabled. Code: ${err.code}, message: ${err.message}`);
- }
复制代码 networkManager.setGlobalProxySync
setGlobalProxySync(admin: Want, httpProxy: connection.HttpProxy): void
指定装备管理应用设置网络全局代理。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。httpProxy[connection.HttpProxy]是网络全局Http代理设置信息。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- import { connection } from '@kit.NetworkKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- let exclusionStr: string = "192.168,baidu.com"
- let exclusionArray: Array<string> = exclusionStr.split(',');
- let httpProxy: connection.HttpProxy = {
- host: "192.168.xx.xxx",
- port: 8080,
- exclusionList: exclusionArray
- };
- try {
- networkManager.setGlobalProxySync(wantTemp, httpProxy);
- console.info(`Succeeded in setting network global proxy.`);
- } catch (err) {
- console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`);
- }
复制代码 networkManager.getGlobalProxySync
getGlobalProxySync(admin: Want): connection.HttpProxy
指定装备管理应用获取网络全局代理。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。 返回值:
类型说明[connection.HttpProxy]返回网络全局Http代理设置信息。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- import { connection } from '@kit.NetworkKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- try {
- let result: connection.HttpProxy = networkManager.getGlobalProxySync(wantTemp);
- console.info(`Succeeded in getting network global proxy, result : ${JSON.stringify(result)}`);
- } catch (err) {
- console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`);
- }
复制代码 networkManager.addFirewallRule
addFirewallRule(admin: Want, firewallRule: FirewallRule): void
指定装备管理应用为装备添加防火墙过滤规则。
添加了[Action]为ALLOW规则后,将会默认添加DENY规则,不在ALLOW规则之内的网络数据包将会被丢弃或拦截。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。firewallRule[FirewallRule]是添加防火墙过滤规则。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- let firewallRule: networkManager.FirewallRule = {
- "srcAddr": "192.168.1.1-192.188.22.66",
- "destAddr": "10.1.1.1",
- "srcPort": "8080",
- "destPort": "8080",
- "appUid": "9696",
- "direction": networkManager.Direction.OUTPUT,
- "action": networkManager.Action.DENY,
- "protocol": networkManager.Protocol.UDP,
- }
- networkManager.addFirewallRule(wantTemp, firewallRule);
复制代码 networkManager.removeFirewallRule
removeFirewallRule(admin: Want, firewallRule?: FirewallRule): void
指定装备管理应用移除装备防火墙过滤规则。
移除规则后如果不存在[Action]为ALLOW规则后,会将[addFirewallRule]添加的默认DENY规则清空。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。firewallRule[FirewallRule]否移除防火墙过滤规则。值为空时,清空全部的防火墙规则。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- // 移除指定的规则
- let firewallRule: networkManager.FirewallRule = {
- "srcAddr": "192.168.1.1-192.188.22.66",
- "destAddr": "10.1.1.1",
- "srcPort": "8080",
- "destPort": "8080",
- "appUid": "9696",
- "direction": networkManager.Direction.OUTPUT,
- "action": networkManager.Action.DENY,
- "protocol": networkManager.Protocol.UDP,
- }
- networkManager.removeFirewallRule(wantTemp, firewallRule);
- // 清空所有规则
- networkManager.removeFirewallRule(wantTemp);
复制代码 networkManager.getFirewallRules
getFirewallRules(admin: Want): Array
指定装备管理应用查询防火墙过滤规则。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。 返回值:
类型说明Array<[FirewallRule]>返回当前装备设置的防火墙过滤规则列表,当方法调用错误时会抛出非常。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- let firewallRule: Array<networkManager.FirewallRule>;
- firewallRule = networkManager.getFirewallRules(wantTemp);
复制代码 networkManager.addDomainFilterRule
addDomainFilterRule(admin: Want, domainFilterRule: DomainFilterRule): void
指定装备管理应用为装备添加域名过滤规则。
添加了[Action]为ALLOW规则后,将会默认添加DENY规则,不在ALLOW规则之内的域名解析数据包将会被丢弃或拦截。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。domainFilterRule[DomainFilterRule]是添加域名过滤规则。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- let domainFilterRule: networkManager.DomainFilterRule = {
- "domainName": "www.example.com",
- "appUid": "9696",
- "action": networkManager.Action.DENY,
- }
- networkManager.addDomainFilterRule(wantTemp, domainFilterRule);
复制代码 networkManager.removeDomainFilterRule
removeDomainFilterRule(admin: Want, domainFilterRule?: DomainFilterRule): void
指定装备管理应用移除装备域名过滤规则。
移除规则后如果不存在[Action]为ALLOW规则后,会将[addDomainFilterRule]添加的默认DENY规则清空。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。domainFilterRule[DomainFilterRule]否移除域名过滤规则。值为空时,清空全部的域名规则。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- // 移除指定的规则
- let domainFilterRule: networkManager.DomainFilterRule = {
- "domainName": "www.example.com",
- "appUid": "9696",
- "action": networkManager.Action.DENY,
- }
- networkManager.removeDomainFilterRule(wantTemp, domainFilterRule);
- // 清空所有规则
- networkManager.removeDomainFilterRule(wantTemp);
复制代码 networkManager.getDomainFilterRules
getDomainFilterRules(admin: Want): Array
指定装备管理应用查询域名过滤规则。
必要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
参数:
参数名类型必填说明admin[Want]是装备管理应用。 返回值:
类型说明Array<[DomainFilterRule]>返回当前装备设置的域名过滤规则列表,当方法调用错误时会抛出非常。 错误码ID错误信息9200001The application is not an administrator application of the device.9200002The administrator application does not have permission to manage the device.201Permission verification failed. The application does not have the permission required to call the API.401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. 示例:
- import { Want } from '@kit.AbilityKit';
- let wantTemp: Want = {
- bundleName: 'com.example.myapplication',
- abilityName: 'EntryAbility',
- };
- let domainFilterRule: Array<networkManager.DomainFilterRule>;
- domainFilterRule = networkManager.getDomainFilterRules(wantTemp);kManager.DomainFilterRule>;domainFilterRule = networkManager.getDomainFilterRules(wantTemp);
复制代码 FirewallRule
防火墙过滤规则。
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
名称类型必填说明srcAddrstring否ip源地址。支持IP段,例如:192.168.0.0/22大概192.168.1.100-192.168.1.200destAddrstring否ip目标地址。支持IP段,例如:192.168.0.0/22大概192.168.1.100-192.168.1.200srcPortstring否源端口。destPortstring否目标端口。appUidstring否应用uid。direction[Direction]否规则链。添加防护墙过滤规则时必填;移除防火墙时非必填,表示清空全部的[Direction]链。当值为空时srcAddr,destAddr,srcPort,destPort,appUid也必须传入空值。action[Action]否接收大概丢弃数据包。添加防护墙过滤规则时必填;移除防火墙时非必填,表示清空全部的匹配[Action]规则的链。当值为空时srcAddr,destAddr,srcPort,destPort,appUid也必须传入空值。protocol[Protocol]否网络协议。当值为ALL大概ICMP时,不允许设置srcPort与destPort。 DomainFilterRule
域名过滤规则。
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
名称类型必填说明domainNamestring否域名。添加域名过滤规则时必填。appUidstring否应用uid。action[Action]否接收大概丢弃数据包。添加域名过滤规则时必填;移除域名过滤规则时非必填,表示清空全部的匹配[Action]规则的链。当值为空时,domainName,appUid也必须传入空值。 Direction
规则链。
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
名称值说明INPUT0输入链。OUTPUT1输出链。 Action
数据包的行为。
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
名称值说明ALLOW0接收数据包。DENY1丢弃数据包。 Protocol
网络协议。
体系本领: SystemCapability.Customization.EnterpriseDeviceManager
名称值说明ALL0全部网络协议。TCP1网络协议TCP。UDP2网络协议UDP。ICMP3网络协议ICMP。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |