Handle cmd files using python

Asked

Viewed 2,034 times

2

Hello!

I need to create an application in python to decrease the labor where I work, we use files . bat to execute several commands in cmd, but some of these commands manipulate IP’s and as it comes to a very large network we spend a lot of time changing the IP’s manually inside a text file and creating another .bat. There is some way, to manipulate these cmd commands using Python or another language?

  • In python there is lib subprocess with her you for performing operations in cmd. Follows link of her documentation

3 answers

5


Yes, in Python it would be easy to create a program to manage other scripts . bat, and create them and describe them according to IP.

But it would be even easier, and more maintainable, to exchange at once the various sets of ,bat that you have there for a single Python script that manages everything --

.bat is a found way of executing commands from the old DOS in batch - not even a shell lingugem, and you have to do some contortions to such trivial things in "real languages" as an if or a for.

To manage external scripts, you can use the frameworks library - jinja2 - it is the default template language of Flask,and perhaps other web frameworks. But the idea of a template language is to just read a "base" file, and replace well-defined language markups with variables controlled from Python. In this case, instead of filling in the data from an HTML page to serve on the Web, you would use Jinja to fill in the Ips in a . bat,and burn to disk the resulting file.

The Jinja documentation is here: https://jinja.palletsprojects.com/en/2.10.x/

Now, in the line that the other answers point to, it may be much more practical to use Python at once instead of . bat

With the command line application framework click it is easy to automatically create cmd commands that trigger actions in functions of a Python program, already with all parameters and arguments preset, and a help system printed on the automatically generated terminal.

And there is a whole family of forms to, from within the program in Python, call external commands like a file . bat, case these commands do things that take a little more time to do in Pythn (network-specific configuration commands, for example) - but if they are commands like copying files, manipulating text data, etc.. it’s simpler to do everything in Python anyway.

The documentation of the click: https://click.palletsprojects.com/en/7.x/

And finally, to call other commands, can be used from os.system - and you can shorten the call so you don’t have to write too much:

from os import system as S

S("ipconfig")

When, if it really is a critical project, use the module suproccess and its various functions to have control of the data generated by each external process, output code, to be able to call external commands in parallel, etc.

Documentation of the subprocess: https://docs.python.org/3/library/subprocess.html

  • Thank you very much! I’ll take a look.

2

Welcome to Stackoverflow ( although I am beginner here also kkk ). For language Python, there are two modules that can help you, called os and subprocess.

import os
command = "<insira o comando>"
if os.system(command) == 0: #Esta função retorna 0 caso deu tudo certo e 1 se deu erro
    print("Executado com sucesso.")
else:
    print("Erro ao executar o comando.")

I recommend you use the module os because it is much simpler to work, but if you want something more advanced or want to get an output, you can use the subprocess.

import subprocess
command = "<insira o comando>"
print(subprocess.getoutput(command))

Down here I’m going to leave a pretty cool example:

import subprocess

while True:
    server = "www.google.com"
    output = subprocess.getoutput("ping "+server)
    for line in output.split("\n")[2:6]:
        print(line)
  • Haha thanks! I will test here, let’s see what will come out. Thank you

0

Welcome to Stackoverflow, you can write shell commands using python in a very simple way, as you have not specified exactly what you need I will give you a generic way to do this.

import os

os.system('dir')
os.system('ipconfig')
os.system('mkdir teste')

This way the commands will be executed in exactly the same way as they would be executed in the shell. Just replace the string with the command you need to execute.

Browser other questions tagged

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