Solved

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

Posted on 2011-03-02
7
325 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

696 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