Launching and Controlling Processes

class nclib.process.Process(program, stderr=True, cwd=None, env=None, **kwargs)[source]

A mechanism for launching a local process and interacting with it programatically. This class is a subclass of the basic Netcat object so you may use any method from that class to interact with the process you’ve launched!

Parameters:
  • program – The program to launch. Can be either a list of strings, in which case those strings will become the program argv, or a single string, in which case the shell will be used to launch the program.

  • stderr – How the program’s stderr stream should behave. True (default) will redirect stderr to the output socket, unifying it with stdout. False will redirect it to /dev/null. None will not touch it, causing it to appear on your terminal.

  • cwd – The working directory to execute the program in

  • env – The environment to execute the program in, as a dictionary

Any additional keyword arguments will be passed to the constructor of Netcat.

WARNING: If you provide a string and not a list as the description for the program to launch, then the pid we know about will be associated with the shell that launches the program, not the program itself.

Example: Launch the cat process and send it a greeting. Print out its response. Close the socket and the process exits with status 0.

>>> from nclib import Process
>>> cat = Process('cat')
>>> cat.send('Hello world!')
>>> print(cat.recv())
b'Hello world!'
>>> cat.close()
>>> print(cat.poll())
0
__init__(program, stderr=True, cwd=None, env=None, **kwargs)[source]
kill()[source]

Terminate the process.

static launch(program, sock=None, stderr=True, cwd=None, env=None)[source]

A static method for launching a process. Same rules from the Process constructor apply.

Parameters:

sock – The socket to use for stdin/stdout. If None, will be a new set of pipes.

poll()[source]

Return the exit code of the proces, or None if it has not exited.

send_signal(sig)[source]

Send the signal sig to the process.

wait()[source]

Wait for the process to exit and return its exit code.

class nclib.process.GDBProcess(program, gdbscript=None, **kwargs)[source]

Like nclib.Process, but also launches gdb (in a new $TERMINAL, by default gnome-terminal) to debug the process.

__init__(program, gdbscript=None, **kwargs)[source]
Parameters:
  • program – The program to launch. Can be either a list of strings, in which case those strings will become the program argv, or a single string, in which case the shell will be used to launch the program.

  • stderr – How the program’s stderr stream should behave. True (default) will redirect stderr to the output socket, unifying it with stdout. False will redirect it to /dev/null. None will not touch it, causing it to appear on your terminal.

  • cwd – The working directory to execute the program in

  • env – The environment to execute the program in, as a dictionary

  • protocol – The socket protocol to use. ‘tcp’ by default, can also be ‘udp’

  • gdbscript – The filename of a script for gdb to execute automatically on startup

Any additional keyword arguments will be passed to the constructor of Netcat.