nrvr.remote.ssh
index
/usr/lib/python2.7/site-packages/nrvr/remote/ssh.py

nrvr.remote.ssh - Remote commands over ssh
 
Classes provided by this module include
SshCommandException
SshParameters
SshCommand
 
The main class provided by this module is SshCommand.
 
On the downside, for now it
* reports back indistinguishably the same way stdout and stderr,
* doesn't report back the command's returncode.
 
Works only if module pty is available (e.g. in Python 2.6 on Linux, but not on Windows).
 
As implemented works in Linux.
As implemented requires ssh command.
Nevertheless essential.  To be improved as needed.
 
Idea and first implementation - Leo Baschy <srguiwiz12 AT nrvr DOT com>
 
Public repository - https://github.com/srguiwiz/nrvr-commander
 
Copyright (c) Nirvana Research 2006-2015.
Simplified BSD License

 
Modules
       
os
pty
re
signal
sys
time

 
Classes
       
__builtin__.object
ScpCommand
SshCommand
SshParameters
exceptions.Exception(exceptions.BaseException)
SshCommandException
ScpCommandException

 
class ScpCommand(__builtin__.object)
    Copy a file or files via scp.
 
  Methods defined here:
__init__(self, fromPath, toPath, fromSshParameters=None, toSshParameters=None, recurseDirectories=False, preserveTimes=True)
Create new ScpCommand instance.
 
Will wait until completed.
 
Captures returncode, and output.
 
Either fromPath or toPath is expected to be local, i.e. without user and without IP address.
Correspondingly either fromSshParameters or toSshParameters must NOT be assigned an SshParameters
instance and remain default None.
 
fromPath
    one path or a list of paths.
    
    Absolute paths strongly recommended.
 
toPath
    one path.
    
    Absolute path strongly recommended.
    
    Must be directory if more than one fromPath.
 
fromSshParameters
    an SshParameters instance.
 
toSshParameters
    an SshParameters instance.
 
recurseDirectories
    a hint for when fromSshParameters.

Class methods defined here:
commandsUsedInImplementation(cls) from __builtin__.type
Return a list to be passed to SystemRequirements.commandsRequired().
 
This class can be passed to SystemRequirements.commandsRequiredByImplementations().
get(cls, fromSshParameters, fromRemotePath, toLocalPath, recurseDirectories=False, preserveTimes=True) from __builtin__.type
Return an ScpCommand instance.
 
Will wait until completed.
 
fromSshParameters
    an SshParameters instance for remote.
 
fromRemotePath
    one path or a list of paths.
    
    Absolute paths strongly recommended.
put(cls, fromLocalPath, toSshParameters, toRemotePath, preserveTimes=True) from __builtin__.type
Return an ScpCommand instance.
 
Will wait until completed.
 
fromLocalPath
    one path or a list of paths.
    
    Absolute paths strongly recommended.
 
toSshParameters
    an SshParameters instance for remote.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
output
Collected output string of scp command.
 
May contain extraneous leading or trailing newlines and whitespace.
returncode
Returncode of command or 255 if an scp error occurred.
 
Could be None.

 
class ScpCommandException(SshCommandException)
    
Method resolution order:
ScpCommandException
SshCommandException
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods defined here:
__init__(self, message)

Methods inherited from SshCommandException:
__str__(self)

Data descriptors inherited from SshCommandException:
__weakref__
list of weak references to the object (if defined)
message

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args

 
class SshCommand(__builtin__.object)
    Send a command over ssh.
 
  Methods defined here:
__init__(self, sshParameters, argv, exceptionIfNotZero=True, connectTimeoutSeconds=None, maxConnectionRetries=10, tickerForRetry=True, checkForPermissionDenied=False)
Create new SshCommand instance.
 
Will wait until completed.
 
Captures returncode, and output.
 
Output may contain extraneous leading or trailing newlines and whitespace.
 
Example use::
 
    example = SshCommand(exampleSshParameters, ["ls", "-al"])
    print "returncode=" + str(example.returncode)
    print "output=" + example.output
 
sshParameters
    an SshParameters instance.
 
argv
    list of command and arguments passed to ssh.
    
    If given a string instead of a list then fixed by argv=argv.split() making a list.
    That may only work as expected for some commands on some platforms.
    It should work for a command without arguments.
    
    Hence if you don't want a string split, pass it in wrapped as sole item of a list.

Class methods defined here:
acceptKnownHostKey(cls, sshParameters, connectTimeoutSeconds=None) from __builtin__.type
Accept host's key.
 
Will wait until completed.
 
ipaddress
    IP address or domain name.
commandsUsedInImplementation(cls) from __builtin__.type
Return a list to be passed to SystemRequirements.commandsRequired().
 
This class can be passed to SystemRequirements.commandsRequiredByImplementations().
hasAcceptedKnownHostKey(cls, sshParameters) from __builtin__.type
Return whether an attempt to acceptKnownHostKey() succeeds.
 
Will wait until completed with success or failure.
 
sshParameters
    an SshParameters instance to use in the attempt.
 
return
    whether success.
isAvailable(cls, sshParameters, probingCommand='hostname') from __builtin__.type
Return whether probingCommand succeeds.
 
Will wait until completed.
removeKnownHostKey(cls, ipaddress) from __builtin__.type
Remove line from ~/.ssh/known_hosts file.
sleepUntilHasAcceptedKnownHostKey(cls, sshParameters, checkIntervalSeconds=3.0, ticker=False, extraSleepSeconds=5.0) from __builtin__.type
If available return, else loop sleeping for checkIntervalSeconds.
 
sshParameters
    an SshParameters instance to use in the attempts.
sleepUntilIsAvailable(cls, sshParameters, checkIntervalSeconds=5.0, ticker=False, probingCommand='hostname') from __builtin__.type
If available return, else loop sleeping for checkIntervalSeconds.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
output
Collected output string of command.
 
May contain extraneous leading or trailing newlines and whitespace.
returncode
Returncode of command or 255 if an ssh error occurred.
 
Could be None.

 
class SshCommandException(exceptions.Exception)
    
Method resolution order:
SshCommandException
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods defined here:
__init__(self, message)
__str__(self)

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)
message

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args

 
class SshParameters(__builtin__.object)
    Parameters needed to connect to an ssh host.
 
Implemented to avoid verbosity and complexity of passing same information
many times across several uses each time in separate arguments.
 
  Methods defined here:
__init__(self, ipaddress, user, pwd)
Create new SshParameters instance.
 
Example use::
 
    exampleSshParameters = SshParameters("10.123.45.67", "joe", "redwood")
 
ipaddress
    IP address or domain name.
 
user
    a string.
 
pwd
    a string or None.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)