netssh2.session¶
Module to handle basic session
-
class
netssh2.session.
Session
(host='', user='', passwd='', key_file_private=None, key_file_public=None, key_passwd='', port=22, timeout=5000, buffer_size=1024, target_buffer_size=1024, auth_retries=5, auth_delay=2000, socket_retries=100, socket_delay=100, prompt=None, command_prompt=None, header_newline=True, newline='n', invoke_shell=False, verbose=True, logging_level='INFO')¶ Defines default session to be used across multiple vendors.
-
authenticate_session
()¶ Tries to authenticate existing session
Raises: netssh2.exceptions.NetSsh2AuthenticationError when authentication fails Raises: netssh2.exceptions.NetSsh2TooManyRetriesException if number of auth_retries is exceeded Raises: netssh2.exceptions.Exception if any other issue happens when authenticating Returns: True Return type: bool
-
clean_shell_header
()¶ Gets rid of interactive shell header. :raises: netssh2.exceptions.NetSsh2Timeout in case of timeout when reading the channel.
Returns: True Return type: bool
-
close_channel
(keep_alive=False)¶ Closes channel and returns exist status. :param keep_alive: Should we keep the channel open and just get exist status? default False :type keep_alive: bool :return: channel exit status :rtype: int
-
configure_channel
()¶ Does any channel configuration, for example invoking interactive shell if invoke_shell=True. :return: True :rtype: bool
-
configure_session
()¶ Sets up nonblocking mode, which allows to wait for socket to be ready and more control over channel. :return: True :rtype: bool
-
create_session
()¶ Connect to a host using ssh :return: True :rtype: bool
-
disconnect
()¶ Disconnect from a ssh session :return: True :rtype: bool
-
open_channel
()¶ Opens channel on ssh2 session. :raises: netssh2.exceptions.NetSsh2ChannelException if the channel is not available for some reason.
Returns: True Return type: bool
-
read_chan
(buff_size=None, stderr=False)¶ Read channel output in non blocking way. :param buff_size: Buffer size to read from the channel :type buff_size: int
Parameters: stderr (bool) – Do we want to read STDERR (True) instead of STDOUT (False)? Raises: netssh2.exceptions.NetSsh2Timeout in case of timeout when reading channel. Returns: (size, buffer) size and payload read from the channel Return type: tuple(int, str)
-
read_output
()¶ Reads output of the command we sent. :return: True :rtype: bool
-
read_stderr
()¶ Reads 1024 chars from channel STDERR. :return: size of buffer that was read :rtype: int
-
read_stdout
()¶ Reads 1024 chars from channel STDOUT. :return: size of buffer that was read :rtype: int
-
read_until_prompt
()¶ Keep reading channel until we get prompt. :return: True :rtype: bool
-
run_cmd
(cmd, return_output=False)¶ Run a command to a specific ssh session :param cmd: command to run over ssh :type cmd: string
Parameters: return_output (bool) – Should we return also output Returns: exit_status of command or (exit_status, output) in case of return_output=True Return type: int or tuple(int, str)
-
send_cmd
(cmd)¶ Sends command over ssh2 channel. :param cmd: command to send :type cmd: string
Raises: netssh2.exceptions.NetSsh2Timeout in case of timeout Returns: True Return type: bool
-
send_scp
(source_file, destination_path)¶ Send file over SCP. :param source_file: Path to source file :type source_file: string :param destination_path: Path on server to copy file to :type destination_path: string :return: exist status :rtype: string
-
set_prompt
(new_prompt)¶ Setting different prompt with opened session. :param new_prompt: new string to wait for after command with invoke_shell :type new_prompt: string
Returns: True Return type: bool
-
set_timeout
(new_timeout)¶ Setting different timeout with opened session. :param new_timeout: New timeout value in miliseconds. :type new_timeout: int
Returns: True Return type: bool
-