"""

This python module I developed is used for establishing ssh as well as ftp connection to the various in-house remote machines in the data processing and access.

The following dependcies were used for the code development :
1. paramiko - python module for SSHv2 protocol (https://www.paramiko.org/)
2. threading - python module for implementing network bound parallel processing (https://docs.python.org/3/library/threading.html)
3. sys - python module for system information (https://docs.python.org/3/library/sys.html)
4. ftputil - high level python module for ftp connection (https://ftputil.sschwarzer.net/trac/wiki/Documentation)
5. pandas - high performance python module for data analysis and computation (http://pandas.pydata.org/pandas-docs/version/0.15/tutorials.html) 
6. xlsxwriter - python module for creating .xlsx excel files (https://xlsxwriter.readthedocs.io/)
7. json - python module for JSON obejct encoding and decoding (https://docs.python.org/3/library/json.html)

This module is used as a dependency by : glitch_analysis.py
Author : Srideep Maulik

"""



from paramiko import client
import threading, paramiko, sys
import ftputil
import pandas as pd
import xlsxwriter
import json

"""
SSH object class for establising ssh connection
"""

class ssh:
    client= None
    shell = None
    transport = None
    
    def __init__(self, address, username, password):
        # Let the user know we're connecting to the server
        print("Connecting to the "+str(address)+"")
        # Create a new SSH client
        self.client = client.SSHClient()
        # The following line is required if you want the script to be able to access a server that's not yet in the known_hosts file
        self.client.set_missing_host_key_policy(client.AutoAddPolicy())
        # Make the connection
        self.client.connect(address, username=username, password=password, look_for_keys=False)
        self.transport = paramiko.Transport((address, 22))
        self.transport.connect(username=username, password=password)

        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()


    def close_connection(self):
        if (self.client != None):
            self.client.close()
            self.transport.close()


    def open_shell(self):
        self.shell = self.client.invoke_shell()


    def send_shell(self,command):
          if(self.shell):
                self.shell.send(command + "\n")
          else:
              print("Shell not opened.")

     
    def process(self):
        global connection
        while True:
            # Print data when available
            if self.shell != None and self.shell.recv_ready():
                alldata = self.shell.recv(1024)
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                strdata = str(alldata, "utf8")
                strdata.replace('\r', '')
                print(strdata, end = "")
                if(strdata.endswith("$ ")):
                    print("\n$ ", end = "")

"""
ftp function for establising ssh connection
"""


def ftp_connection(address, username, password):
    # Let the user know we're connecting to the server
    print("Establishing communication with FTP server: "+str(address)+"")
    # Establish connection
    ftp_host = ftputil.FTPHost(address,username,password)
    return ftp_host
       

    







