Hardware Device monitoring and processing 2-(enable and disable Nic)

Source: Internet
Author: User

Train of Thought: Get the iftable table and get whether the current status of the device is mib_if_admin_status_up or mib_if_admin_status_down.

Implementation:
1) first check the NIC status checknetstatus ()

2) Enable and disable the NIC based on its current status.

Checknetstatus ()

Including header files and libraries <br/> # include <PM. h> <br/> # include <iphlpapi. h> <br/> # pragma comment (Lib, "iphlpapi. lib ") <br/> bool checknetstatus () <br/> {<br/> bool netstatus = true; // The default status is networked. <br/> pmib_iftable iftable; <br/> DWORD dwsize = 0; <br/> DWORD dwretval = 0; <br/> mib_ifrow ifrow; <br/> // allocate memory for our pointers. <br/> iftable = (mib_iftable *) malloc (sizeof (mib_iftable); <br/> // obtain the test and obtain enough space <br/> If (get Iftable (iftable, & dwsize, 0) = error_insufficient_buffer) {<br/> // release the space requested before the iftable, not processed <br/> iftable = (mib_iftable *) malloc (dwsize); <br/>}< br/> // obtain <br/> If (dwretval = getiftable (iftable, & dwsize, 0 )) = no_error) <br/>{< br/> for (INT I = 0; I <(INT) iftable-> dwnumentries; I ++) <br/> {<br/> ifrow = iftable-> table [I]; <br/> char * PTR = (char *) (ifrow. bdescr); <br/> wtolower (PTR); <br/> If (strst R (PTR, "USB") | strstr (PTR, "loopback") <br/> continue; <br/> dwretval = getifentry (& ifrow ); <br/> If (ifrow. dwadminstatus = mib_if_admin_status_down) <br/> netstatus = false; <br/> else <br/> netstatus = true; <br/>}< br/> else <br/> {<br/> const char * notice11 = "getiftable failed .! "; <Br/> log (notice11, strlen (notice11); <br/>}< br/> free (iftable); <br/> return netstatus; <br/> 

 

Disable Nic

 

Void disablenetwork ()

Void disablenetwork () <br/>{< br/> If (! Checknetstatus () <br/>{< br/> const char * notice = "the network is already disabled and you do not need to disable it again"; <br/> log (notice, strlen (Notice); <br/> return; <br/>}< br/> // todo: add the control notification handler code here <br/> pmib_iftable iftable; <br/> DWORD dwsize = 0; <br/> DWORD dwretval = 0; <br/> mib_ifrow ifrow; <br/> // allocate memory for our pointers. <br/> iftable = (mib_iftable *) malloc (sizeof (mib_iftable); <br/> // obtain the test result, obtain enough space <br/> If (getiftable (iftable, & Dwsize, 0) = error_insufficient_buffer) {<br/> // release the space requested before the iftable, not processed <br/> iftable = (mib_iftable *) malloc (dwsize); <br/>}< br/> // obtain <br/> If (dwretval = getiftable (iftable, & dwsize, 0 )) = no_error) <br/>{< br/> for (INT I = 0; I <(INT) iftable-> dwnumentries; I ++) <br/> {<br/> // reset interface status <br/> ifrow = iftable-> table [I]; <br/> char * PTR = (char *) (ifrow. bdescr); <br/> wtolower (PTR); <br/> If (Strstr (PTR, "USB") | strstr (PTR, "loopback") // devices of different models are different <br/> continue; <br/> char notice00 [max_path] = "Description:"; <br/> zeromemory (¬ ice00 [strlen ("Description:") + 1], MAX_PATH-strlen ("Description: ")-1); <br/> strcat (notice00, PTR); <br/> log (notice00, strlen (notice00 )); // write the log file <br/> ifrow. dwadminstatus = mib_if_admin_status_down; <br/> If (dwretval = setifentry (& ifrow) = no_error) <br/>{< br/>/ /Printf ("setifentry mib_if_admin_status_down. /n "); <br/> char notice00 [max_path] =" Disable success: "; <br/> zeromemory (paiice00 [strlen (" Disable success :") + 1], MAX_PATH-strlen ("Disable success:")-1); <br/> strcat (notice00, PTR); <br/> log (notice00, strlen (notice00 )); // write the log file <br/>}< br/> else <br/>{< br/> const char * notice11 =" getiftable failed .! "; <Br/> log (notice11, strlen (notice11); <br/>}< br/> free (iftable ); <br/> // release the space requested before the iftable, not processed <br/>} 

 

 

// Enable the NIC

 

Void enablenetwork ()

Void enablenetwork () <br/>{< br/> If (checknetstatus () <br/>{< br/> const char * notice = "the network is enabled, no need to enable "; <br/> log (Notice, strlen (Notice); <br/> return; <br/>}< br/> pmib_iftable iftable; <br/> DWORD dwsize = 0; <br/> DWORD dwretval = 0; <br/> mib_ifrow ifrow; <br/> // allocate memory for our pointers. <br/> iftable = (mib_iftable *) malloc (sizeof (mib_iftable); <br/> // obtain the test and obtain enough space <br/> If (getiftable (Iftable, & dwsize, 0) = error_insufficient_buffer) {<br/> // release the space requested before the iftable, not processed <br/> iftable = (mib_iftable *) malloc (dwsize); <br/>}< br/> // obtain <br/> If (dwretval = getiftable (iftable, & dwsize, 0 )) = no_error) <br/>{< br/> for (INT I = 0; I <(INT) iftable-> dwnumentries; I ++) <br/> {<br/> // reset interface status <br/> ifrow = iftable-> table [I]; <br/> char * PTR = (char *) (ifrow. bdescr); <br/> wtolower (PTR); <Br/> If (strstr (PTR, "USB") | strstr (PTR, "loopback") <br/> continue; <br/> char notice00 [max_path] = "Description:"; <br/> zeromemory (¬ ice00 [strlen ("Description:") + 1], MAX_PATH-strlen ("Description: ")-1); <br/> strcat (notice00, PTR); <br/> log (notice00, strlen (notice00 )); // write the log file <br/> ifrow. dwadminstatus = mib_if_admin_status_up; <br/> If (dwretval = setifentry (& ifrow) = no_error) <br/>{< br/>/print F ("setifentry mib_if_admin_status_up. /n "); <br/> char notice00 [max_path] =" enable sucess: "; <br/> zeromemory (paiice00 [strlen (" enable sucess :") + 1], MAX_PATH-strlen ("enable sucess:")-1); <br/> strcat (notice00, PTR); <br/> log (notice00, strlen (notice00 )); // write the log file <br/>}< br/> else <br/>{< br/> const char * notice11 =" getiftable failed .! "; <Br/> log (notice11, strlen (notice11); <br/>}< br/> free (iftable); <br/>}< br/> 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.