Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Python Script that Recives a few arguments and uses a few text files for manipulation

Posted on 2011-03-02
7
Medium Priority
?
327 Views
Last Modified: 2012-06-27
Hi ,
I had a batch file i tried to wrote but cant seem to get it right , i have a system that eventually can run Python script files and im trying to create a script / snippet which does the following

short explanation
it runs a report , and checks for a text file with status (for loop)  , if its completed - it goes to the 2nd loop

recieves 2 arguments (sys.argv[1], and [2])
Runs an executable file (subprocess.popen)
outputs the stdout to a log file with the name sys.argv[1].log
does a loop (for) on a the output txt file - and looks for the 2nd token

i.e - for /f "tokens=2" %n IN ('type %1__runjob.log') DO (thats in Batch)
 
the output of sys.argv[1].log is as such (its already outputted like that , just piped into a file)
Job 1234 Submitted
       ____
     (2nd token)

:checkjob
for /f "tokens=4" %x IN ('arik.exe show job %n ^| findstr "status"') DO
if %x==COMPLETE (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %a IN ('type %2_Agentlist.log') do arik.exe view report -s -q c:\%2\%datetime%_%a.csv "%1" "%x" network 0
goto done)
 
ELSE  goto checkjob
0
Comment
Question by:m0tek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35016132
Not sure what your actual code looks like, but within a .bat batch file, you need to double your percent signs (%%) for local variables (%a, %n and %x, etc), like this, for example

for /f "skip=6" %%a IN ('type %2_Agentlist.log') do arik.exe view report -s -q c:\%2\%datetime%_%%a.csv "%1" "%%x" network 0
goto done)

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35016766
Are you looking to correct this .bat file, or convert it to Python?
0
 

Author Comment

by:m0tek
ID: 35017851
either is good
i am pasting the current batch (doesnt work)


::expecting %1 to be the Policy name and %2 the domain
arik.exe run job "Everyone_FC_Shares" "testbatch" > %1__runjob.log
for /f "tokens=2" %n IN ('type %1__runjob.log') DO
:checkjob
for /f "tokens=4" %x IN ('arik.exe show job %n ^| findstr "status"') DO
if %x==COMPLETE (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %a IN ('type %2_Agentlist.log') do arik.exe view report -sThftic -q c:\%2\%datetime%_%a.csv "%1" "%x" network 0
goto done)
 
ELSE  goto checkjob
 
:done
echo done
0
Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

 
LVL 16

Expert Comment

by:sjklein42
ID: 35017917
Can be confusing!  Need to double the percent signs for the "for" loop variables, like this:

::expecting %1 to be the Policy name and %2 the domain
arik.exe run job "Everyone_FC_Shares" "testbatch" > %1__runjob.log
for /f "tokens=2" %%n IN ('type %1__runjob.log') DO 
:checkjob
for /f "tokens=4" %%x IN ('arik.exe show job %%n ^| findstr "status"') DO 
if "%%x"=="COMPLETE" (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %%a IN ('type %2_Agentlist.log') do arik.exe view report -sThftic -q c:\%2\%datetime%_%%a.csv "%1" "%%x" network 0
goto done)
 
ELSE  goto checkjob
 
:done
echo done

Open in new window

0
 

Author Comment

by:m0tek
ID: 35025063
still having trouble , can anyone help me convert this to python?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35028389
If no Python takers, I'm quite sure the batch file could be made to work.

Please post an actual input file we can use to test it, along with an example or two of where it fails.

Thanks.
0
 
LVL 15

Accepted Solution

by:
mish33 earned 2000 total points
ID: 35117544

import time, os, sys

def main(args):
   if len(args) != 2:
      print "expecting %1 to be the Policy name and %2 the domain"
      return
   policy, domain = args
   log = policy + '__runjob.log'
   os.system('arik.exe run job "Everyone_FC_Shares" "testbatch" > ' + log)
   job = token(2, log)
   while True:
      os.system('arik.exe show job %s ^| findstr "status" > status' % job)
      status = token(4, 'status')
      if status == 'COMPLETE':
         break
   log = domain + '_Agentlist.log'
   os.system('arik.exe show domain -l %s > %s' % (domain, log))
   a = open(log).readlines()[6]
   os.system(r'arik.exe view report -sThftic -q c:\%s\%s_%s.csv "%s" "%s" network 0'
             % (domain, time.strftime('%Y%m%m%H%M%S'), a, policy, status))

def token(n, file):
   return open(file).read().split()[n-1]

if __name__ == '__main__':
   main(sys.argv[1:])

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question