The sshutil.cmd Module

exception sshutil.cmd.CalledProcessError(code, command, output=None, error=None)

Bases: subprocess.CalledProcessError

class sshutil.cmd.SSHCommand(command, host, port=22, username=None, password=None, debug=False, cache=None, proxycmd=None)

Bases: sshutil.conn.SSHConnection

run()

Run a command, return stdout.

Returns:stdout
Raises:CalledProcessError
>>> cmd = SSHCommand("ls -d /etc", "localhost")
>>> print(cmd.run(), end="")
/etc
>>> cmd = SSHCommand("grep foobar doesnt-exist", "localhost")
>>> cmd.run()                                   
Traceback (most recent call last):
    ...
CalledProcessError: Command 'grep foobar doesnt-exist' returned non-zero exit status 2
run_status()

Run a command, return exitcode and stdout.

Returns:(status, stdout)
>>> status, output = SSHCommand("ls -d /etc", "localhost").run_status()
>>> status
0
>>> print(output, end="")
/etc
>>> status, output = SSHCommand("grep foobar doesnt-exist", "localhost").run_status()
>>> status
2
>>> print(output, end="")
run_status_stderr()

Run the command returning exit code, stdout and stderr.

Returns:(returncode, stdout, stderr)
>>> status, output, error = SSHCommand("ls -d /etc", "localhost").run_status_stderr()
>>> status
0
>>> print(output, end="")
/etc
>>> print(error, end="")
>>> status, output, error = SSHCommand("grep foobar doesnt-exist", "localhost").run_status_stderr()
>>> status
2
>>> print(output, end="")
>>>
>>> print(error, end="")
grep: doesnt-exist: No such file or directory
run_stderr()

Run a command, return stdout and stderr,

Returns:(stdout, stderr)
Raises:CalledProcessError
>>> cmd = SSHCommand("ls -d /etc", "localhost")
>>> output, error = cmd.run_stderr()
>>> print(output, end="")
/etc
>>> print(error, end="")
>>> cmd = SSHCommand("grep foobar doesnt-exist", "localhost")
>>> cmd.run_stderr()                                    
Traceback (most recent call last):
    ...
CalledProcessError: Command 'grep foobar doesnt-exist' returned non-zero exit status 2
class sshutil.cmd.SSHPTYCommand(command, host, port=22, username=None, password=None, debug=False, cache=None, proxycmd=None)

Bases: sshutil.cmd.SSHCommand

Instances of this class also obtain a PTY prior to executing the command

class sshutil.cmd.ShellCommand(command, debug=False)

Bases: object

run()

Run a command over an ssh channel, return stdout. Raise CalledProcessError on failure.

>>> cmd = ShellCommand("ls -d /etc", False)
>>> print(cmd.run(), end="")
/etc
>>> cmd = ShellCommand("grep foobar doesnt-exist", False)
>>> cmd.run()                                   
Traceback (most recent call last):
    ...
CalledProcessError: Command 'grep foobar doesnt-exist' returned non-zero exit status 2
run_status()

Run a command over an ssh channel, return exitcode and stdout.

>>> status, output = ShellCommand("ls -d /etc").run_status()
>>> status
0
>>> print(output, end="")
/etc
>>> status, output = ShellCommand("grep foobar doesnt-exist").run_status()
>>> status
2
>>> print(output, end="")
run_status_stderr()

Run a command over an ssh channel, return exit code, stdout and stderr.

>>> cmd = ShellCommand("ls -d /etc")
>>> status, output, error = cmd.run_status_stderr()
>>> status
0
>>> print(output, end="")
/etc
>>> print(error, end="")
run_stderr()

Run a command over an ssh channel, return stdout and stderr, Raise CalledProcessError on failure

>>> cmd = ShellCommand("ls -d /etc")
>>> output, error = cmd.run_stderr()
>>> print(output, end="")
/etc
>>> print(error, end="")
>>> cmd = ShellCommand("grep foobar doesnt-exist")
>>> cmd.run_stderr()                                    
Traceback (most recent call last):
    ...
CalledProcessError: Command 'grep foobar doesnt-exist' returned non-zero exit status 2
sshutil.cmd.read_to_eof(recvmethod)
sshutil.cmd.setup_module(_)
sshutil.cmd.shell_escape_single_quote(command)

Escape single quotes for use in a shell single quoted string Explanation:

  1. End first quotation which uses single quotes.
  2. Start second quotation, using double-quotes.
  3. Quoted character.
  4. End second quotation, using double-quotes.
  5. Start third quotation, using single quotes.

If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word

sshutil.cmd.terminal_size()