I have a generic script in another language which I use to query an ASA5500 firewall. Because of thousands of possibilities in even the 'show' commands, I write small .cmd files which are macro-executed from the main script. A .cmd file looks something like

showline("terminal pager 50000",0)
showline("show running-config",1)

Each of the functions, i.e showline() are in the generic script; it simply opens the .cmd file and processes each line (processed as Tab-separated commands).

;process individual command lines
For i=1 To n
command = StrTrim(ItemExtract(i,cCmd,@TAB))
If StrLen(command)>3
Execute %command%

The essential part is Execute %command%, which becomes the same as [for example] showline("terminal pager 50000",0)

So, assuming I can replicate the functions like showline() in Python, is there a way I can execute from the .cmd file? Hope this makes sense.

I have feeling that you are better of reverse engeneering the code from scratch from description of what the code does. That is higher level and you produce more idiomatic and clear Python code. You could find that you can accomplish everything with minimal clue code by using 0ython standard library and maybe any third party module or code snippet. Is the original code well documented? ideally there would be pseudo code of what code does.

What you just wrote makes absolutely no sense. It might have looked like a complicated post but boiled down to a simple yes or no - does Python support macro commands as many other scripting languages do. It has nothing to do with reverse engineering, or documented source code.

Python supports evaluation, compiling code to byte code, inspection, meta programming etc. I thought you wanted to recode legacy stuff in Python.

Python supports evaluation...

So, perhaps an eval() method can execute a line from a text file as if it were part of the actual script? For example [this is pseudo code] suppose you wanted to construct a script that echoed words from a text file talk.txt that looked like

How Are you?
I am fine

and you have constructed a function talk(txt)

file = fileopen("talk.txt","r")
While ! file.eof
talk( fileread(file) )

Again - that is pseudo code, but hopefully you get the idea.

Python code assuming talk defined (untested from mobile)

map(talk, open("talk.txt"))

Practically you would do of course


to print a file.

The intent is not to print the text file but to Execute each line in the text file as if it were a command, again assuming everything that was to be executed (functions, methods etc..) was available in the calling script.

What is supposed to happen if you execute 'How are you?'

You said yourself:

script that echoed words from a text file talk.txt

def talk(*args):
    for arg in args: 
        print arg,

map(talk, open("talk.txt"))
How Are you?
I am fine
How Are you?
I am fine

How Are you?
I am fine

Thank you - much clearer. Sorry if I confused you. I was thinking there would be some sort of eval() or exec() for each line in the text file. If you could suggest one more refinement. Now assume talk.txt is

talk("How Are You")
bark("I am fine")
bark("...and how are you")
snarl("...sorry I asked")

and you wanted to execute, line-by-line just once - only difference is where talk() echoes back the parameter, bark() echoes it bold-face, snarl() in italics. I think in that case there has to be some sort of fopen()/fread() syntax.

That case is simpler then as the file is program to run so you can only write

import talk

if it is possible to name files with py extension and keep in same directory as the code or any place in path or pythonpath. In Python2 it is possible to do exec_file also but as it is discontinued in Python3, better to do it expicitly


To be super correct with file closing we can include with statement for contextmanager

with open("talk.txt") as f: exec(f.read())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.