Hello! Help. Help is needed: The purpose of the program is to find mac addresses via cmd.exe using the arp -a command, and then copying the result (completely) to a file. I assume that the launch of cmd.exe and the subsequent input of the arp -a command can be implemented through the os library and the argument (I assume that arp -a is an argument when running cmd.exe). The problem is in my ignorance of how I can run cmd with an argument (arp -a), and get output to a file.

    3 answers 3

    Try

    import os os.startfile(r'c:\Windows\system32\cmd.exe', 'any-arg') #Cmd.exe start-up 

    In any case, you can always resort to subprocces and do it as follows:

     import subprocess subprocess.Popen([r'C:\Windows\system32\cmd.exe', '--some-arg']) 

    Well, there is also an option to run the file using the os library, but not using the startfile:

     os.system(r'C:\Windows\system32\cmd.exe -myarg') 
    • I need the arp -a argument. In 1 case, the error is: Traceback (most recent call last): File "C: \ Users \ Artel \ Documents \ mac_finder.py", line 23, in <os.startfile <module> (r'C: \ Windows \ System32 \ cmd.exe ',' arp -a ') # Cmd.exe start-up OSError: [WinError 1155] No application has been associated with the specified file for performing this operation:' C: \\ Windows \\ System32 \\ cmd.exe ' - Antony Voron
    • Try os.system(r'C:\Windows\system32\cmd.exe arp -a') - kitscribe
    • It does not roll ... I do not understand why, but only cmd.exe is launched. I need cmd.exe to start with the command arp -a, and I understand that this is an argument? - Antony Voron
    • @AntonyVoron ok, and how does dir work with argument? Displays the current directory? - kitscribe
    • one
      2all to start, say, dir via cmd , then the command line should look like this: cmd /C dir . Key /C means "execute the command and close." Just cmd dir or cmd arp do not work, only with the /C key. - insolor

    The question is closed, the final program code for searching mac addresses and saving to the bin file:

     import subprocess with open('sh$18mc.bin', 'wb', 0) as file: subprocess.run(r'c:\windows\system32\cmd.exe /C arp -a', stdout=file, check=True) 

    Thanks to @KitScribe for the command opening code in cmd.exe, and @jfs for the output code to the file. Thanks to all participants of the question.

      If the command is not internal (like ASSOC ) , then there is usually no need to run cmd.exe to get the output of the external program in Python.

      Instead, run the program directly. For example, to redirect the standard output of the arp -a command to a txt file:

       #!/usr/bin/env python3 import subprocess with open('результат.txt', 'wb', 0) as file: subprocess.run('arp -a', stdout=file, check=True) 

      On Windows, the command and its arguments are passed as a string (native interface). Portable code should use a list: each argument as a separate element should be passed: ['arp', '-a'] . If you have problems with the encoding, then see Python Interacting with cmd.exe .

      Please note that cmd.exe not used to launch arp.exe .

      If you want to run a command that requires cmd.exe , for example dir , then shell=True enough to pass:

       #!/usr/bin/env python3 import subprocess subprocess.run('dir /A', check=True, shell=True) 
      • The arp -a command (argument) is required (to view the MAC), but even with assoc in txt the file displays only standard information from cmd: Microsoft Windows [Version 6.1.7601] (c) Љ®ЇЇа ж жп © Єа®б ®dv (Microsoft Corp.), 2009. Ґ б Їа ў § ЁҐ. C: \ Users \ Documents> - Antony Voron
      • @AntonyVoron what code did you run? What did you expect to receive? What happens instead? Give the code and the output of the command literally (with formatting). Describe in detail how it differs from the desired. Edit your question and add this information to it (or create a new one if this is a new problem). - jfs
      • Updated the question. Has it become clearer, and what more should I add? - Antony Voron
      • Thanks for participating, add the final code to the header. - Antony Voron
      • @AntonyVoron did not become clearer. What error did the code cause? I updated the answer to explicitly show how arp to call. Do not put a solution (answer) in your question. Assigning questions to Stack Overflow so that they can help other people with the same problem (therefore, the question should contain a clear description of the problem, and the answers should contain a solution (preferably) in a form that can be useful not only to the author of the question. The author does not declare the questions closed on Stack Overflow (you can mark the answer, which personally helped you solve the problem as accepted, but this does not prevent the addition of new answers.) - jfs