Solved

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

Posted on 2011-03-02
7
319 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
7 Comments
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
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
Comment Utility
Are you looking to correct this .bat file, or convert it to Python?
0
 

Author Comment

by:m0tek
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
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
Comment Utility
still having trouble , can anyone help me convert this to python?
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
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
Comment Utility

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now