1e41f4b71Sopenharmony_ci# @ohos.net.ethernet (Ethernet Connection Management) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **ethernet** module provides wired network capabilities, which allow users to set the IP address, subnet mask, gateway, and Domain Name System (DNS) server, and HTTP proxy of a wired network. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 7e41f4b71Sopenharmony_ci> The APIs provided by this module are system APIs. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Modules to Import 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## ethernet.setIfaceConfig<sup>9+</sup> 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_cisetIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback\<void>): void 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciSets the network interface configuration. This API uses an asynchronous callback to return the result. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**System API**: This is a system API. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**Parameters** 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 30e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | ---- | ------------------------------------------ | 31e41f4b71Sopenharmony_ci| iface | string | Yes | Interface name. | 32e41f4b71Sopenharmony_ci| ic | [InterfaceConfiguration](#interfaceconfiguration9) | Yes | Network interface configuration to set. | 33e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.| 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**Error codes** 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| ID| Error Message | 38e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 39e41f4b71Sopenharmony_ci| 201 | Permission denied. | 40e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 41e41f4b71Sopenharmony_ci| 401 | Parameter error. | 42e41f4b71Sopenharmony_ci| 2200001 | Invalid parameter value. | 43e41f4b71Sopenharmony_ci| 2200002 | Failed to connect to the service. | 44e41f4b71Sopenharmony_ci| 2200003 | System internal error. | 45e41f4b71Sopenharmony_ci| 2201004 | Invalid Ethernet profile. | 46e41f4b71Sopenharmony_ci| 2201005 | Device information does not exist. | 47e41f4b71Sopenharmony_ci| 2201006 | Ethernet device not connected. | 48e41f4b71Sopenharmony_ci| 2201007 | Ethernet failed to write user configuration information. | 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**Example** 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci```ts 53e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 54e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_cilet config: ethernet.InterfaceConfiguration = { 57e41f4b71Sopenharmony_ci mode: 0, 58e41f4b71Sopenharmony_ci ipAddr: "192.168.xx.xxx", 59e41f4b71Sopenharmony_ci route: "192.168.xx.xxx", 60e41f4b71Sopenharmony_ci gateway: "192.168.xx.xxx", 61e41f4b71Sopenharmony_ci netMask: "255.255.255.0", 62e41f4b71Sopenharmony_ci dnsServers: "1.1.1.1" 63e41f4b71Sopenharmony_ci}; 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ciethernet.setIfaceConfig("eth0", config, (error: BusinessError) => { 66e41f4b71Sopenharmony_ci if (error) { 67e41f4b71Sopenharmony_ci console.log("setIfaceConfig callback error = " + JSON.stringify(error)); 68e41f4b71Sopenharmony_ci } else { 69e41f4b71Sopenharmony_ci console.log("setIfaceConfig callback ok"); 70e41f4b71Sopenharmony_ci } 71e41f4b71Sopenharmony_ci}); 72e41f4b71Sopenharmony_ci``` 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci## ethernet.setIfaceConfig<sup>9+</sup> 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_cisetIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void> 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ciSets the network interface configuration. This API uses a promise to return the result. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**System API**: This is a system API. 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci**Parameters** 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 89e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------- | ---- | ------------------------ | 90e41f4b71Sopenharmony_ci| iface | string | Yes | Interface name. | 91e41f4b71Sopenharmony_ci| ic | [InterfaceConfiguration](#interfaceconfiguration9) | Yes | Network interface configuration to set.| 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci**Return value** 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci| Type | Description | 96e41f4b71Sopenharmony_ci| ------------------- | ----------------------------------------------------------- | 97e41f4b71Sopenharmony_ci| Promise\<void> | Promise used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.| 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci**Error codes** 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci| ID| Error Message | 102e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 103e41f4b71Sopenharmony_ci| 201 | Permission denied. | 104e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 105e41f4b71Sopenharmony_ci| 401 | Parameter error. | 106e41f4b71Sopenharmony_ci| 2200001 | Invalid parameter value. | 107e41f4b71Sopenharmony_ci| 2200002 |Failed to connect to the service. | 108e41f4b71Sopenharmony_ci| 2200003 | System internal error. | 109e41f4b71Sopenharmony_ci| 2201004 | Invalid Ethernet profile. | 110e41f4b71Sopenharmony_ci| 2201005 | Device information does not exist. | 111e41f4b71Sopenharmony_ci| 2201006 | Ethernet device not connected. | 112e41f4b71Sopenharmony_ci| 2201007 | Ethernet failed to write user configuration information. | 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci**Example** 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci```ts 117e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 118e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_cilet config: ethernet.InterfaceConfiguration = { 121e41f4b71Sopenharmony_ci mode: 0, 122e41f4b71Sopenharmony_ci ipAddr: "192.168.xx.xxx", 123e41f4b71Sopenharmony_ci route: "192.168.xx.xxx", 124e41f4b71Sopenharmony_ci gateway: "192.168.xx.xxx", 125e41f4b71Sopenharmony_ci netMask: "255.255.255.0", 126e41f4b71Sopenharmony_ci dnsServers: "1.1.1.1" 127e41f4b71Sopenharmony_ci}; 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciconst setConfigPromise = ethernet.setIfaceConfig("eth0", config); 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_cisetConfigPromise.then(() => { 132e41f4b71Sopenharmony_ci console.log("setIfaceConfig promise ok"); 133e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 134e41f4b71Sopenharmony_ci console.log("setIfaceConfig promise error = " + JSON.stringify(error)); 135e41f4b71Sopenharmony_ci}); 136e41f4b71Sopenharmony_ci``` 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci## ethernet.getIfaceConfig<sup>9+</sup> 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_cigetIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>): void 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ciObtains the configuration of a network interface. This API uses an asynchronous callback to return the result. 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci**System API**: This is a system API. 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_NETWORK_INFO 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci**Parameters** 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 153e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------- | ----- | ------------ | 154e41f4b71Sopenharmony_ci| iface | string | Yes | Interface name.| 155e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[InterfaceConfiguration](#interfaceconfiguration9)> | Yes | Callback used to return the result. | 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**Error codes** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci| ID| Error Message | 160e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 161e41f4b71Sopenharmony_ci| 201 | Permission denied. | 162e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 163e41f4b71Sopenharmony_ci| 401 | Parameter error. | 164e41f4b71Sopenharmony_ci| 2200001 | Invalid parameter value. | 165e41f4b71Sopenharmony_ci| 2200002 | Failed to connect to the service. | 166e41f4b71Sopenharmony_ci| 2200003 | System internal error. | 167e41f4b71Sopenharmony_ci| 2201005 | Device information does not exist. | 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**Example** 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci```ts 172e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 173e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ciethernet.getIfaceConfig("eth0", (error: BusinessError, value: ethernet.InterfaceConfiguration) => { 176e41f4b71Sopenharmony_ci if (error) { 177e41f4b71Sopenharmony_ci console.log("getIfaceConfig callback error = " + JSON.stringify(error)); 178e41f4b71Sopenharmony_ci } else { 179e41f4b71Sopenharmony_ci console.log("getIfaceConfig callback mode = " + JSON.stringify(value.mode)); 180e41f4b71Sopenharmony_ci console.log("getIfaceConfig callback ipAddr = " + JSON.stringify(value.ipAddr)); 181e41f4b71Sopenharmony_ci console.log("getIfaceConfig callback route = " + JSON.stringify(value.route)); 182e41f4b71Sopenharmony_ci console.log("getIfaceConfig callback gateway = " + JSON.stringify(value.gateway)); 183e41f4b71Sopenharmony_ci console.log("getIfaceConfig callback netMask = " + JSON.stringify(value.netMask)); 184e41f4b71Sopenharmony_ci console.log("getIfaceConfig callback dnsServers = " + JSON.stringify(value.dnsServers)); 185e41f4b71Sopenharmony_ci } 186e41f4b71Sopenharmony_ci}); 187e41f4b71Sopenharmony_ci``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci## ethernet.getIfaceConfig<sup>9+</sup> 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_cigetIfaceConfig(iface: string): Promise\<InterfaceConfiguration> 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ciObtains the configuration of a network interface. This API uses a promise to return the result. 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**System API**: This is a system API. 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_NETWORK_INFO 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**Parameters** 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 204e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ------------ | 205e41f4b71Sopenharmony_ci| iface | string | Yes | Interface name.| 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci**Return value** 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci| Type | Description | 210e41f4b71Sopenharmony_ci| --------------------------------- | ---------------------------------- | 211e41f4b71Sopenharmony_ci| Promise\<[InterfaceConfiguration](#interfaceconfiguration9)> | Promise used to return the result. | 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci**Error codes** 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci| ID| Error Message | 216e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 217e41f4b71Sopenharmony_ci| 201 | Permission denied. | 218e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 219e41f4b71Sopenharmony_ci| 401 | Parameter error. | 220e41f4b71Sopenharmony_ci| 2200001 | Invalid parameter value. | 221e41f4b71Sopenharmony_ci| 2200002 | Failed to connect to the service. | 222e41f4b71Sopenharmony_ci| 2200003 | System internal error. | 223e41f4b71Sopenharmony_ci| 2201005 | Device information does not exist. | 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci**Example** 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci```ts 228e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 229e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ciethernet.getIfaceConfig("eth0").then((data: ethernet.InterfaceConfiguration) => { 232e41f4b71Sopenharmony_ci console.log("getIfaceConfig promise mode = " + JSON.stringify(data.mode)); 233e41f4b71Sopenharmony_ci console.log("getIfaceConfig promise ipAddr = " + JSON.stringify(data.ipAddr)); 234e41f4b71Sopenharmony_ci console.log("getIfaceConfig promise route = " + JSON.stringify(data.route)); 235e41f4b71Sopenharmony_ci console.log("getIfaceConfig promise gateway = " + JSON.stringify(data.gateway)); 236e41f4b71Sopenharmony_ci console.log("getIfaceConfig promise netMask = " + JSON.stringify(data.netMask)); 237e41f4b71Sopenharmony_ci console.log("getIfaceConfig promise dnsServers = " + JSON.stringify(data.dnsServers)); 238e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 239e41f4b71Sopenharmony_ci console.log("getIfaceConfig promise error = " + JSON.stringify(error)); 240e41f4b71Sopenharmony_ci}); 241e41f4b71Sopenharmony_ci``` 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci## ethernet.isIfaceActive<sup>9+</sup> 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ciisIfaceActive(iface: string, callback: AsyncCallback\<number>): void 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ciChecks whether a network interface is active. This API uses an asynchronous callback to return the result. 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci**System API**: This is a system API. 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_NETWORK_INFO 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**Parameters** 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 258e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | -------------------------------------------------- | 259e41f4b71Sopenharmony_ci| iface | string | Yes | Interface name. If this parameter is left empty, the API checks for any active network interface. | 260e41f4b71Sopenharmony_ci| callback | AsyncCallback\<number> | Yes | Callback used to return the result. The value **1** means that the network interface is active, **0** means that the network interface is inactive, and any other value means that an error has occurred.| 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci**Error codes** 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci| ID| Error Message | 265e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 266e41f4b71Sopenharmony_ci| 201 | Permission denied. | 267e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 268e41f4b71Sopenharmony_ci| 401 | Parameter error. | 269e41f4b71Sopenharmony_ci| 2200001 | Invalid parameter value. | 270e41f4b71Sopenharmony_ci| 2200002 | Failed to connect to the service. | 271e41f4b71Sopenharmony_ci| 2200003 | System internal error. | 272e41f4b71Sopenharmony_ci| 2201005 | Device information does not exist. | 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci**Example** 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci```ts 277e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 278e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ciethernet.isIfaceActive("eth0", (error: BusinessError, value: number) => { 281e41f4b71Sopenharmony_ci if (error) { 282e41f4b71Sopenharmony_ci console.log("whether2Activate callback error = " + JSON.stringify(error)); 283e41f4b71Sopenharmony_ci } else { 284e41f4b71Sopenharmony_ci console.log("whether2Activate callback = " + JSON.stringify(value)); 285e41f4b71Sopenharmony_ci } 286e41f4b71Sopenharmony_ci}); 287e41f4b71Sopenharmony_ci``` 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci## ethernet.isIfaceActive<sup>9+</sup> 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ciisIfaceActive(iface: string): Promise\<number> 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ciChecks whether a network interface is active. This API uses a promise to return the result. 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci**System API**: This is a system API. 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_NETWORK_INFO 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci**Parameters** 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 304e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------- | 305e41f4b71Sopenharmony_ci| iface | string | Yes | Interface name. If this parameter is left empty, the API checks for any active network interface.| 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci**Return value** 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci| Type | Description | 310e41f4b71Sopenharmony_ci| ----------------| ------------------------------------------------------------------ | 311e41f4b71Sopenharmony_ci| Promise\<number> | Promise used to return the result. The value **1** means that the network interface is active, **0** means that the network interface is inactive, and any other value means that an error has occurred.| 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci**Error codes** 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci| ID| Error Message | 316e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 317e41f4b71Sopenharmony_ci| 201 | Permission denied. | 318e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 319e41f4b71Sopenharmony_ci| 401 | Parameter error. | 320e41f4b71Sopenharmony_ci| 2200001 | Invalid parameter value. | 321e41f4b71Sopenharmony_ci| 2200002 | Failed to connect to the service. | 322e41f4b71Sopenharmony_ci| 2200003 | System internal error. | 323e41f4b71Sopenharmony_ci| 2201005 | Device information does not exist. | 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci**Example** 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci```ts 328e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 329e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ciethernet.isIfaceActive("eth0").then((data: number) => { 332e41f4b71Sopenharmony_ci console.log("isIfaceActive promise = " + JSON.stringify(data)); 333e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 334e41f4b71Sopenharmony_ci console.log("isIfaceActive promise error = " + JSON.stringify(error)); 335e41f4b71Sopenharmony_ci}); 336e41f4b71Sopenharmony_ci``` 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci## ethernet.getAllActiveIfaces<sup>9+</sup> 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_cigetAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ciObtains the list of all active network interfaces. This API uses an asynchronous callback to return the result. 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci**System API**: This is a system API. 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_NETWORK_INFO 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci**Parameters** 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 353e41f4b71Sopenharmony_ci| -------- | ------------------------------------ | ---- | ------------------------------ | 354e41f4b71Sopenharmony_ci| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return the result.| 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci**Error codes** 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_ci| ID| Error Message | 359e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 360e41f4b71Sopenharmony_ci| 201 | Permission denied. | 361e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 362e41f4b71Sopenharmony_ci| 2200002 | Failed to connect to the service. | 363e41f4b71Sopenharmony_ci| 2200003 | System internal error. | 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci**Example** 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci```ts 368e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 369e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ciethernet.getAllActiveIfaces((error: BusinessError, value: string[]) => { 372e41f4b71Sopenharmony_ci if (error) { 373e41f4b71Sopenharmony_ci console.log("getAllActiveIfaces callback error = " + JSON.stringify(error)); 374e41f4b71Sopenharmony_ci } else { 375e41f4b71Sopenharmony_ci console.log("getAllActiveIfaces callback value.length = " + JSON.stringify(value.length)); 376e41f4b71Sopenharmony_ci for (let i = 0; i < value.length; i++) { 377e41f4b71Sopenharmony_ci console.log("getAllActiveIfaces callback = " + JSON.stringify(value[i])); 378e41f4b71Sopenharmony_ci } 379e41f4b71Sopenharmony_ci } 380e41f4b71Sopenharmony_ci}); 381e41f4b71Sopenharmony_ci``` 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci## ethernet.getAllActiveIfaces<sup>9+</sup> 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_cigetAllActiveIfaces(): Promise\<Array\<string>> 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ciObtains the list of all active network interfaces. This API uses a promise to return the result. 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci**System API**: This is a system API. 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_NETWORK_INFO 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci**Return value** 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci| Type | Description | 398e41f4b71Sopenharmony_ci| ------------------------------ | ----------------------------------------------- | 399e41f4b71Sopenharmony_ci| Promise\<Array\<string>> | Promise used to return the result. | 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci**Error codes** 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci| ID| Error Message | 404e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 405e41f4b71Sopenharmony_ci| 201 | Permission denied. | 406e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 407e41f4b71Sopenharmony_ci| 2200002 | Failed to connect to the service. | 408e41f4b71Sopenharmony_ci| 2200003 | System internal error. | 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci**Example** 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci```ts 413e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 414e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ciethernet.getAllActiveIfaces().then((data: string[]) => { 417e41f4b71Sopenharmony_ci console.log("getAllActiveIfaces promise data.length = " + JSON.stringify(data.length)); 418e41f4b71Sopenharmony_ci for (let i = 0; i < data.length; i++) { 419e41f4b71Sopenharmony_ci console.log("getAllActiveIfaces promise = " + JSON.stringify(data[i])); 420e41f4b71Sopenharmony_ci } 421e41f4b71Sopenharmony_ci}).catch((error:BusinessError) => { 422e41f4b71Sopenharmony_ci console.log("getAllActiveIfaces promise error = " + JSON.stringify(error)); 423e41f4b71Sopenharmony_ci}); 424e41f4b71Sopenharmony_ci``` 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci## ethernet.on('interfaceStateChange')<sup>10+</sup> 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_cion(type: 'interfaceStateChange', callback: Callback\<InterfaceStateInfo>): void 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ciRegisters an observer for NIC hot swap events. This API uses an asynchronous callback to return the result. 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci**System API**: This is a system API. 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_NETWORK_INFO 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**Parameters** 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 441e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ---------- | 442e41f4b71Sopenharmony_ci| type | string | Yes | Event type. The value is **interfaceStateChange**.| 443e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[InterfaceStateInfo](#interfacestateinfo11)> | Yes | Callback used to return the result. | 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci**Error codes** 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci| ID| Error Message | 448e41f4b71Sopenharmony_ci| ------- | -------------------------------------------- | 449e41f4b71Sopenharmony_ci| 201 | Permission denied. | 450e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 451e41f4b71Sopenharmony_ci| 401 | Parameter error. | 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci**Example** 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci```ts 456e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 457e41f4b71Sopenharmony_ci 458e41f4b71Sopenharmony_ciethernet.on('interfaceStateChange', (data: object) => { 459e41f4b71Sopenharmony_ci console.log('on interfaceSharingStateChange: ' + JSON.stringify(data)); 460e41f4b71Sopenharmony_ci}); 461e41f4b71Sopenharmony_ci``` 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ci## ethernet.off('interfaceStateChange')<sup>10+</sup> 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_cioff(type: 'interfaceStateChange', callback?: Callback\<InterfaceStateInfo\>): void 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ciUnregisters the observer for NIC hot swap events. This API uses an asynchronous callback to return the result. 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci**System API**: This is a system API. 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_NETWORK_INFO 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci**Parameters** 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 478e41f4b71Sopenharmony_ci| -------- | --------------------------------------- | ---- | ---------- | 479e41f4b71Sopenharmony_ci| type | string | Yes | Event type. The value is **interfaceStateChange**.| 480e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[InterfaceStateInfo](#interfacestateinfo11)> | No | Callback used to return the result. | 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci**Error codes** 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci| ID| Error Message | 485e41f4b71Sopenharmony_ci| ------- | -------------------------------------------- | 486e41f4b71Sopenharmony_ci| 201 | Permission denied. | 487e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 488e41f4b71Sopenharmony_ci| 401 | Parameter error. | 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ci**Example** 491e41f4b71Sopenharmony_ci 492e41f4b71Sopenharmony_ci```ts 493e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ciethernet.off('interfaceStateChange'); 496e41f4b71Sopenharmony_ci``` 497e41f4b71Sopenharmony_ci 498e41f4b71Sopenharmony_ci## ethernet.getMacAddress<sup>13+</sup> 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_cigetMacAddress(): Promise\<Array\<MacAddressInfo>> 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ciObtains the names and MAC addresses of all Ethernet NICs. This API uses a promise to return the result. 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci**System API**: This is a system API. 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci**Required permission**: ohos.permission.GET_ETHERNET_LOCAL_MAC 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci**Return value** 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci| Type | Description | 513e41f4b71Sopenharmony_ci| --------------------------------- | ---------------------------------- | 514e41f4b71Sopenharmony_ci| Promise\<Array[\<MacAddressInfo>](#macaddressinfo13)> | Promise used to return the result. | 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ci**Error codes** 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci| ID| Error Message | 519e41f4b71Sopenharmony_ci| ------- | ----------------------------------------| 520e41f4b71Sopenharmony_ci| 201 | Permission denied. | 521e41f4b71Sopenharmony_ci| 202 | Non-system applications use system APIs. | 522e41f4b71Sopenharmony_ci| 2200002 | Failed to connect to the service. | 523e41f4b71Sopenharmony_ci| 2201005 | Device information does not exist. | 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci**Example** 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci```ts 528e41f4b71Sopenharmony_ciimport { ethernet } from '@kit.NetworkKit'; 529e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ciethernet.getMacAddress().then((data: Array<ethernet.MacAddressInfo>) => { 532e41f4b71Sopenharmony_ci console.info("getMacAddress promise data = " + JSON.stringify(data)); 533e41f4b71Sopenharmony_ci}).catch((error: BusinessError) => { 534e41f4b71Sopenharmony_ci console.error("getMacAddress promise error = " + JSON.stringify(error)); 535e41f4b71Sopenharmony_ci}); 536e41f4b71Sopenharmony_ci``` 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci## InterfaceConfiguration<sup>9+</sup> 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ciDefines the network configuration for the Ethernet connection. 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ci**System API**: This is a system API. 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 547e41f4b71Sopenharmony_ci| ------------ | ----------------------- | ---|------------------------------------------------------------ | 548e41f4b71Sopenharmony_ci| mode | [IPSetMode](#ipsetmode9) | Yes| Configuration mode of the Ethernet connection.| 549e41f4b71Sopenharmony_ci| ipAddr | string | Yes| Static IP address of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in Dynamic Host Configuration Protocol (DHCP) mode.| 550e41f4b71Sopenharmony_ci| route | string | Yes| Route of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.| 551e41f4b71Sopenharmony_ci| gateway | string | Yes| Gateway of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.| 552e41f4b71Sopenharmony_ci| netMask | string | Yes| Subnet mask of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.| 553e41f4b71Sopenharmony_ci| dnsServers | string | Yes| DNS server addresses of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode. Multiple addresses are separated by commas (,).| 554e41f4b71Sopenharmony_ci| httpProxy<sup>10+</sup> | [HttpProxy](js-apis-net-connection.md#httpproxy10) | No| HTTP proxy of the Ethernet connection. By default, no proxy is configured.| 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci## InterfaceStateInfo<sup>11+</sup> 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ciListens for status changes of an Ethernet NIC. 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci**System API**: This is a system API. 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 565e41f4b71Sopenharmony_ci| ------------ | ----------------------- | --- | ---------------------------------------------------- | 566e41f4b71Sopenharmony_ci| iface | string | Yes| Name of the Ethernet NIC. | 567e41f4b71Sopenharmony_ci| active | boolean | Yes| Whether the Ethernet NIC is activated. The value **true** indicates the NIC is activated, and the value **false** indicates the opposite.| 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_ci## IPSetMode<sup>9+</sup> 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ciDefines the configuration mode of the Ethernet connection. 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci**System API**: This is a system API. 574e41f4b71Sopenharmony_ci 575e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci| Name | Value | Description | 578e41f4b71Sopenharmony_ci| --------------------- | ---- | -------------------------- | 579e41f4b71Sopenharmony_ci| STATIC | 0 | Static network configuration for an Ethernet connection.| 580e41f4b71Sopenharmony_ci| DHCP | 1 | Dynamic network configuration for an Ethernet connection.| 581e41f4b71Sopenharmony_ci| LAN_STATIC<sup>11+</sup>| 2 | Static network configuration for a LAN connection. | 582e41f4b71Sopenharmony_ci| LAN_DHCP<sup>11+</sup> | 3 | Dynamic network configuration for a LAN connection. | 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ci## MacAddressInfo<sup>13+</sup> 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ciDefines the name and MAC address of an Ethernet NIC. 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ci**System API**: This is a system API. 589e41f4b71Sopenharmony_ci 590e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Ethernet 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 593e41f4b71Sopenharmony_ci| ------------ | ----------------------- | --- | ---------------------------------------------------- | 594e41f4b71Sopenharmony_ci| iface | string | Yes| Name of the Ethernet NIC. | 595e41f4b71Sopenharmony_ci| macAddr | string | Yes| MAC address of the Ethernet NIC.| 596