Advertisement
Guest User

Untitled

a guest
Sep 15th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. from Exceptions import NotConnected, UnableToConnect, FileNotFound
  2. import paramiko
  3. import select
  4.  
  5.  
  6. class SSHClient(object):
  7.  
  8.     def __init__(self):
  9.         self._ssh_client = paramiko.SSHClient()
  10.         self._ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  11.         self._following_file = False
  12.         # self._ssh_client.load_host_keys()
  13.  
  14.     def connect(self, address, user, password):
  15.         try:
  16.             self._ssh_client.connect(address, username=user, password=password)
  17.         except:
  18.             raise UnableToConnect()
  19.  
  20.     def is_connected(self):
  21.         transport = self._ssh_client.get_transport() if self._ssh_client else False
  22.         return (transport or False) and transport.is_active()
  23.  
  24.     def disconnect(self):
  25.         if self.is_connected:
  26.             self._ssh_client.close()
  27.  
  28.     def run_command(self, command, with_output=True):
  29.         if not self.is_connected():
  30.             raise NotConnected()
  31.  
  32.         print(command)
  33.         stdin, stdout, stderr = self._ssh_client.exec_command(command, get_pty=True)
  34.  
  35.         if with_output:
  36.             return [line.rstrip() for line in stdout.readlines()]
  37.  
  38.     def read_file(self, path):
  39.         sftp_client = self._ssh_client.open_sftp()
  40.         remote_file = sftp_client.open(path)
  41.         content = remote_file.readlines()
  42.         remote_file.close()
  43.         sftp_client.close()
  44.         return content
  45.  
  46.     def follow_file(self, path, buffer):
  47.         transport = self._ssh_client.get_transport()
  48.         channel = transport.open_session()
  49.  
  50.         print("tail -f " + path)
  51.         channel.get_pty()
  52.         channel.exec_command("tail -f " + path)
  53.         self._following_file = True
  54.  
  55.         last_line = ""
  56.         while self._following_file:
  57.             rl, wl, xl = select.select([channel], [], [], 0.0)
  58.             if len(rl) > 0:
  59.                 lines = [x.decode("utf-8").strip('\n') for x in channel.recv(1024).split(b'\n')]
  60.                 lines[0] = last_line + lines[0]
  61.                 last_line = lines[-1]
  62.                 for line in lines[:-1]:
  63.                     buffer.append(line)
  64.  
  65.         channel.close()
  66.         print("Stopped following file {0}".format(path))
  67.  
  68.     def stop_following_file(self):
  69.         self._following_file = False
  70.  
  71.     def create_file(self, path, remove_first=False):
  72.         if remove_first:
  73.             self.remove_file(path)
  74.         create_command = "touch " + path
  75.         self.run_command(create_command, with_output=False)
  76.  
  77.     def remove_file(self, path):
  78.         remove_command = "rm " + path
  79.         self.run_command(remove_command, with_output=False)
  80.  
  81.     def file_exists(self, path):
  82.         output = self.run_command("test -e {0} || echo file does not exist".format(path))
  83.         print(output)
  84.         if output == ["file does not exist"]:
  85.             return False
  86.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement