Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from paramiko import client
- class SSH_Agent:
- def __init__(self, server_name, username = getpass.getuser(), password = None, connection_timeout = CONNECTION_TIMEOUT):
- self.ssh_agent = client.SSHClient()
- self.ssh_agent.set_missing_host_key_policy(client.AutoAddPolicy())
- self.ssh_agent.connect(server_name, username = username, password = password if password is not None else username, timeout = connection_timeout)
- def execute_command(self, command, out_streams = [sys.stdout], err_streams = [sys.stderr], poll_intervals = POLL_INTERVALS):
- stdin, stdout, stderr = self.ssh_agent.exec_command(command)
- channel = stdout.channel
- stdin.close()
- channel.shutdown_write()
- while not channel.closed or channel.recv_ready() or channel.recv_stderr_ready():
- got_data = False
- output_channels = select.select([channel], [], [], poll_intervals)[0]
- if output_channels:
- channel = output_channels[0]
- if channel.recv_ready():
- for stream in out_streams:
- stream.write(channel.recv(len(channel.in_buffer)))
- stream.flush()
- got_data = True
- if channel.recv_stderr_ready():
- for stream in err_streams:
- stream.write(channel.recv_stderr(len(channel.in_stderr_buffer)))
- stream.flush()
- got_data = True
- if not got_data
- and channel.exit_status_ready()
- and not channel.recv_ready()
- and not channel.recv_stderr_ready():
- channel.shutdown_read()
- channel.close()
- break
- return channel.recv_exit_status()
- tput: No value for $TERM and no -T specified
- stdin, stdout, stderr = self.ssh_agent.exec_command(command, get_pty = True)
- tput: No value for $TERM and no -T specified
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement