Netcat#

class pync.Netcat(dest='', port=None, l=False, u=False, p=None, stdin=None, stdout=None, stderr=None, **kwargs)[source]#

Factory class that returns the correct Netcat object based on the arguments given.

Parameters:
  • dest (str, optional) – The IP address or hostname to connect or bind to depending on the “l” parameter.

  • port (int, list(int)) – The port number to connect or bind to depending on the “l” parameter.

  • l (bool, optional) – Set to True to create a server and listen for incoming connections.

  • u (bool, optional) – Set to True to use UDP for transport instead of the default TCP.

  • p (int, optional) – The source port number to bind to.

  • kwargs – All other keyword arguments get passed to the underlying Netcat class.

You can use this class as a context manager using the “with” statement:

with Netcat(...) as nc:
    nc.readwrite()

If you use it without the “with” statement, please make sure to use the close method after use:

nc = Netcat(...)
nc.readwrite()
nc.close()
Examples:

Use the “l” option to create a pync.NetcatTCPServer object.#
from pync import Netcat
with Netcat(dest='localhost', port=8000, l=True) as nc:
    nc.readwrite()
By default, without the “l” option, Netcat will return a pync.NetcatTCPClient object.#
from pync import Netcat
with Netcat(dest='localhost', port=8000) as nc:
    nc.readwrite()
Create a pync.NetcatUDPServer with the “u” and “l” options.#
from pync import Netcat
with Netcat(dest='localhost', port=8000, l=True, u=True) as nc:
    nc.readwrite()
And a pync.NetcatUDPClient using only the “u” option.#
from pync import Netcat
with Netcat(dest='localhost', port=8000, u=True) as nc:
    nc.readwrite()
Any other keyword arguments get passed to the underlying Netcat class.#
from pync import Netcat
# Use the "k" option to keep the server open between connections.
with Netcat(dest='localhost', port=8000, l=True, k=True) as nc:
    nc.readwrite()
Pass a list of ports to connect to one after the other.#
# Simple port scan example.
from pync import Netcat
# Use the "z" option to turn Zero i_o on (connect then close).
# Use the "v" option to turn verbose output on to see connection success or failure.
ports = [8000, 8003, 8002]
with Netcat(dest='localhost', port=ports, z=True, v=True) as nc:
    nc.readwrite()
ArgumentParser#

alias of NetcatArgumentParser

TCPClient#

alias of NetcatTCPClient

TCPServer#

alias of NetcatTCPServer

UDPClient#

alias of NetcatUDPClient

UDPServer#

alias of NetcatUDPServer

classmethod from_args(args, stdin=None, stdout=None, stderr=None)[source]#

Create a Netcat object from command-line arguments instead of keyword arguments.

Parameters:
  • args (str) – A string containing the command-line arguments to create the Netcat instance with.

  • stdin (file, optional) – A file-like object to read outgoing network data from.

  • stdout (file, optional) – A file-like object to write incoming network data to.

  • stderr (file, optional) – A file-like object to write verbose/debug/error messages to.

Example:

from pync import Netcat
with Netcat.from_args('-l localhost 8000') as nc:
    nc.readwrite()