Exemplo n.º 1
0
 def test_scpget(self):
     conn = SSHConnection('localhost', login=self.user)
     srcfile = os.path.join(self.srcdir.name, 'a')
     destfile = os.path.join(self.destdir.name, 'a')
     text = 'some text'
     self._writefile(text, srcfile)
     conn.scpget(srcfile, self.destdir.name)
     assert self._readfile(destfile) == text
Exemplo n.º 2
0
def get_ssh_conn(ip_address, login='******', identity_file=None):
    """Return ssh connection."""
    configfile = 'ssh_config'
    if not os.path.isfile(configfile):
        configfile = None
    return SSHConnection(ip_address,
                         login=login,
                         configfile=configfile,
                         identity_file=identity_file)
Exemplo n.º 3
0
    def scp(self,
            source,
            dest_path,
            mode,
            erase_owner=False,
            do_checksum=True,
            configfile=None):
        try:
            # Check to see if the source exists, raises ERROR_FILE_NOT_FOUND
            self.source_exists(source)

            # get the sha256 checksum of the source file
            sha256_checksum = checksum(source)

            # set up the ssh connection
            conn = SSHConnection(self.host,
                                 login=self.user,
                                 port=self.port,
                                 configfile=configfile,
                                 identity_file=self.ssh_key)

            # scp the file
            if erase_owner:
                owner = None
            else:
                owner = self.user
            conn.scp((source, ), target=dest_path, mode=mode, owner=owner)

            if do_checksum:
                # Now get the sha256 checksum of the remote file
                checksum_path = dest_path
                command = "if [ -d \"%s\" ]; then echo 'ISDIR'; else echo 'ISFILE'; fi" % dest_path
                ret = conn.run(command)
                try:
                    is_dir = ret.stdout.split()[0]
                    if is_dir.lower() == 'isdir':
                        checksum_path = "%s/%s" % (dest_path,
                                                   os.path.basename(source))
                except IndexError:
                    raise SSHError("SCP failed:\n  Command: %s\n  stdout: %s\n  stderr: %s" % \
                                   (command, ret.stdout, ret.stderr))

                command = "/usr/bin/sha256sum %s" % checksum_path
                ret = conn.run(command)
                try:
                    # the shell utility return the checksum + the file path, we only want the checksum
                    remote_checksum = ret.stdout.split()[0]
                    if sha256_checksum != remote_checksum:
                        raise SSHError("Checksums do not match.")
                except IndexError:
                    raise SSHError("SCP failed:\n  Command: %s\n  stdout: %s\n  stderr: %s" % \
                                   (command, ret.stdout, ret.stderr))

        except SSHError, ssh_e:
            raise SCPError("SCP failed: %s" % str(ssh_e))
Exemplo n.º 4
0
def publish():
    from datetime import datetime as dt
    from shutil import rmtree, copytree
    from os.path import join

    par_dir = CONFIG["output_dir"]
    dir_name = "{}_{}".format(par_dir, dt.strftime(dt.now(), "%Y_%m_%d_%H"))
    rmtree(dir_name, ignore_errors=True)
    copytree(par_dir, dir_name)

    if CONFIG["publish_remote"] in ("local", "localhost"):
        makedirs(CONFIG["publish_dir"], exist_ok=True)
        pubdir = join(CONFIG["publish_dir"], dir_name)
        rmtree(pubdir, ignore_errors=True)
        copytree(dir_name, pubdir)
        if CONFIG["publish_url"] is not None:
            print(
                "The plots are available at " + CONFIG["publish_url"] + "/" + dir_name
            )
    elif CONFIG["publish_remote"] is not None:
        from openssh_wrapper import SSHConnection

        print("connecting to remote server... ", end="")
        username, remote = CONFIG["publish_remote"].split("@")
        conn = SSHConnection(remote, login=username)
        print("done.")
        print("preparing destination... ", end="")
        conn.run("mkdir -p {}".format(CONFIG["publish_dir"]))
        conn.run("rm -rf {}/{}".format(CONFIG["publish_dir"], dir_name))
        print("done.")
        conn.timeout = 0
        print("copying plots... ", end="")
        conn.scp((dir_name,), CONFIG["publish_dir"])
        print("done.")
        if CONFIG["publish_data"] is not None:
            print("copying data... ", end="")
            conn.scp(
                (CONFIG["publish_data"],), join(CONFIG["publish_dir"], dir_name, "data")
            )
            print("done.")
        print("fixing permissions... ", end="")
        conn.run("chmod a+r -R {}".format(join(CONFIG["publish_dir"], dir_name)))
        print("done.")
        if CONFIG["publish_url"] is not None:
            print("The plots are available at " + join(CONFIG["publish_url"], dir_name))
Exemplo n.º 5
0
    def __init__(self,
                 via_ssh=None,
                 ssh_username=None,
                 ssh_hostname=None,
                 ssh_key=None):
        '''Constructor'''
        self._via_ssh = via_ssh
        self._ssh_username = ssh_username
        self._ssh_hostname = ssh_hostname
        self._ssh_key = ssh_key

        if self._via_ssh is True:
            if self._ssh_hostname is None:
                raise Exception("No hostname defined")

            self._ssh_connection = SSHConnection(server=self._ssh_hostname,
                                                 identity_file=self._ssh_key,
                                                 login=self._ssh_username)
Exemplo n.º 6
0
def run(host, cmd):
    print("Sending command " + (cmd) + "to host " + (host))
    conn = SSHConnection(host)
    ret = conn.run(cmd)
    print(ret)
Exemplo n.º 7
0
                self.colors[key] = get_random_hex()
            value.plot(kind='line',
                       color=self.colors[key],
                       ax=ax,
                       label=self.name + ' ' + key)


if __name__ == "__main__":
    random.seed(42)

    with open('params.json', 'r') as f:
        params = json.load(f)

    print('connect to host')
    conn = SSHConnection(params['host'],
                         login=params['ssh_user'],
                         port=params['ssh_port'])

    gpustats = {}

    print('start monitoring')
    if params['plot_graph']:
        plt.ion()
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.set_xlim(0, params['steps'])

    while True:
        if params['plot_graph']:
            ax.clear()
            ax.set_xlim(0, params['steps'])
Exemplo n.º 8
0
# In[3]:

response_dict = json.loads(r.text)
for i in response_dict:
    print("key: ", i, "val: ", response_dict[i][0])

# print(r.__dict__)

# In[17]:

print(response_dict)

# In[4]:

from openssh_wrapper import SSHConnection
conn = SSHConnection('172.20.15.87', login='******')

# In[6]:

# https://mrcissp.com/2019/12/17/python-package-paramiko/
import paramiko
import socket
vhost = "172.22.250.48"
vport = 22
vusername = "******"
vpassword = "******"

# Lets create an Object SSH
SSH = paramiko.SSHClient()

# Lets override the default policy behavior
Exemplo n.º 9
0
    def connect(self):
        """
        To establish the connection to resource.
        """
        try:
            p_module = sys._getframe().f_back.f_code.co_filename
            p_function = sys._getframe().f_back.f_code.co_name
            t = repr(traceback.format_stack())
            #for line in t:
            #    print line

            #logger.debug("Running connect function\n    - called from "+p_module+"\n    - by function "+p_function)
            logger.debug(
                "\n\nRunning connect function from %s\n    - called from %s\n    - by function %s\nTraceback :\n%s\n"
                % (self.parent_module, p_module, p_function, t))
            #logger.debug("\np_module = %s\np_function = %s\n" % (p_module, p_function))

            if not self.configfile:
                logger.debug("Variable 'self.configfile' is not defined")
                logger.error(
                    "OpenSHH configuration file's path is not defined")

            if not exists(self.configfile):
                self.createConfFiles()
            logger.debug("The socket " + join(
                Communicator.socket_dir, '%s-%s@%s:%s' %
                (self.parent_module, self.username, self.frontend,
                 self.port)) + " existance is " + str(
                     exists(
                         join(
                             Communicator.socket_dir, '%s-%s@%s:%s' %
                             (self.parent_module, self.username, self.frontend,
                              self.port)))))

            if not exists(
                    join(
                        Communicator.socket_dir, '%s-%s@%s:%s' %
                        (self.parent_module, self.username, self.frontend,
                         self.port))):
                logger.debug(
                    "No master conncection exists for %s so a new one will be created"
                    % self.parent_module)

                def first_ssh():
                    try:
                        logger.debug(
                            "Running first_ssh function\n    - Creating first connection for %s"
                            % self.parent_module)
                        #this is here because the threads are created at the same time, so the moment one creates the conection, the rest are going to cause an UnboundLocalError exception
                        #(which probably shouldn't be ocurring since ControlMaster is set to auto - only if they execute this at the same time)
                        if not exists(
                                join(
                                    Communicator.socket_dir, '%s-%s@%s:%s' %
                                    (self.parent_module, self.username,
                                     self.frontend, self.port))):
                            command = 'ssh -F %s -i %s -p %s -T %s@%s' % (
                                self.configfile, self.private_key,
                                str(self.port), self.username, self.frontend)
                        pipe = subprocess.Popen(command.split(),
                                                stdin=subprocess.PIPE,
                                                stdout=subprocess.PIPE,
                                                stderr=subprocess.PIPE)
                        out, err = pipe.communicate()

                        if err:
                            if "too long for Unix domain socket" in str(
                                    err) or "ControlPath too long" in str(err):
                                logger.debug(
                                    "Socket path was too long for Unix domain socket.\n    Creating sockets in ~/.ssh/dmr4g.\n    Exception captured in first_ssh."
                                )
                                self._change_socket_dir()
                                logger.debug(
                                    "Calling first_ssh once again, but with a new socket_dir"
                                )
                                first_ssh()
                            elif "disabling multiplexing" in str(err):
                                logger.debug(
                                    "connect function: The multiplexing of connections isn't working. Eliminating "
                                    + self.parent_module + "'s socket file.")
                                self._delete_socket()
                                first_ssh()
                            elif "bind: No such file or directory" in str(
                                    err) or "cannot bind to path" in str(err):
                                logger.debug(
                                    "The connection through the socket %s-%s@%s:%s wasn't established since the socket directory %s hasn't been created yet."
                                    % (self.parent_module, self.username,
                                       self.frontend, self.port,
                                       Communicator.socket_dir))
                                self.createConfFiles()
                                first_ssh()
                            else:
                                logger.debug(
                                    "Unexpected error occured while running first_ssh:\n"
                                    + str(err))
                                logger.warning(str(err))
                    except UnboundLocalError as err:
                        logger.warning(
                            "Local variable referenced before assignment")
                        logger.debug(str(err))

                t = threading.Thread(target=first_ssh, args=())
                t.daemon = True
                logger.debug("Starting thread with first_ssh")
                t.start()
                time.sleep(
                    5
                )  #so that there's time to make the first connection in case there was an error

            if self.conn == None:
                logger.debug("No conn exists (conn == " + str(self.conn) +
                             ") for " + self.parent_module +
                             " so a new one will be created.")
                self.conn = SSHConnection(self.frontend,
                                          login=self.username,
                                          port=str(self.port),
                                          configfile=self.configfile,
                                          identity_file=self.private_key,
                                          ssh_agent_socket=self.agent_socket,
                                          timeout=SSH_CONNECT_TIMEOUT)

            logger.debug("Ending connect function from %s" %
                         self.parent_module)

        except Exception as excep:
            if "too long for Unix domain socket" in str(excep):
                logger.debug(
                    "Socket path was too long for Unix domain socket.\n    Creating sockets in ~/.ssh/dmr4g.\n    Exception captured in connect's except."
                )
                self._change_socket_dir()
                self.connect()
            else:
                logger.error(str(excep))
                raise