Connect and disconnect Internet?

Asked

Viewed 612 times

4

I would make two . exe basic, one would be to disconnect the Internet and open one program and the other would be to just connect to the Internet.

The part of opening the program I know, now disconnect and connect the Internet in c++ not yet learned. I know I could do with . bat, but actually I know how to do in . bat, but I wanted to know in c++ too.

Em . bat the disconnect and open would look like this:

@echo off
netsh interface set interface "" DISABLED
cd diretorio
programa.exe

.bat to simply connect:

@echo off
netsh interface set interface "" ENABLED
  • So you’re disconnecting the network intrerface, not "the internet". If the same interface is used for local network, the local network is also unavailable.

  • I know, in my case there is no problem the local network be disabled ok ?

  • But if you want to give two examples, one disconnecting only the Internet and the other disconnecting the Internet and the local network would be nice.

  • 2

    @Lucasvirgili is good to pay attention before editing, because you changed the tags and compromised the question. In the meantime an excellent answer came out, only based on the wrong language. Some time from now I remove the comment.

  • An uncomplicated way is to use the function system or CreateProcess simply calling the programs you use in bat, netsh, etc.

1 answer

7

Note: The question was edited and they tagged for . But the purpose of the question is an answer in . At the time of this reply the tag was in , and a solution via . I’ll leave you the answer as it may help future visitors.


Reply to Batch

The simplest way in Windows to turn off only the Internet, is to proceed with the removal of gateway pattern.

Disconnect Internet

Following code to collect information from gateway and store it in a text file, then delete it:

@echo off
cd /d "%~dp0"

REM recolher a gateway atual
set dest=0.0.0.0
for /f "tokens=2,3" %%A in ('"route print %dest% | findstr /c:"%dest%" "') do (

REM guardar o IP e eliminar a gateway
echo %%A %%B>%dest%.txt
route delete %dest% >nul
)
exit /b

Internet

Since we kept the information from gateway in a text file, we can connect the Internet to gateway with the definitions of the same at the moment we remove it:

@echo off
cd /d "%~dp0"

REM garantir que o ficheiro com as definições existe
set dest=0.0.0.0
if not exist %dest%.txt exit /b 2

REM restaurar a gateway
for /f "tokens=1,2" %%A in (%dest%.txt) do (route add %dest% mask %%A %%B >nul)
exit /b

Solution credits for the user @and31415 in this reply in their.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.