Top-rated recipes tagged "sysadmin"http://code.activestate.com/recipes/tags/sysadmin/top/2015-05-15T09:25:08-07:00ActiveState Code RecipesModule to allow Asynchronous subprocess use on Windows and Posix platforms (Python) 2006-12-01T17:30:02-08:00Josiah Carlsonhttp://code.activestate.com/recipes/users/1241800/http://code.activestate.com/recipes/440554-module-to-allow-asynchronous-subprocess-use-on-win/ <p style="color: grey"> Python recipe 440554 by <a href="/recipes/users/1241800/">Josiah Carlson</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 10. </p> <p>The 'subprocess' module in Python 2.4 has made creating and accessing subprocess streams in Python relatively convenient for all supported platforms, but what if you want to interact with the started subprocess? That is, what if you want to send a command, read the response, and send a new command based on that response?</p> <p>Now there is a solution. The included subprocess.Popen subclass adds three new commonly used methods: recv(maxsize=None), recv_err(maxsize=None), and send(input), along with a utility method: send_recv(input='', maxsize=None).</p> <p>recv() and recv_err() both read at most maxsize bytes from the started subprocess. send() sends strings to the started subprocess. send_recv() will send the provided input, and read up to maxsize bytes from both stdout and stderr.</p> <p>If any of the pipes are closed, the attributes for those pipes will be set to None, and the methods will return None.</p> <p>v. 1.3 fixed a few bugs relating to *nix support v. 1.4,5 fixed initialization on all platforms, a few bugs relating to Windows support, added two utility functions, and added an example of how to use this module. v. 1.6 fixed linux _recv() and test initialization thanks to Yuri Takhteyev at Stanford. v. 1.7 removed _setup() and __init__() and fixed subprocess unittests thanks to Antonio Valentino. Added 4th argument 'tr' to recv_some(), which is, approximately, the number of times it will attempt to recieve data. Added 5th argument 'stderr' to recv_some(), where when true, will recieve from stderr. Cleaned up some pipe closing. v. 1.8 Fixed missing self. parameter in non-windows _recv method thanks to comment. v. 1.9 Fixed fcntl calls for closed handles.</p> Terminating a subprocess on Windows (Python) 2004-11-24T12:52:28-08:00Jimmy Retzlaffhttp://code.activestate.com/recipes/users/98735/http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/ <p style="color: grey"> Python recipe 347462 by <a href="/recipes/users/98735/">Jimmy Retzlaff</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>The new subprocess module in Python 2.4 (also available for 2.2 and 2.3 at <a href="http://effbot.org/downloads/#subprocess" rel="nofollow">http://effbot.org/downloads/#subprocess</a>) allows access to the handle of any newly created subprocess. You can use this handle to terminate subprocesses using either ctypes or the pywin32 extensions.</p> Parsing the command line (Python) 2004-04-18T08:15:21-07:00Michele Simionatohttp://code.activestate.com/recipes/users/1122360/http://code.activestate.com/recipes/278844-parsing-the-command-line/ <p style="color: grey"> Python recipe 278844 by <a href="/recipes/users/1122360/">Michele Simionato</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>The module optparse was a great addition to Python 2.3, since it is much more powerful and easier to use than getopt. Using optparse, writing command-line tools is a breeze. However, the power of optparse comes together with a certain verbosity. This recipe allows to use optparse with a minimum of boilerplate, trading flexibility for easy of use. Still, it covers 95% of my common needs, so I think it may be useful to others.</p> getch()-like unbuffered character reading from stdin on both Windows and Unix (Python) 2003-01-07T02:31:32-08:00Danny Yoohttp://code.activestate.com/recipes/users/98032/http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/ <p style="color: grey"> Python recipe 134892 by <a href="/recipes/users/98032/">Danny Yoo</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>A small utility class to read single characters from standard input, on both Windows and UNIX systems. It provides a getch() function-like instance.</p> Pyline: a grep-like, sed-like command-line tool. (Python) 2006-03-30T14:50:24-08:00Graham Fawcetthttp://code.activestate.com/recipes/users/108898/http://code.activestate.com/recipes/437932-pyline-a-grep-like-sed-like-command-line-tool/ <p style="color: grey"> Python recipe 437932 by <a href="/recipes/users/108898/">Graham Fawcett</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 3. </p> <p>This utility was born from the fact that I keep forgetting how to use "sed", and I suck at Perl. It brings ad-hoc command-line piping sensibilities to the Python interpeter. (Version 1.2 does better outputting of list-like results, thanks to Mark Eichin.)</p> Method signature checking decorators (Python) 2008-01-28T07:13:53-08:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/426123-method-signature-checking-decorators/ <p style="color: grey"> Python recipe 426123 by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 3. </p> <p>This recipe adds parameter type checking to each method or function invocation. Not a replacement for static typing, but it makes a nice pair of shackles to me.</p> List information about installed Python packages and modules (Python) 2005-09-02T06:53:56-07:00Anand Pillaihttp://code.activestate.com/recipes/users/2499592/http://code.activestate.com/recipes/440501-list-information-about-installed-python-packages-a/ <p style="color: grey"> Python recipe 440501 by <a href="/recipes/users/2499592/">Anand Pillai</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 3. </p> <p>This recipe provides a quick way to search and retrieve information about installed Python packages and modules in your Python installation. It classifies the retrieved module information into types (built-in, source files, c-extensions etc) and prints module information under each type as a sorted list.</p> Win32com - Batch repathing of Autocad Xrefs (Python) 2005-08-31T10:28:24-07:00Ed Blakehttp://code.activestate.com/recipes/users/2492237/http://code.activestate.com/recipes/440498-win32com-batch-repathing-of-autocad-xrefs/ <p style="color: grey"> Python recipe 440498 by <a href="/recipes/users/2492237/">Ed Blake</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>This script asks for a base directory and then changes all xrefs in all drawings in all subdirectories so that they use relative paths. To use it just copy it somewhere in your target directory structure and run.</p> Generate a human readable 'random' password (nicepass.py) (Python) 2005-04-26T07:13:28-07:00Pradeep Kishore Gowdahttp://code.activestate.com/recipes/users/2230395/http://code.activestate.com/recipes/410076-generate-a-human-readable-random-password-nicepass/ <p style="color: grey"> Python recipe 410076 by <a href="/recipes/users/2230395/">Pradeep Kishore Gowda</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 4. </p> <p>Password will be generated in the form 'word'+digits+'word' eg.,nice137pace instead of a difficult-to-remember - K8Yn9muL</p> Simple pattern-based string generator (Python) 2004-12-03T16:38:13-08:00Guy Argohttp://code.activestate.com/recipes/users/1050552/http://code.activestate.com/recipes/355531-simple-pattern-based-string-generator/ <p style="color: grey"> Python recipe 355531 by <a href="/recipes/users/1050552/">Guy Argo</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Simple script for generating all the string described by a pattern passed on the command-line:</p> Generate Python Symbol Lists for a Programmer's Editor Syntax Coloring File (Python) 2003-05-20T13:07:58-07:00Jim Jinkinshttp://code.activestate.com/recipes/users/1170097/http://code.activestate.com/recipes/200638-generate-python-symbol-lists-for-a-programmers-edi/ <p style="color: grey"> Python recipe 200638 by <a href="/recipes/users/1170097/">Jim Jinkins</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Use reflection to generate lists of Python symbols. Use them to build a syntax coloring file for your preferred programmer's editor.</p> <p>This is easier and less error-prone than updating your syntax file by reading "What's New In Version x" and other documentation.</p> Manipulating Windows Services (Python) 2001-05-30T18:01:14-07:00andy mckayhttp://code.activestate.com/recipes/users/92886/http://code.activestate.com/recipes/59872-manipulating-windows-services/ <p style="color: grey"> Python recipe 59872 by <a href="/recipes/users/92886/">andy mckay</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Its easy to mess with Windows Service using Python.</p> Method signature type checking decorator for Python 3 (Python) 2015-05-15T09:25:08-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/572161-method-signature-type-checking-decorator-for-pytho/ <p style="color: grey"> Python recipe 572161 by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a> (<a href="/recipes/tags/checking/">checking</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/signature/">signature</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/type/">type</a>). Revision 8. </p> <p>This recipe allows nice and clean validation for method parameters/return values. It uses function annotations available in Python 3 for the actual signature specification.</p> Getting system information under Windows (Python) 2007-04-22T11:47:15-07:00Eli Golovinskyhttp://code.activestate.com/recipes/users/2925878/http://code.activestate.com/recipes/511491-getting-system-information-under-windows/ <p style="color: grey"> Python recipe 511491 by <a href="/recipes/users/2925878/">Eli Golovinsky</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>This code uses pywin32 and the _winreg module to get some vital information about the system the script is running on, including OS version, installed browsers and free space on the main HD.</p> <p>Enjoy</p> Cross-platform startfile and mailto functions (Python) 2008-09-20T04:39:16-07:00Antonio Valentinohttp://code.activestate.com/recipes/users/2832200/http://code.activestate.com/recipes/511443-cross-platform-startfile-and-mailto-functions/ <p style="color: grey"> Python recipe 511443 by <a href="/recipes/users/2832200/">Antonio Valentino</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>This recipe provides a couple of cross-platform utility functions, 'open' and 'mailto', for opening files and URLs in the registered default application and for sending e-mails using the user's preferred composer. The python standard library already provides the os.startfie function that allows the user to open a generic file in the associated application but it only works on Windows. The 'webbrowser' module is cross-platform but only handles a specific kind of application, the web browser, and it is useless if one wants to open e.g. a source code python file in the user's preferred text editor. The 'open' function simply opens the file or URL passed as parameter in the system registered application. The 'mailto' function starts the user's preferred e-mail composer and pre-fills the 'to', 'cc','bcc' and 'subject' headers if corresponding parameters are provided. It also handles parameters for the message body and for attachments.</p> Read-Write Lock class, RLock like (Python) 2007-03-06T14:11:59-08:00Heiko Wundramhttp://code.activestate.com/recipes/users/4039652/http://code.activestate.com/recipes/502283-read-write-lock-class-rlock-like/ <p style="color: grey"> Python recipe 502283 by <a href="/recipes/users/4039652/">Heiko Wundram</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>During a recent application server design, I came across the need for a thread locking class which supports read and write locks (in the filesystem lock kind of way).</p> Get the path of the currently executing python script using import. (Python) 2006-02-24T05:23:36-08:00Jitender Cheemahttp://code.activestate.com/recipes/users/2790517/http://code.activestate.com/recipes/474083-get-the-path-of-the-currently-executing-python-scr/ <p style="color: grey"> Python recipe 474083 by <a href="/recipes/users/2790517/">Jitender Cheema</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <h4>1. sys.arg has the script name</h4> <h4>2. Although there can be many ways, e.g. os.cwd() but, there is another</h4> <h4>trick to</h4> <h4>obtain the Complete URI or Location of the current script.</h4> <h4>You can argue, os.getcwd()</h4> <h4>import can give you working directory of the current script</h4> <h4>Third party Java code calling your script.... ;)</h4> <p>#</p> Get system/language/user dependent paths on windows (Python) 2006-02-05T11:54:03-08:00Chris Arndthttp://code.activestate.com/recipes/users/1499228/http://code.activestate.com/recipes/473846-get-systemlanguageuser-dependent-paths-on-windows/ <p style="color: grey"> Python recipe 473846 by <a href="/recipes/users/1499228/">Chris Arndt</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>This modules provides a few handy functions to retrieve the path names of some windows system directories from the registry [1] and the environment. These path names can be different depending on OS version, installation language, current user and personal setup. Because of this, they should not be included statically in your program.</p> win32 process information (Python) 2005-10-24T10:33:50-07:00Chris Lambacherhttp://code.activestate.com/recipes/users/2526202/http://code.activestate.com/recipes/442477-win32-process-information/ <p style="color: grey"> Python recipe 442477 by <a href="/recipes/users/2526202/">Chris Lambacher</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>This function will return a list of tuples of process names and ids. It should provide a starting point for grabbing other process related information out of the pdh.</p> win32com - Accessing AutoCAD entities (Python) 2005-08-30T13:48:52-07:00Ed Blakehttp://code.activestate.com/recipes/users/2492237/http://code.activestate.com/recipes/440493-win32com-accessing-autocad-entities/ <p style="color: grey"> Python recipe 440493 by <a href="/recipes/users/2492237/">Ed Blake</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Autodesk's AutoCAD drafting software has for a number of versions included an increasingly complete COM interface. Using the Python win32com modules we have been able to automate some aspects the software; unfortunately because of the organization of the interface certain methods and properties were inaccessible from Python. In recent versions of the win32 modules a new function has been added though: win32com.client.CastTo(). By casting our objects as the correct type we now have access to much of the object model that used to be unreachable.</p> <p>In this example we search the ModelSpace collection for text objects. Once found we then cast them and alter one of the text specific properties. To test this code open AutoCAD and in a blank file add at least one dtext object with 'Spam' as its value.</p>