Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from paramiko import SSHClient, AutoAddPolicy
- from sys import exit
- def connect(host, port, username, password):
- ssh_client = SSHClient()
- ssh_client.set_missing_host_key_policy(AutoAddPolicy())
- ssh_client.connect(host, port=port, username=username, password=password)
- return ssh_client.invoke_shell('raw')
- def write_line(channel, line):
- channel.send(line + '\n')
- def read_line(channel):
- if not hasattr(channel, 'line_buffer'):
- channel.line_buffer = ""
- while True:
- end_of_line = channel.line_buffer.find('\n\r')
- if not end_of_line == -1:
- line = channel.line_buffer[0:end_of_line]
- channel.line_buffer = channel.line_buffer[end_of_line+2:]
- return line
- channel.line_buffer += channel.recv(4096)
- def execute(channel, command):
- result = []
- write_line(channel, command)
- while True:
- line = read_line(channel)
- result.append(line)
- if line.startswith('error'):
- return result
- if __name__ == '__main__':
- channel = connect('localhost', 10022, 'serveradmin', 'secret')
- header = read_line(channel)
- if not header == 'TS3':
- exit('Not a TS3-Server...')
- motd = read_line(channel)
- result = execute(channel, 'whoami')
- print(result)
- execute(channel, 'quit')
Add Comment
Please, Sign In to add comment