1 | Python interface to remctl |
---|
2 | |
---|
3 | OVERVIEW |
---|
4 | |
---|
5 | The Python interface to remctl provides Python bindings to the libremctl |
---|
6 | client library plus a high-level interface that translates the libremctl |
---|
7 | API into something closer to idiomatic Python. |
---|
8 | |
---|
9 | This module provides three interfaces: a low-level interface that |
---|
10 | provides a minimal translation from the libremctl API; a simple |
---|
11 | interface in Python that performs a single call to a remctl server and |
---|
12 | returns the result as an object; and a full interface in Python which |
---|
13 | provides more control over the connection, returns individual output |
---|
14 | tokens, and allows multiple commands to be sent via the same connection. |
---|
15 | |
---|
16 | REQUIREMENTS |
---|
17 | |
---|
18 | The module has been tested with Python 2.5 and may not work with older |
---|
19 | versions of Python, although the only known incompatibility are |
---|
20 | parameters in setup.py not supported prior to Python 2.3. |
---|
21 | |
---|
22 | SIMPLIFIED INTERFACE |
---|
23 | |
---|
24 | remctl.remctl(host, port, principal, command) |
---|
25 | Runs a command on the remote system and returns an object containing |
---|
26 | the results. |
---|
27 | |
---|
28 | Arguments: |
---|
29 | * host (required): string, the host to connect to |
---|
30 | * port (optional): unsigned short, the port to connect to |
---|
31 | * principal (optional): string, authentication identity of host |
---|
32 | * command (required): sequence or iterator yielding the command |
---|
33 | |
---|
34 | If port is not given, the library default is used (try 4373 first |
---|
35 | and fall back to attempting the legacy 4444 port). If principal is |
---|
36 | not given, the library default (host/<host> with the realm |
---|
37 | determined by the domain-realm mapping) is used. To use the |
---|
38 | defaults, only the host and command may be specified using named |
---|
39 | arguments, or None can be passed as the port and principal |
---|
40 | arguments. |
---|
41 | |
---|
42 | command can be any sequence or iterator that returns a series of |
---|
43 | strings or things that can be converted to strings making up the |
---|
44 | command. |
---|
45 | |
---|
46 | Returns an object of type RemctlSimpleResult with the following |
---|
47 | attributes: |
---|
48 | * stdout: string, the standard output from the command |
---|
49 | * stderr: string, the standard error from the command |
---|
50 | * status: integer, the exit status of the command |
---|
51 | |
---|
52 | Exceptions: |
---|
53 | * ValueError, TypeError: an invalid argument was supplied |
---|
54 | * RemctlProtocolError: an error occurred in the remctl communication |
---|
55 | |
---|
56 | The value attribute (or string value) of the RemctlProtocolError |
---|
57 | exception contains the string returned by the remctl library. This |
---|
58 | may be an internal error (such as a network error) or an error |
---|
59 | returned by the remote server (such as an unknown command error). |
---|
60 | |
---|
61 | Here is an example using the simplified interface: |
---|
62 | |
---|
63 | use remctl, sys |
---|
64 | command = ('test', 'test') |
---|
65 | try: |
---|
66 | result = remctl(host = 'foo.example.com', command = command) |
---|
67 | except RemctlProtocolError, error: |
---|
68 | print "Error:", str(error) |
---|
69 | sys.exit(1) |
---|
70 | if result.stdout: |
---|
71 | print "stdout:", result.stdout |
---|
72 | if result.stderr: |
---|
73 | print "stderr:", result.stderr |
---|
74 | print "exit status:", result.status |
---|
75 | |
---|
76 | FULL INTERFACE |
---|
77 | |
---|
78 | The full remctl interface requires the user to do more bookkeeping, but |
---|
79 | provides more flexibility and visibility into what is happening at a |
---|
80 | protocol level. It allows issuing multiple commands on the same |
---|
81 | persistant connection (provided that the remote server supports protocol |
---|
82 | version two; if it doesn't, the library will transparently fall back to |
---|
83 | opening a connection for each command). |
---|
84 | |
---|
85 | To use the full interface, first create a connection object with |
---|
86 | remctl.Remctl() (either passing it the connection arguments or then |
---|
87 | calling its open() method), and then call the command() method to issue |
---|
88 | a command. Read output tokens with output() until a status token has |
---|
89 | been received. Then the command is complete and another command can be |
---|
90 | issued. |
---|
91 | |
---|
92 | remctl.Remctl(host, port, principal) |
---|
93 | The constructor. Create a new Remctl object. The arguments are |
---|
94 | optional; if given, the constructor immediately calls open() and |
---|
95 | passes those arguments to it. See below for their meaning and |
---|
96 | possible exceptions. |
---|
97 | |
---|
98 | All further methods below must be called on a Remctl object as returned |
---|
99 | by the constructor. |
---|
100 | |
---|
101 | Remctl.open(host, port, principal) |
---|
102 | Open a connection to a remote server and authenticate. There is no |
---|
103 | return value; an exception is thrown on any error. |
---|
104 | |
---|
105 | Arguments: |
---|
106 | * host (required): string, the host to connect to |
---|
107 | * port (optional): unsigned short, the port to connect to |
---|
108 | * principal (optional): string, authentication identity of host |
---|
109 | |
---|
110 | If port is not given, the library default is used (try 4373 first |
---|
111 | and fall back to attempting the legacy 4444 port). If principal is |
---|
112 | not given, the library default (host/<host> with the realm |
---|
113 | determined by the domain-realm mapping) is used. |
---|
114 | |
---|
115 | Exceptions: |
---|
116 | * ValueError, TypeError: an invalid argument was supplied |
---|
117 | * RemctlError: a network or authentication error occurred |
---|
118 | |
---|
119 | The value attribute (or string value) of the RemctlError exception |
---|
120 | contains the string returned by the remctl library, the same as |
---|
121 | would be returned by the error() method. |
---|
122 | |
---|
123 | Remctl.command(command) |
---|
124 | Send a command to the remote host. There is no return value; an |
---|
125 | exception is thrown on any error. |
---|
126 | |
---|
127 | The Remctl object must already be connected. The command may, under |
---|
128 | the remctl protocol, contain any character, but be aware that most |
---|
129 | remctl servers will reject commands or arguments containing ASCII 0 |
---|
130 | (NUL). This currently therefore cannot be used for upload of |
---|
131 | arbitrary unencoded binary data. |
---|
132 | |
---|
133 | Arguments: |
---|
134 | * command (required): sequence or iterator yielding the command |
---|
135 | |
---|
136 | command can be any sequence or iterator that returns a series of |
---|
137 | strings or things that can be converted to strings making up the |
---|
138 | command. |
---|
139 | |
---|
140 | Exceptions: |
---|
141 | * ValueError, TypeError: an invalid argument was supplied |
---|
142 | * RemctlError: a network or authentication error occurred |
---|
143 | * RemctlNotOpenedError: no connection currently open |
---|
144 | |
---|
145 | The value attribute (or string value) of the RemctlError exception |
---|
146 | contains the string returned by the remctl library, the same as |
---|
147 | would be returned by the error() method. |
---|
148 | |
---|
149 | Remctl.output() |
---|
150 | Reads an output token from the server and returns it as a tuple. A |
---|
151 | command will result in either one error token or zero or more output |
---|
152 | tokens followed by a status token. The output is complete as soon |
---|
153 | as any token other than an output token has been received, but the |
---|
154 | module will keep returning done tokens to the caller for as long as |
---|
155 | output() is called without another call to command(). |
---|
156 | |
---|
157 | The members of the returned tuple are: |
---|
158 | * type: string, "output", "status", "error", or "done" |
---|
159 | * output: string, the returned output or error |
---|
160 | * stream: integer, 1 for stdout or 2 for stderr for an output token |
---|
161 | * status: integer, exit status of command for a status token |
---|
162 | * error: integer, remctl protocol error for an error token |
---|
163 | |
---|
164 | type will always be present. The other members of the tuple may be |
---|
165 | None depending on the type of token. Output tokens will have |
---|
166 | output and stream information, error tokens will have output and |
---|
167 | error information, and status tokens will have status information. |
---|
168 | done tokens will return None for all other elements. |
---|
169 | |
---|
170 | For error tokens, error holds the numeric error code (see the remctl |
---|
171 | protocol specification), which is the recommended value for programs |
---|
172 | to check when looking for specific errors. output will contain an |
---|
173 | English text translation of the error code and the exact text may |
---|
174 | change. |
---|
175 | |
---|
176 | Exceptions: |
---|
177 | * RemctlNotOpenedError: no connection currently open |
---|
178 | |
---|
179 | Remctl.close() |
---|
180 | Close a connection. After calling this method, open() must be |
---|
181 | called for this object before sending any further commands. The |
---|
182 | connection will also be automatically closed when the object is |
---|
183 | destroyed, so calling this method is often not necessary. |
---|
184 | |
---|
185 | Remctl.error() |
---|
186 | Returns the error from the last failed Remctl method. This will be |
---|
187 | the same string as was returned as the string value of a RemctlError |
---|
188 | or RemctlProtocolError exception. |
---|
189 | |
---|
190 | LOW-LEVEL INTERFACE |
---|
191 | |
---|
192 | This module also provides a _remctl module, which exports a low-level |
---|
193 | interface with essentially the same API as the C API. It is very |
---|
194 | similar to the simplified and full interface above except that the |
---|
195 | simplified call returns its results as a tuple, status codes are |
---|
196 | returned from functions instead of throwing exceptions (exceptions are |
---|
197 | only thrown for things like out-of-memory errors), argument checking |
---|
198 | won't be as nice and in some cases relies on the underlying libremctl C |
---|
199 | library to do the error checking, and command arguments have to be lists |
---|
200 | and not arbitrary sequences or iterators. |
---|
201 | |
---|
202 | The _remctl interface is not currently documented or intended for direct |
---|
203 | use. Use at your own risk. |
---|
204 | |
---|
205 | HISTORY |
---|
206 | |
---|
207 | The original implementation was written by Thomas L. Kula |
---|
208 | <kula@tproa.net> and was known as pyremctl. As of remctl 2.13 it is |
---|
209 | part of the stock remctl distribution and ongoing maintenance is done by |
---|
210 | Russ Allbery and Thomas L. Kula. |
---|
211 | |
---|
212 | THANKS |
---|
213 | |
---|
214 | Andrew Mortensen for general code formatting comments and a reminder to |
---|
215 | free malloc'd memory. |
---|