The problem is that those servers are running Windows, so there is only one way that I know of to automate the file copying, by using psexec from the pstools suite to execute a script on the list of servers. A windows batch-script would probably have worked, however, as there was quite a few steps involved, and I'm not really confident in writing batch-scripts, I decided to try to use a python script for the file copying instead.
The problem with psexec is that it can only copy one file at a time to be executed remotely, and as python was not in the %PATH of the servers I had to find a way to make psexec copy the script, and run it, and then run the python script. One way would of course be to have the python script as a separate file, but that would mean I had to copy it separately from the psexec invocation.
Instead I decided to try to embed the python program inside the batch script, thereby creating a hybrid batch/python program, a file which is valid both for the batch script and for the python interpreter. A quick search revealed this post, which contained an embryo for the hybrid. Adapting it worked quite nicely while running locally.
rem = """-*-Python-*- script
@echo off
rem -------------------- DOS section --------------------
c:\python25\python %0 %*
goto exit
"""
# -------------------- Python section --------------------
print "hi from python"
DosExitLabel = """
:exit
rem """
I saved this as test.bat and tried to invoke it with the following command:
psexec \\computer -u <username> -s -c -f test.bat
Psexec runs the script fine, but python fails to find test.bat. After some debugging, it turned out that the test.bat file the psexec copies to the server ends up in c:\windows\system32 directory. However, I really could not get python to execute the script, and I have no idea why.
After some time banging my head against the wall, and littering the script with various attempts at invoking python in strange ways, was to pipe the script to the python-interpreter. This works like a charm. Full solution:
rem = """-*-Python-*- script
@echo off
rem -------------------- DOS section --------------------
type %~f0 | c:\python25\python - %*
goto exit
"""
# -------------------- Python section --------------------
print "hi from python"
DosExitLabel = """
:exit
rem """
No comments:
Post a Comment