C program executing commands in cmd

Asked

Viewed 1,253 times

4

I am wanting to create a C executable that opens the prompt and execute commands through it, the executable is a solution to the problem "ethernet does not have a valid ip configuration". I’ve even been able to compile one, but some commands ask for elevation...can someone help me with that? Or if you have a simpler way to do that would help a lot too.

#include <stdlib.h>

int main ()
{

    system("ipconfig /release");
    system("ipconfig /renew");
    system("ipconfig /flushdns");
    system("ipconfig /registerdns");
    system("nbtstat -RR");
    system("netsh int ip reset all");
    system("netsh winsock reset");
}

2 answers

6

Hello, you need to use shellExecute to do this, it will run a . bat with these commands.

Example: it would be something like this.

ShellExecute(hwnd, "runas", "c:\\net.bat", NULL, "c:\\windows\\system", SW_SHOWNORMAL );

or

ShellExecute(0, "open", "cmd.exe", "/c ipconfig /release /c ipconfig /renew /c ipconfig /flushdns /c ipconfig /registerdns /c nbtstat -RR /cnetsh int ip reset all /cwinsock reset ", 0, SW_HIDE); //para ver a tela do CMD substitua para SW_HIDE por SW_SHOWNORMAL 

Documentation

  • Sorry, I didn’t understand how to apply this command to the code

  • Do a test with the second command, in it you do not have the HWND just copy and paste, if it works from a look at the documentation to mean how it works.

  • I used the second command, compiled but it gives "Process returned 0" so I saw in the documentation this return is a mistake. Also, apparently the cmd commands are not executed correctly, I have already revised all but still failing. Error giving is "unrecognized or incomplete command line."

  • tries to replace the fourth parameter with this /c ipconfig /release ipconfig /renew ipconfig /flushdns ipconfig /registerdns nbtstat -RR netsh int ip reset all winsock reset

  • Thank you! is still failing but this way I could find the syntax that cmd understands, I changed "/c" to "/k" to visualize what was happening and saw that for "ipconfig" when it has more than one command should be written like this: "/k ipconfig [/release | /Renew | /flushdns | /registerdns]". Thus compiled, only gave error in "Renew", says '/Renew' is not recognized as an internal or external command, a program operable or a batch file.

4



inserir a descrição da imagem aqui


  • To execute their commands 1 at a time, only add the command: cmd /c .

Is this the interpreter of commando, and it is he who will "use/interpret ” your commands

O layout do seu código ficaria dessa forma :


#include <stdlib.h>

int main ()
{
    system("cmd /c ipconfig /release");
    system("cmd /c ipconfig /renew");
    system("cmd /c ipconfig /flushdns");
    system("cmd /c ipconfig /registerdns");
    system("cmd /c nbtstat -RR");
    system("cmd /c netsh int ip reset all");
    system("cmd /c netsh winsock reset");
}

Ou de uma forma concatenada em uma só linha, o interpretador vai “entender” que é parar chamar/executar comando por comando, um após o outro obedecendo o operador "&"!

Onde o código ficaria com esse layout "cmd /c Comando & comando & comando &..." :


#include <stdlib.h>

int main ()
{
    system("cmd /c ipconfig /release & ipconfig /renew & ipconfig /flushdns & ipconfig /registerdns & nbtstat -RR & netsh int ip reset all & netsh winsock reset");
}

• Observações sobre os operadores: &, |, && e ||

According to the scenario/behaviour of their commands, you can do use when:

execute & execute & execute

execute | recebe _saída_do_comando_anterior

execute | recebe _saída_do_comando_anterior & execute

executou_sem_erro && então_execute_também

executou_com_erro || execute_também_porque_deu_erro

executou_com_erro || executou_com_erro || executou_com_erro

executou_sem_erro && executou_sem_erro && executou_sem_erro

executou_sem_erro && então_execute || execute_esse_no_primeiro_deu_erro

Browser other questions tagged

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