-
Notifications
You must be signed in to change notification settings - Fork 17
Scripting Toolkit: SSH usage
Usage:
_ssh = SSH(dest='192.168.0.10:22',
username='USERNAME',
password='PASSWORD',
connected=callback_on_connected,
disconnected=callback_on_disconnected,
timeout=callback_on_command_timeout,
sent=ssh_sent,
sendDelimiters='\n',
receiveDelimiters='\r\n')
-
dest: IP address with port (or late via
setDest(...)
method) -
username: username to log in the server
-
password: password to log in the server
-
connected: callback when fully connected
-
disconnected: callback when disconnected (after successful connection)
def ssh_disconnected(): console.info('[ssh_disconnected]')
-
timeout: when a general IO timeout or
.request
related timeout occurs -
sendDelimiters: string to append to each
.send
(Default value is\n
). -
receiveDelimiters: characters which trigger
received
callback, orNone or ''
to avoid buffering altogether. -
disableEcho: flag to disable ECHO Technical specification (Note: this parameter may not work depending on SSH server implementation of devices)
-
RESERVED - ONLY "SHELL" SUPPORTED FOR NOW mode: 'shell' or 'exec'. what is the difference? In most cases (especially for automation), using 'exec' mode is much simpler than 'shell' mode, because you don't have to care about console out from the server. In 'exec' mode, you can make sure that you receive an entire response message when sending a command. In contrast, in 'shell' mode a single response message may be split in many parts. Please remember that 'shell' mode is for interaction with user on screen. (Note: 'exec' mode may not be supported depending on SSH server implementation of devices)
-
RESERVED - NOT IN USE knownHosts: KnownHosts for the server.
-
RESERVED - NOT IN USE reverseForwardingParams: parameters to establish reverse SSH port forwarding. What is reverse SSH port forwarding?
reverse_forwarding_params = { 'bind_address': None, 'rport': 80, 'host': 'localhost', 'lport': 80 }
-
RESERVED - NOT IN USE executed: callback to be called when a command successfully sent to the server. To check response from server, please use shellConsoleOut callback.
def ssh_executed(cmd): console.info('[cmd] %s' % cmd)
In 'shell' mode, send a command to the server and receive response.
def ssh_connected():
console.info('[ssh_connected]')
def ssh_disconnected():
console.info('[ssh_disconnected]')
def ssh_sent(data):
console.info('[ssh_sent] %s' % data)
def ssh_received(resp):
console.info('[ssh_received] %s' % resp)
ssh_con = SSH(
dest='192.168.0.10:22',
username='USERNAME',
password='PASSWORD',
connected=ssh_connected,
received=ssh_received,
sent=ssh_sent,
disconnected=ssh_disconnected)
ssh_con.send('ps -ef')
or
RESERVED - NOT IN USE - In 'exec' mode, send a command to the server and receive response.
def ssh_connected():
console.info('[ssh_connected]')
def ssh_disconnected():
console.info('[ssh_disconnected]')
def ssh_executed(cmd):
console.info('[cmd] %s' % cmd)
def ssh_shellOut(message):
console.info('[shellout] %s' % message)
ssh_con = SSH(
mode='exec',
dest='192.168.0.10:22',
username='USERNAME',
password='PASSWORD',
connected=ssh_connected,
executed=ssh_executed,
disconnected=ssh_disconnected,
shellConsoleOut=ssh_shellOut
)
ssh_con.send('ps -ef')
or
ssh_con.send('ps -ef', lambda response: console.info(response))
Nodel: http://nodel.io/ | White Paper