Exemplo n.º 1
0
class sshConnection():

    def __init__(self, host, user, password):
        self.host = host
        self.user = user
        self.password = password
        self.connection = None
        self.channel = None

    def connect(self):
        try:
            print "[+] Estableciendo conexion SSH con %s@%s...." % (self.user, self.host)
            self.connection = SSHClient()
            self.connection.set_missing_host_key_policy(AutoAddPolicy())
            self.connection.connect(
                self.host, 
                username = self.user, 
                password = self.password, 
                allow_agent = False, 
                look_for_keys = False
            )
        except AuthenticationException, e:
            print "[+] Error de autenticacion: " ,e
            return 1
        except SSHException, e:
            print "[+] Error en SSH: " , e
            return 2
Exemplo n.º 2
0
def update_server(server):

    if utilities.ping(server):

        client = SSHClient()

        if utilities.ssh(server, client):

            print '------------------------------------'
            print server.name
            command = 'dzdo /usr/sbin/smbd -V'
            stdin, stdout, stderr = client.exec_command(command)
            try:
                samba = stdout.readlines()[0]
                print samba
            except:
                samba = "None"
                print samba

            samba = re.sub(r'Version ', '', samba)

            # check existing value, if it exists, don't update
            if str(samba) != str(server.samba):
                utilities.log_change(server, 'samba', str(server.samba), str(samba))
                LinuxServer.objects.filter(name=server).update(samba=samba, modified=timezone.now())
Exemplo n.º 3
0
def update_server(server):

    if utilities.ping(server):

        client = SSHClient()
        if utilities.ssh(server, client):
            print server.name
            # with the vio servers we want the ios.level rather than the os_level
            vio_servers = AIXServer.objects.filter(name__contains='vio')
            hmc_servers = AIXServer.objects.filter(name__contains='hmc')
            if server in vio_servers:
                command = 'cat /usr/ios/cli/ios.level'
            elif server in hmc_servers:
                command = 'lshmc -V | grep Release'
            else:
                command = 'dzdo oslevel -s'
            stdin, stdout, stderr = client.exec_command(command)

            # need rstrip() because there are extra characters at the end
            oslevel = stdout.readlines()[0].rstrip()

            if server in hmc_servers:
                oslevel = "HMC " + oslevel

            if server in vio_servers:
                oslevel = "VIO " + oslevel

            # check existing value, if it exists, don't update
            if str(oslevel) != str(server.os_level):
                utilities.log_change(server, 'oslevel', str(server.os_level), str(oslevel))
                AIXServer.objects.filter(name=server, exception=False, active=True).update(os_level=oslevel, modified=timezone.now())
Exemplo n.º 4
0
def get_proxmox_ssh(proxmox):
    proxmox_ssh = SSHClient()
    proxmox_ssh.set_missing_host_key_policy(WarningPolicy())
    proxmox_ssh.connect(proxmox['host'],
                        username=proxmox['user'].split('@')[0])

    return proxmox_ssh
Exemplo n.º 5
0
class Main():
    def __init__(self):
        self.server = "192.168.1.250"
        self.user   = "******"
        self.pw     = getpass.getpass("Password for \"fisk\" on the remote host:")
        self.initSSH("ask")
        self.execRemoteMount()
        input("Check volume mount then press enter")
        self.execRemoteClose()
        self.ssh.close()

    def sendData(self, data):
        while not self.channel.send_ready(): time.sleep(1)
        self.channel.send(str(data))

    def recvData(self, size):
        while not self.channel.recv_ready(): time.sleep(1)
        data = self.channel.recv(size)
        return data

    def execRemoteClose(self):
        stdin, stdout, stderr = self.ssh.exec_command("sudo /home/fisk/Projects/sshs/sshs close")

    def execRemoteMount(self):
        stdin, stdout, stderr = self.ssh.exec_command("sudo /home/fisk/Projects/sshs/sshs open /media/fisk/ARCHIVE1/Safe/onOLEcNsojuin.bin --key /media/fisk/ARCHIVE1/safe.keyfile")
        output = stdout.readlines()
        print(output)
        
    def initSSH(self, auto_add="no"):
        self.ssh = SSHClient() 
        self.ssh.set_missing_host_key_policy(AutoAddPolicy())
        self.ssh.connect(self.server, username=self.user, password=self.pw)
        self.channel = self.ssh.invoke_shell()
Exemplo n.º 6
0
def update_server(server):

    if utilities.ping(server):

        client = SSHClient()
        if utilities.ssh(server, client):
            print server.name
            command = 'python -V'
            stdin, stdout, stderr = client.exec_command(command)

            # https://bugs.python.org/issue18338
            # Apparently this is an old bug and only fixed in Python 3.4 and 2.7
            # where the version is sent to stderr

            try:
                version = stderr.readlines()[0].rstrip()
                version = re.sub('Python ', '', version)
            except:
                version = 'None'
            
            if version == 'None':
                try:
                    version = stdout.readlines()[0].rstrip()
                    version = re.sub('Python ', '', version)
                except:
                    version = 'None'

            print version

            # check existing value, if it exists, don't update
            if str(version) != str(server.python):
                utilities.log_change(server, 'python', str(server.python), str(version))

                LinuxServer.objects.filter(name=server).update(python=version, modified=timezone.now())
            client.close()
Exemplo n.º 7
0
def update_server(server):

    if utilities.ping(server):

        client = SSHClient()
        if utilities.ssh(server, client):
            print server.name
            command = 'lslpp -L | grep -i imper'
            stdin, stdout, stderr = client.exec_command(command)
            test = stdout.readlines()

            try:
                output = test[0].rstrip()
                imperva_version = ' '.join(output.split())
                imperva_version = imperva_version.split(" ")[1].rstrip()
                print imperva_version

                # check existing value, if it exists, don't update
                if str(imperva_version) != str(server.imperva):
                    utilities.log_change(server, 'Imperva', str(server.imperva), str(imperva_version))
                    AIXServer.objects.filter(name=server).update(imperva=imperva_version, modified=timezone.now())

            except:
                imperva_version = 'None'
                print imperva_version
                if str(imperva_version) != str(server.imperva):
                    utilities.log_change(server, 'Imperva', str(server.imperva), str(imperva_version))
                    AIXServer.objects.filter(name=server).update(imperva=imperva_version, modified=timezone.now())
Exemplo n.º 8
0
def update_server(server):

    counter = 0
    if utilities.ping(server):

        client = SSHClient()
        if utilities.ssh(server, client):

            command = 'dzdo emgr -l'
            stdin, stdout, stderr = client.exec_command(command)
            lines = stdout.readlines()
            print ""
            print "-----------------------------------"
            print server.name
            # del lines[0]
            # del lines[1]
            # del lines[2]

            for line in lines[3:-17]:
                counter = counter + 1
                print line.rstrip()

            print "Number of efixes -> " + str(counter)
            # check existing value, if it exists, don't update
            if counter != server.efix:
                utilities.log_change(server, 'efix', str(server.efix), str(counter))

                AIXServer.objects.filter(name=server).update(efix=counter, modified=timezone.now())
Exemplo n.º 9
0
def update_server(server):

    if utilities.ping(server):

        client = SSHClient()
        if utilities.ssh(server, client):
            my_output = False
            command = 'lspv | grep ASM | uniq'
            stdin, stdout, stderr = client.exec_command(command)
            output = stdout.readlines()
            for line in output:
                line = line.rstrip()
                if re.search("ASM", line):
                    my_output = True

            # FIXME
            print '--------'
            print server
            print '111111111111111111111111111'
            print output
            print '222222222222222222222222222222'
            print my_output
            print '333333333333333333333333333333'
            print server.asm

            # check existing value, if it exists, don't update
            if my_output != server.asm:
                utilities.log_change(server, 'asm', str(server.asm), str(my_output))

                AIXServer.objects.filter(name=server).update(asm=my_output, modified=timezone.now())
Exemplo n.º 10
0
 def start_monitor(self):
     local_name = platform.node()
     print "Starting Cassandra Monitor for %s" % self.server
     while True:
         try:
             ssh = SSHClient()
             ssh.load_system_host_keys()
             ssh.set_missing_host_key_policy(AutoAddPolicy())
             ssh.connect(self.server, timeout=5, allow_agent=False)
             stdin, stdout, stderr = ssh.exec_command(CASS_CMD)
             stdin.close()
             for line in stdout:
                 # Any line that shows up will be a downed server
                 # server datacenter rack status state load owns token
                 downed_node = line.split()
                 raise DownedNodeException(downed_node[0])
             stdout.close()
             err = stderr.read()
             if err:
                 raise Exception("Unknown error: %s" % str)
             stderr.close()
             ssh.close()
         except DownedNodeException as e:
             self.mark_error(e.node, e)
         except SSHException as e:
             self.mark_error(self.server, "%s could not connect to %s: %s" % (local_name, self.server, e))
         except Exception as e:
             self.mark_error(local_name, "Unknown error: %s" % e)
         else:
             self.clear_error()
         time.sleep(INTERVAL)
Exemplo n.º 11
0
class SSH(object):
    """
    package for ssh_client, in this class stores the sftp handlers and the
    ssh_client
    """

    def __init__(self, host, user, password, port=22):
        self.host = host
        self.user = user
        self.password = password
        self.port = port

    def ssh_connect(self, task_list, ret_map):
        try:
            self.clt = SSHClient()
            self.clt.load_system_host_keys()
            self.clt.set_missing_host_key_policy(AutoAddPolicy())
            self.clt.connect(hostname=self.host, username=self.user, password=self.password, port=self.port)
            self.sftp = self.clt.open_sftp()
        except Exception, e:
            try:
                self.clt.connect(hostname=self.host, username=self.user, password=self.password, port=22)
                self.sftp = self.clt.open_sftp()
            except Exception, e:
                for task in task_list:
                    if len(task) > 1:
                        ret_map[task[0]] = (0, "ssh connect error")
                return False
Exemplo n.º 12
0
def _execute_command_on_node(host, cmd):
    ssh = SSHClient()
    try:
        _setup_ssh_connection(host, ssh)
        return _open_channel_and_execute(ssh, cmd)
    finally:
        ssh.close()
Exemplo n.º 13
0
def update_server(server):

    if utilities.ping(server):

        client = SSHClient()
        if utilities.ssh(server, client):

            tmef = ''
            print "======================="
            print server.name

            try:
                command = 'lparstat -i | grep "Target Memory Expansion Factor"'
                stdin, stdout, stderr = client.exec_command(command)
                tmef = stdout.readlines()[0].rstrip()
                tmef = tmef.split()[5]
                print '->>' + tmef
                if tmef == '-':
                    tmef = 0.00
                else:
                    tmef = float(tmef)
            except:
                pass
            print "======================="
            print server.name
            print tmef

            # check existing value, if it exists, don't update
            if str(tmef) != str(server.tmef):
                utilities.log_change(server, 'tmef', str(server.tmef), str(tmef))

                AIXServer.objects.filter(name=server).update(tmef=tmef, modified=timezone.now())
Exemplo n.º 14
0
	def connectRemoteAddress(self,remote_user,remote_address,key_file):
		ssh=SSHClient()
		ssh.load_system_host_keys()
		private_key=path.expanduser("~/.ssh/id_rsa")
		key=RSAKey.from_private_key_file(private_key)
		ssh.connect(username=remote_user,hostname=remote_address,pkey=key)
		return ssh
Exemplo n.º 15
0
def update_server():

    server_count = 0
    server_list = LinuxServer.objects.filter(decommissioned=False)

    for server in server_list:

        if utilities.ping(server):

            client = SSHClient()
            if utilities.ssh(server, client):
                command = 'adquery user | grep ctesta'
                stdin, stdout, stderr = client.exec_command(command)
                try:
                    exists = stdout.readlines()[0].rstrip()
                    print '--------------------'
                    print server.name
                    print exists
                    server_count = server_count + 1
                    command2 = 'ls -l /home | grep ctesta'
                    stdin, stdout, stderr = client.exec_command(command2)
                    try:
                        exists2 = stdout.readlines()[0].rstrip()
                        print exists2
                    except:
                        print 'No home directory'

                except:
                    continue
    print "Total number of Linux servers: " + str(server_count)
Exemplo n.º 16
0
class SSHConsole(object):
    # By trying a few different reboot commands we don't need to special case
    # different types of hosts. The "shutdown" command is for Windows, but uses
    # hyphens because it gets run through a bash shell. We also delay the
    # shutdown for a few seconds so that we have time to read the exit status
    # of the shutdown command.
    reboot_commands = ["reboot", "sudo reboot", "shutdown -f -t 3 -r"]
    # Best guess at the maximum possible width of any shell prompt we encounter.
    # This needs to be tracked because we run commands through a pty, and if
    # len(prompt) + len(cmd) is more than the pty width, a newline will show up
    # in the output partway through the command. This is really far from ideal
    # but until we can run commands through ssh "exec" instead of a pty, we're
    # stuck with it.
    max_prompt_size = 100

    def __init__(self, fqdn, credentials, pty_width=1000):
        self.fqdn = fqdn
        self.credentials = credentials
        self.pty_width = pty_width
        self.connected = False
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(IgnorePolicy())

    def connect(self, usernames=None, timeout=30):
        last_exc = None
        if usernames:
            possible_credentials = {}
            for u in usernames:
                possible_credentials[u] = self.credentials[u]
        else:
            possible_credentials = self.credentials
        for username, passwords in possible_credentials.iteritems():
            first_password = True
            for p in passwords:
                try:
                    log.debug("Attempting to connect as %s", username)
                    self.client.connect(hostname=self.fqdn, username=username, password=p, timeout=timeout, look_for_keys=False, allow_agent=False)
                    log.info("Connection as %s succeeded!", username)
                    self.connected = True
                    # Nothing else to do, easiest just to return here rather
                    # than break out of two loops.
                    return
                # We can eat most of these exceptions because we try multiple
                # different auths. We need to hang on to it to re-raise in case
                # we ultimately fail.
                except AuthenticationException, e:
                    log.debug("Authentication failure.")
                    if first_password:
                        log.warning("First password as %s didn't work.", username)
                        first_password = False
                    last_exc = e
                except socket.error, e:
                    # Exit out early if there is a socket error, such as:
                    # ECONNREFUSED (Connection Refused). These errors are
                    # typically raised at the OS level.
                    from errno import errorcode
                    log.debug("Socket Error (%s) - %s", errorcode[e[0]], e[1])
                    last_exc = e
                    break
Exemplo n.º 17
0
def get_ssh_client(hostname, username, port=config.SSH_PORT,
                   password=None, pkey_filename=None, pkey=None):
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(hostname, port, username, password=password,
                key_filename=pkey_filename,
                pkey=pkey)
    return ssh
Exemplo n.º 18
0
def BruteForce(hostname, port, username, passward):
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    try:
        ssh.connect(hostname)
    except Exception, e:
        status = "error"
        pass
Exemplo n.º 19
0
def main():
    logging.basicConfig(level=logging.INFO)

    if isfile(get_config_path(__file__, '/esxi.ini')):
        config = ConfigurationINI(get_config_path(__file__, '/esxi.ini'))
    elif isfile('/etc/esxi.ini'):
        config = ConfigurationINI('/etc/esxi.ini')
    else:
        logging.critical('/etc/esxi.ini missing.')
        exit(0)

    logging.debug('Configuration file used : {conf}'.format(conf=get_config_path(__file__, '/esxi.ini')))

    try:
        ssh = SSHClient()
        ssh.set_missing_host_key_policy(AutoAddPolicy())
        ssh.connect(config['esxi']['hostip'], username=config['esxi']['username'], password=config['esxi']['password'])
    except SocketError as e:
        logging.critical('Host unreachable.')
        logging.critical(e.__str__())
        exit()

    logging.info('vim-cmd hostsvc/firmware/sync_config')
    ssh.exec_command('vim-cmd hostsvc/firmware/sync_config')
    logging.info('vim-cmd hostsvc/firmware/backup_config')
    stdin, stdout, stderr = ssh.exec_command('vim-cmd hostsvc/firmware/backup_config')

    for l in stdout:
        m = search('http://\*(.*)', l.strip())
        if m is not None:
            download = "http://{host}{position}".format(
                host=config['esxi']['hostip'],
                position=m.group(1)
            )
            logging.info("Downloading {0}".format(download))
            local_file = '{localpath}/backup-{host}-{date}.tgz'.format(
                host=config['esxi']['hostdns'],
                date=strftime(config['local']['dateformat']),
                localpath=config['local']['savepath']
            )
            urlretrieve(download, local_file)

            if config['webdav']['enabled']:
                logging.info("Uploading file on WebDAV")
                comediaoc = webdav_connect(
                    config['webdav']['host'],
                    username=config['webdav']['username'],
                    password=config['webdav']['password'],
                    protocol=config['webdav']['proto'],
                    verify_ssl=False
                )
                comediaoc.upload(local_file, '{0}/backup-{1}-{2}.tgz'.format(
                    config['webdav']['savepath'],
                    config['esxi']['hostdns'],
                    strftime(config['local']['dateformat'])
                ))

    ssh.close()
Exemplo n.º 20
0
def inspect_container(server, name):
    client = SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    client.load_system_host_keys()
    client.connect(server[0], username=server[1], password=server[2])
    stdin, stdout, stderr = client.exec_command('sudo docker inspect %s' % name)
    lines = stdout.readlines()
    client.close()
    return ''.join(lines)
Exemplo n.º 21
0
class VM():

    def __init__(self, name, backend, hostname, domain, ip):
        "Creates a connection to the client"

        self.name = name
        self.backend = backend
        self.hostname = hostname
        self.domain = domain
        self.ip = ip
        self.fqdn = '%s.%s' % (self.hostname, self.domain)
        self.locals = dict()

        show.debug("New VM object!")
        show.debug("Name: %s" % self.name)
        show.debug("Backend: %s" % self.backend)
        show.debug("Hostname: %s" % self.hostname)
        show.debug("Domain: %s" % self.domain)
        show.debug("IP: %s" % self.ip)

    def set_sudoers(self):
        show('Configuring sudo commands execution in sudoers')
        show('Using root login')
        self.connect(user='******')
        self.cmd('sed -i.bak "s/Defaults    requiretty'
                 '/# Defaults    requiretty/g" /etc/sudoers')
        self.close()

    def start(self):
        self.backend.start(self.name)

    def connect(self, user=locals.USER):
        # show('Connecting to %s' % self.fqdn)
        success = False
        timeout = 0

        show('Connecting..')

        while not (success or timeout > 60):
            try:
                self.client = SSHClient()
                self.client.set_missing_host_key_policy(WarningPolicy())
                self.client.connect(self.ip, username=user,
                                    key_filename=locals.PRIVATE_KEY)
                success = True
            except UserWarning:
                show.debug('UserWarning ignored')
            except Exception, e:
                show.debug('Caught exception: %s' % e)
                sleep(2)
                timeout += 2

        if timeout > 60:
            raise RuntimeError("Could not connect to the %s" % self.ip)

        show('Connection successful!')
Exemplo n.º 22
0
 def run(self, hostname, username, password):
     method = Device.objects.filter(host=hostname)[0].loginmethod
     if method == "ssh":
         ssh = SSHClient()
         ssh.set_missing_host_key_policy(AutoAddPolicy())
         try:
             ssh.connect(hostname=hostname, username=username, password=password, timeout=2)
             return True
         except:
             return False
Exemplo n.º 23
0
    def ssh_client(self):
        """
        When needing a more precise SSH client, returns a paramiko SSH client

        :return: paramiko.SSHClient
        """
        client = SSHClient()
        client.load_system_host_keys()
        client.connect(self.ip(), username='******', pkey=self.ssh_key())
        return client
Exemplo n.º 24
0
def get_container_list(server, image_name):
    client = SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    client.load_system_host_keys()
    client.connect(server[0], username=server[1], password=server[2])
    stdin, stdout, stderr = client.exec_command("sudo docker -H tcp://0.0.0.0:2376 ps --format '{{.Image}} {{.Names}}' | grep '%s' | awk '{print $2}'" % image_name)
    lines = stdout.readlines()
    client.close()
    
    return [line.strip() for line in lines]
Exemplo n.º 25
0
 def _uploadFiles(self, dstdir, **sshargs):
     (yield TaskOutput(u"ENTER", OutputType.NOTIFY))
     sshcli = SSHClient()
     sftpcli = None
     code = 0
     try:
         if not (yield TaskOutput(u"Conntecting to %s ..." % sshargs["hostname"])):
             raise CommandTerminated()
         sshcli.set_missing_host_key_policy(AutoAddPolicy())
         sshcli.connect(**sshargs)
         if not (yield TaskOutput(u"Connected, ready to upload ...")):
             raise CommandTerminated()
         ret = sshcli.exec_command("[ -d {0} ] && rm -rf {0}; mkdir -p {0}".format(dstdir))
         errstr = ret[2].read()
         if errstr != "":
             raise Exception(errstr)
         sftpcli = sshcli.open_sftp()
         for f in os.listdir(self.hdiff_dir):
             if f.lower().endswith(".html"):
                 localfile = os.path.join(self.hdiff_dir, f)
                 remotefile = os.path.join(dstdir, f).replace(os.sep, "/")
                 if not (yield TaskOutput(u"Uploading %s ..." % f)):
                     raise CommandTerminated()
                 sftpcli.put(localfile, remotefile)
     except CommandTerminated:
         code = -2
         (yield TaskOutput(u"Uploading Terminited", OutputType.WARN))
     except Exception as ex:
         code = -1
         (yield TaskOutput(ex.message, OutputType.ERROR))
     finally:
         if sftpcli:
             sftpcli.close()
         sshcli.close()
         (yield TaskOutput(u"EXIT %d" % code, OutputType.NOTIFY))
Exemplo n.º 26
0
class SSH(object):
    """docstring for SSH"""
    def __init__(self):
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(AutoAddPolicy())
        self.client.load_system_host_keys()

    def connect(self, server, user, key):
        logging.info("Connecting to {u}@{s} [with key:{k}]".format(
            u=user, s=server, k=key
        ))
        self.client.connect(server, username=user,
                       key_filename=key, timeout=2.0)

    def close():
        self.client.close()

    @property
    def transport(self):
        return self.client.get_transport()


    @staticmethod
    def SSHFactory(config):
        logging.debug("Creating SSH Object.")
        sClient = SSH()
        sClient.connect(config.staging,
                        config.stagingUser,
                        config.key
                        )
        return sClient
Exemplo n.º 27
0
def verify_ssh_login(userid):
    client = SSHClient()
    client.load_system_host_keys()
    # client.set_missing_host_key_policy(WarningPolicy)
    client.set_missing_host_key_policy(AutoAddPolicy())

    # TEST ONLY
    hosts = ["india.futuregrid.org"]
    key = os.path.expanduser(os.path.join("~", ".ssh", "id_rsa"))
    print "[key: %s]" % key

    if not userid:
        userid = getpass.getuser()

    for host in hosts:
        try:
            client.connect(host, username=userid, key_filename=key)
            client.close()
            print "[%s] succeeded with %s." % (host, userid)
        except (BadHostKeyException,
                AuthenticationException,
                SSHException) as e:
            # print sys.exc_info()
            print ("[%s] %s with %s. Please check your ssh setup (e.g. key " +
                   "files, id, known_hosts)") % (host, e, userid)
Exemplo n.º 28
0
Arquivo: main.py Projeto: dobe/pypsh
 def _exec(self):
     exitcode = 0
     client = SSHClient()
     client.load_system_host_keys()
     client.set_missing_host_key_policy(WarningPolicy)
     try:
         client.connect(self.config.get('hostname'),
                        int(self.config.get('port', 22)),
                        username=self.config.get('user'))
         stdin, stdout, stderr = self.exec_command(client)
         for i, line in enumerate(stdout):
             line = line.rstrip()
             print("{0}: {1}".format(self.host, line))
         for i, line in enumerate(stderr):
             line = line.rstrip()
             print(colored("{0}: {1}".format(self.host, line), 'red'))
             exitcode = 1
     except IOError as e:
         print(colored('{0}: {1}'.format(self.host, str(e)), 'red'))
         exitcode = 1
     except (BadHostKeyException, AuthException, SSHException) as e:
         print(colored('{0}: {1}'.format(self.host, e.message)), 'red')
         exitcode = 1
     finally:
         client.close()
         return exitcode
Exemplo n.º 29
0
 def startConnection(self): 
     """
     connection to remote server
     can launch a thread checking every minute your mailbox...
     """
     #if not self.directory:return
     #self.parent().close()
     self.close()
     client = SSHClient()
     client.set_missing_host_key_policy(AutoAddPolicy())
     try:
         client.connect(str(self.host.text()), username=str(self.username.text()), password=str(self.password.text()))
     except SSHException: qApp.instance().showErrorMessage("Error", "The ssh session could not be established")
     except AuthenticationException: qApp.instance().showErrorMessage("Error","Authentication failed")
     except BadHostKeyException: qApp.instance().showErrorMessage("Error", "the server's host key could not be verified")
     
     sftpCp = self.cleaningRepository(client)
     if sftpCp:
         sftp = client.open_sftp()
         self.sftpCopying(sftp)
         sftp.close()
     else:
         self.retrieveDirTree(c, out)
     self.makeRScript(client)
     self.makeShellScript(client)
     if self.interactiveSession.isChecked():
         import interactive
         print ('invoking a shell')
         chan = client.invoke_shell()
         chan.send('bash\n')
         interactive.interactive_shell(chan)
         chan.close()
     else:
         self.launchCommand(client)
     client.close()
Exemplo n.º 30
0
 def __init__(self,hostname, username):
     self._ssh = SSHClient()
     self._ssh.load_system_host_keys()
     self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     self._ssh.connect(hostname=hostname, username=username, timeout=10)
Exemplo n.º 31
0
    def client(self, command):
        s = SSHClient()
        if self.no_auth is True:
            s.set_missing_host_key_policy(AutoAddPolicy())
        else:
            s.load_system_host_keys()

        s.connect(self.host, self.port)
        stdin, stdout, stderr = s.exec_command(command)
        return stdout.read()
        s.close()
Exemplo n.º 32
0
class RemoteClient:
    '''Client for connecting to remote host via SSH & SCP'''
    def __init__(self, host, user, key='id_rsa', remote_path='~'):
        self.host = host
        self.user = user
        self.remote_path = remote_path
        self.key_file = str(Path.home()) + '/.ssh/' + key
        self.client = None
        self.scp = None
        self.conn = None

    def _get_ssh_key(self, pword=None):
        '''Fetch locally stored SSH key'''
        try:
            self.ssh_key = RSAKey.from_private_key_file(self.key_file)
            log.debug(f'Found RSA key at {self.key_file}')
        except PasswordRequiredException:
            pword = getpass('Enter SSH key password:'******'Found passworded RSA key at {self.key_file}')
        except SSHException as e:
            log.error(e)

    def _connect(self):
        '''Open connection to remote host'''
        if self.conn is None:
            try:
                self._get_ssh_key()
                self.client = SSHClient()
                self.client.load_system_host_keys()
                self.client.set_missing_host_key_policy(AutoAddPolicy())
                self.client.connect(
                    self.host,
                    username=self.user,
                    pkey=self.ssh_key,
                    look_for_keys=True,
                    timeout=5000
                )
                self.scp = SCPClient(self.client.get_transport())
            except (AuthenticationException, SSHException) as e:
                log.error(f'Connection failed: {e}')
                raise e
        return self.client

    def set_loglevel(self, level="ERROR"):
        log.setLevel(level)
        return log.getLevelName()

    def disconnect(self):
        '''Close ssh connection'''
        if self.client:
            self.client.close()
        if self.scp:
            self.scp.close()

    def execute(self, command):
        '''
        Execute command on remote host.

        :param command: Unix command as a single string.
        :type command: str
        '''
        self.conn = self._connect()
        stdin, stdout, stderr = self.client.exec_command(command)
        stdout.channel.recv_exit_status()
        for error in stderr.readlines():
            log.error(f'INPUT: {command} | ERR: {error}')
        return stdout.readlines()

    def upload(self, file, r=False):
        '''
        Upload file to a remote directory.

        :param files: filename as string.
        :type files: str
        '''
        self.conn = self._connect()
        try:
            self.scp.put(file, recursive=r, remote_path=self.remote_path)
        except SCPException as e:
            log.error(f'File transfer error: {e}')
            raise e
        log.info(f'Uploaded {self.remote_path}/{file} on {self.host}')

    def download(self, file, r=False):
        '''
        Download file from remote directory.

        :param files: filename as string
        :type files: str
        '''
        self.conn = self._connect()
        try:
            self.scp.get(file, recursive=r)
        except SCPException as e:
            log.error(f'File transfer error: {e}')
            raise e
        log.info(f'Pulled {self.remote_path}/{file} from {self.host}')
Exemplo n.º 33
0
# Define env vars
ROOT = os.environ['INSTALL_DIR']
POOL = os.environ['POOL_DIR']
TRASH = os.environ['TRASH_DIR']
DEPLOY = os.environ['DEPLOY_DIR']
SUPERVISOR_USER = os.environ['SUPERVISOR_USER']
SUPERVISOR_IP = os.environ['SUPERVISOR_IP']
ENVVAR_PATH = os.environ['ENVVAR_PATH']
ENVVAR_NAME = os.environ['ENVVAR_NAME']
POST_INSTALL_PATH = os.environ['POST_INSTALL_PATH']
POST_INSTALL_NAME = os.environ['POST_INSTALL_NAME']
APACHE_ENVVARS = '/etc/apache2/envvars'

# Define ssh connection to supervisor
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(SUPERVISOR_IP,username=SUPERVISOR_USER)

# SCPCLient takes a paramiko transport as its only argument
scp = SCPClient(ssh.get_transport())

# Make log file
logging.basicConfig(filename='debug.log', level=logging.DEBUG)

# Helper functions

def post_install():
    """Execute post installation script"""
    log = ''
Exemplo n.º 34
0
class ssh(ClientBase, Commands):
    def __init__(self, sessions, options):
        """
            Initialize the SSH Bee, and the Base classes.

        :param sessions: A dict which is updated every time a new session is created.
        :param options: A dict containing all options
        """
        ClientBase.__init__(self, sessions, options)
        Commands.__init__(self)
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(AutoAddPolicy())
        self.comm_chan = None

    def start(self):
        """
            Launches a new SSH client session on the server taken from the `self.options` dict.

        :param my_ip: IP of this Client itself
        """
        username = self.options['username']
        password = self.options['password']
        server_host = self.options['server']
        server_port = self.options['port']
        honeypot_id = self.options['honeypot_id']

        session = self.create_session(server_host, server_port, honeypot_id)

        self.sessions[session.id] = session
        logger.debug(
            'Sending %s bait session to {0}:{1}. (bait id: %s)'.format(
                'ssh', server_host, server_port, session.id))
        try:
            self.connect_login()
            session.did_connect = True
            # TODO: Handle failed login
            session.add_auth_attempt('plaintext',
                                     True,
                                     username=username,
                                     password=password)
            session.did_login = True
        except (SSHException, AuthenticationFailed) as err:
            logger.debug('Caught exception: {0} ({1})'.format(
                err, str(type(err))))
        else:
            command_count = 0
            command_limit = random.randint(6, 11)
            while command_count < command_limit:
                command_count += 1
                self.sense()
                comm, param = self.decide()
                self.act(comm, param)
                gevent.sleep(random.uniform(0.4, 5.6))
            self.logout()
            session.did_complete = True
        finally:
            session.alldone = True

    def send_command(self, cmd):
        """
            Send a command to the remote SSH server.

        :param cmd: The command to send
        """
        logger.debug('Sending {0} command.'.format(cmd))
        self.comm_chan.sendall(cmd + '\n')

    def get_response(self):
        """
            Get the response from the server. *This may not return the full response*

        :return: Response data
        """
        while not self.comm_chan.recv_ready():
            time.sleep(0.5)
        return self.comm_chan.recv(2048)

    def connect_login(self):
        """
            Try to login to the Remote SSH Server.

        :return: Response text on successful login
        :raise: `AuthenticationFailed` on unsuccessful login
        """
        self.client.connect(self.options['server'], self.options['port'],
                            self.options['username'], self.options['password'])
        self.comm_chan = self.client.invoke_shell()
        time.sleep(1)  # Let the server take some time to get ready.
        while not self.comm_chan.recv_ready():
            time.sleep(0.5)
        login_response = self.comm_chan.recv(2048)
        if not login_response.endswith('$ '):
            raise AuthenticationFailed
        return login_response

    def logout(self):
        """
            Logout from the remote server
        """
        self.send_command('exit')
        self.get_response()
        self.comm_chan.close()
Exemplo n.º 35
0
from paramiko import SSHClient, AutoAddPolicy
''' EXERCISE 2, point no. 1 '''
# to telnet to a server
# telnet <ip address> <port no>

ssh = SSHClient()
ssh.load_system_host_keys()  # to look for trusted hosts
ssh.set_missing_host_key_policy(
    AutoAddPolicy())  # to add the host if not already added
ssh.connect('192.168.56.101', username='******', password='******')

# to check disk usage
print("//////////////Disk Usage//////////////")
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('df -H')
for i in ssh_stdout:
    print(i.strip('\n'))

# to check inode usage
print("//////////////iNode Usage//////////////")
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('df -i')
for i in ssh_stdout:
    print(i.strip('\n'))

# to list files & directory
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('cd code/; ls -l')
for i in ssh_stdout:
    print(i.strip('\n'))

# to use ftp & sftp
# ftp <ip address>
# <username>
Exemplo n.º 36
0
from paramiko import SSHClient
from paramiko import AutoAddPolicy
import time

mip = '192.168.1.31'
ml = 'O2_Check'
mp = 'admin2'

ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(mip, port=22, username=ml, password=mp)

i = 0
while i < 10:
    exe = "put [/queue simple get queue-O2 byte]"
    excmd2 = ssh.exec_command(exe)[1].read()
    print(excmd3)
    str(excmd2)
    sp = []
    sp = str(excmd2).split('/')
    l1 = int(sp[0][2:])
    l2 = int(sp[1][0:-5])
    MBite = (l1 + l2) / 1000000
    print('Vodafone_Test: ', MBite, "Mb", '\n')
    i += 1
    time.sleep(15)

ssh.close()
Exemplo n.º 37
0
 def __init__(self, **kwargs):
     self.client = SSHClient()
     self.client.set_missing_host_key_policy(AutoAddPolicy())
     self.kwargs = kwargs
Exemplo n.º 38
0
def copyGraphToDestination(graphDBname):
    # If the graph folder existed, remove it.
    transport = paramiko.Transport(NEO4J_DESTINATION_SERVER_NAME)
    transport.connect(username=ssh_username, password=ssh_password)

    sftp = paramiko.SFTPClient.from_transport(transport)
    dbFolderExisted = False
    try:
        sftp.stat(
            os.path.join(NEO4J_DESTINATION_DIR_PATH, 'data/databases/',
                         graphDBname + '_successful_copy'))
        print("Database exists remotely.")
        return graphDBname
    except IOError:
        print("Database copy flag does not exist remotely.")

    try:
        sftp.stat(
            os.path.join(NEO4J_DESTINATION_DIR_PATH, 'data/databases/',
                         graphDBname + '.db'))  # Test if remote_path exists
        print(
            "Database folder existed remotely, but the copy flag does not exist."
        )
        print(
            os.path.join(NEO4J_DESTINATION_DIR_PATH, 'data/databases/',
                         graphDBname + '.db'))
        sys.exit("Error: Need manual check, database exists remotely.")
    except IOError:
        print("Database folder did not existed remotely.")
    sftp.close()

    print("Start copying graph Database...")
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(NEO4J_DESTINATION_SERVER_NAME,
                username=ssh_username,
                password=ssh_password)

    # SCPCLient takes a paramiko transport as an argument, used this one to copy recursive
    scp = SCPClient(ssh.get_transport(), progress=progress)
    scp.put(os.path.join(NEO4J_SOURCE_SERVER_PATH, 'data/databases/',
                         graphDBname + '.db'),
            recursive=True,
            remote_path=os.path.join(NEO4J_DESTINATION_DIR_PATH,
                                     'data/databases/'))
    scp.put(
        os.path.join(NEO4J_SOURCE_SERVER_PATH, 'data/databases/',
                     'db_successful_copy'),
        os.path.join(NEO4J_DESTINATION_DIR_PATH, 'data/databases/',
                     graphDBname + '_successful_copy'))
    scp.close()
    ssh.close()
    print("Finished copying graph Database.")
Exemplo n.º 39
0
from pynput.mouse import Listener
from scp import SCPClient
from paramiko import SSHClient
import logging
import pyautogui

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('ex.com', 22, "USERNAME", "PASSWORD")
scpCl = SCPClient(ssh.get_transport())

count = 0


def on_click(x, y, button, pressed):
    if pressed:
        screensht = pyautogui.screenshot()
        scpCl.put(screensht, "test" + str(count) + ".png")
        #screensht.save("test"+str(count)+".png")
        global count
        count += 1


with Listener(on_click=on_click) as listener:
    listener.join()
    scpCl.close()
Exemplo n.º 40
0
import random
import sys
import os
import select
import paramiko
from paramiko import SSHClient
import subprocess
import time

client_master = SSHClient()
client_master.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client_master.load_system_host_keys()
client_master.connect("10.10.1.89", username="******", password="******")
client_slave1 = SSHClient()
client_slave1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client_slave1.load_system_host_keys()
client_slave1.connect("10.10.1.90", username="******", password="******")
client_slave2 = SSHClient()
client_slave2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client_slave2.load_system_host_keys()
client_slave2.connect("10.10.1.51", username="******", password="******")


def write_stop(file, diff, misconfig_code, client, write_dir):
    file_lines = []
    ftp = client.open_sftp()
    remote_file = ftp.open("/home/ubuntu/Neha_Shreya/" + file)
    for line in remote_file:
        file_lines.append(line)
    file_lines.append("TIME:" + str(diff) + ",CODE:" + str(misconfig_code) +
                      "\n")
Exemplo n.º 41
0
 def __init__(self):
     self.sc = SSHClient()
     self.sc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Exemplo n.º 42
0
def get_info_top_cpu(ssh: paramiko.SSHClient):
    results: dict = {}
    std_in, std_out, std_err = ssh.exec_command('top -H -b -d 1 -n 10')
    res, err = std_out.read(), std_err.read()
    result_top_cpu = bytes.decode(res if res else err).strip()

    lines = re.sub(' +', ' ', result_top_cpu).split('\n')
    cpu_lines = []
    # 提取top命令中的多条CPU信息
    for line in lines:
        if len(line) > 8 and line[0: 8] == '%Cpu(s):':
            cpu_lines.append(line.strip())
    args_cpu_us = []  # 数组: 用户空间占用CPU百分比
    args_cpu_sy = []  # 数组: 内核空间占用CPU百分比
    args_cpu_ni = []  # 数组: 用户进程空间内改变过优先级的进程占用CPU百分比
    args_cpu_id = []  # 数组: 空闲CPU百分比
    args_cpu_wa = []  # 数组: 等待输入输出的CPU时间百分比
    args_cpu_hi = []  # 数组: 硬件CPU中断占用百分比
    args_cpu_si = []  # 数组: 软中断占用百分比
    args_cpu_st = []  # 数组: 虚拟机占用百分比
    for cpu_line in cpu_lines:
        args_cpu_info = cpu_line.split(',')
        args_cpu_us.append(float(args_cpu_info[0].strip().split(' ')[1]))
        args_cpu_sy.append(float(args_cpu_info[1].strip().split(' ')[0]))
        args_cpu_ni.append(float(args_cpu_info[2].strip().split(' ')[0]))
        args_cpu_id.append(float(args_cpu_info[3].strip().split(' ')[0]))
        args_cpu_wa.append(float(args_cpu_info[4].strip().split(' ')[0]))
        args_cpu_hi.append(float(args_cpu_info[5].strip().split(' ')[0]))
        args_cpu_si.append(float(args_cpu_info[6].strip().split(' ')[0]))
        args_cpu_st.append(float(args_cpu_info[7].strip().split(' ')[0]))
    """ 获取cpu各项信息的最大值、最小值、平均值 """
    # 用户空间占用CPU百分比
    results['cpu_us_max'] = max(args_cpu_us)
    results['cpu_us_min'] = min(args_cpu_us)
    results['cpu_us_avg'] = int(sum(args_cpu_us) / len(args_cpu_us) * 100) / 100  # 乘100再除100,是为了处理float精度问题
    # 内核空间占用CPU百分比
    results['cpu_sy_max'] = max(args_cpu_sy)
    results['cpu_sy_min'] = min(args_cpu_sy)
    results['cpu_sy_avg'] = int(sum(args_cpu_sy) / len(args_cpu_sy) * 100) / 100  # 乘100再除100,是为了处理float精度问题
    # 用户进程空间内改变过优先级的进程占用CPU百分比
    results['cpu_ni_max'] = max(args_cpu_ni)
    results['cpu_ni_min'] = min(args_cpu_ni)
    results['cpu_ni_avg'] = int(sum(args_cpu_ni) / len(args_cpu_ni) * 100) / 100  # 乘100再除100,是为了处理float精度问题
    # 空闲CPU百分比
    results['cpu_id_max'] = max(args_cpu_id)
    results['cpu_id_min'] = min(args_cpu_id)
    results['cpu_id_avg'] = int(sum(args_cpu_id) / len(args_cpu_id) * 100) / 100  # 乘100再除100,是为了处理float精度问题
    # 等待输入输出的CPU时间百分比
    results['cpu_wa_max'] = max(args_cpu_wa)
    results['cpu_wa_min'] = min(args_cpu_wa)
    results['cpu_wa_avg'] = int(sum(args_cpu_wa) / len(args_cpu_wa) * 100) / 100  # 乘100再除100,是为了处理float精度问题
    # 硬件CPU中断占用百分比
    results['cpu_hi_max'] = max(args_cpu_hi)
    results['cpu_hi_min'] = min(args_cpu_hi)
    results['cpu_hi_avg'] = int(sum(args_cpu_hi) / len(args_cpu_hi) * 100) / 100  # 乘100再除100,是为了处理float精度问题
    # 软中断占用百分比
    results['cpu_si_max'] = max(args_cpu_si)
    results['cpu_si_min'] = min(args_cpu_si)
    results['cpu_si_avg'] = int(sum(args_cpu_si) / len(args_cpu_si) * 100) / 100  # 乘100再除100,是为了处理float精度问题
    # 虚拟机占用百分比
    results['cpu_st_max'] = max(args_cpu_st)
    results['cpu_st_min'] = min(args_cpu_st)
    results['cpu_st_avg'] = int(sum(args_cpu_st) / len(args_cpu_st) * 100) / 100  # 乘100再除100,是为了处理float精度问题

    return results
Exemplo n.º 43
0
class SFtpClient(object):
    _client = None

    def __init__(self, credentials):
        self._client = SSHClient()
        self._host = credentials['host']
        self._port = int(credentials['port']) if 'port' in credentials else 22
        self._client.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))

        private_key = None
        if 'private_key' in credentials and credentials['private_key']:
            key_data = credentials['private_key']
            if not isinstance(key_data, str):
                key_data = str(key_data, "utf-8")
            key_file = io.StringIO()
            key_file.write(key_data)
            key_file.seek(0)
            if 'private_key_password' in credentials and \
                    credentials['private_key_password'] != "":
                private_key_password = credentials['private_key_password']
            else:
                private_key_password = None
            private_key = RSAKey.from_private_key(
                key_file,
                password=private_key_password)

        retries = 5
        passwd = credentials['password'] if 'password' in credentials else None
        while True:
            try:
                self._client.connect(
                    self._host,
                    port=self._port,
                    username=credentials['user'],
                    pkey=private_key,
                    password=passwd,
                    look_for_keys=False
                )
            except ssh_exception.SSHException as err:
                if retries > 0 and \
                        str(err) == "Error reading SSH protocol banner":
                    retries -= 1
                    logging.getLogger("paramiko"). \
                        warning("Retrying SSH connection: " + str(err))
                    continue
                else:
                    raise err
            break

    def sendKeyFile(self, ssh_client, localpath, remotepath):
        self.sendFile(localpath, remotepath)
        # Give 600 permissions to file
        return ssh_client.execute_shell_command('chmod 600 ' + remotepath,wait_result=True)

    def sendFile(self, localpath, remotepath):
        sftp = self._client.open_sftp()
        sftp.put(localpath, remotepath)
        sftp.close()

    def removeFile(self, remotepath):
        sftp = self._client.open_sftp()
        sftp.remove(remotepath)
        sftp.close()

    def close_connection(self):
        """Closes opened connection"""
        if self._client is not None:
            self._client.close()
Exemplo n.º 44
0
from paramiko import SSHClient, AutoAddPolicy
from threading import Thread

import logging
logging.basicConfig(filename="run_bc.log")
log = logging.getLogger("paramiko")
log.setLevel(logging.DEBUG)

client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())

client.connect('192.168.56.105', username='******', password='******')
stdin, stdout, stderr = client.exec_command('python')


def send_command(out):
    while True:
        line = input("Enter command: ")
        out.write(line + "\n")


def get_result(instream):
    for line in instream:
        print(line, flush=True)


t1 = Thread(target=send_command, args=(stdin, ))
t2 = Thread(target=get_result, args=(stdout, ))
t1.start()
t2.start()
Exemplo n.º 45
0
 def Start_Async_Show(self, host, user, passwd):
   ssh = SSHClient()
   ssh.load_system_host_keys()
   ssh.set_missing_host_key_policy(client.AutoAddPolicy())
   ssh.connect(host, port=22, username=user, password=passwd, timeout=30)
   channel = ssh.invoke_shell()
   channel.exec_command("show router bgp routes | match Routes")
   ssh.close()
Exemplo n.º 46
0
class RemoteClient:
    """Client to interact with a remote host via SSH & SCP."""

    def __init__(self, host, user, ssh_key_filepath, remote_path):
        self.host = host
        self.user = user
        self.ssh_key_filepath = ssh_key_filepath
        self.remote_path = remote_path
        self.client = None
        self.scp = None
        self.conn = None
        self._upload_ssh_key()

    def _get_ssh_key(self):
        """
        Fetch locally stored SSH key.
        """
        try:
            self.ssh_key = RSAKey.from_private_key_file(self.ssh_key_filepath)
            logger.info(f'Found SSH key at self {self.ssh_key_filepath}')
        except SSHException as error:
            logger.error(error)
        return self.ssh_key

    def _upload_ssh_key(self):
        try:
            system(f'ssh-copy-id -i {self.ssh_key_filepath} {self.user}@{self.host}>/dev/null 2>&1')
            system(f'ssh-copy-id -i {self.ssh_key_filepath}.pub {self.user}@{self.host}>/dev/null 2>&1')
            logger.info(f'{self.ssh_key_filepath} uploaded to {self.host}')
        except FileNotFoundError as error:
            logger.error(error)

    def _connect(self):
        """
        Open connection to remote host.
        """
        if self.conn is None:
            try:
                self.client = SSHClient()
                self.client.load_system_host_keys()
                self.client.set_missing_host_key_policy(AutoAddPolicy())
                self.client.connect(self.host,
                                    username=self.user,
                                    key_filename=self.ssh_key_filepath,
                                    look_for_keys=True,
                                    timeout=5000)
                self.scp = SCPClient(self.client.get_transport())
            except AuthenticationException as error:
                logger.info('Authentication failed: did you remember to create an SSH key?')
                logger.error(error)
                raise error
        return self.client

    def disconnect(self):
        """
        Close ssh connection.
        """
        self.client.close()
        self.scp.close()

    def bulk_upload(self, files):
        """
        Upload multiple files to a remote directory.

        :param files: List of strings representing file paths to local files.
        """
        self.conn = self._connect()
        uploads = [self._upload_single_file(file) for file in files]
        logger.info(f'Finished uploading {len(uploads)} files to {self.remote_path} on {self.host}')

    def _upload_single_file(self, file):
        """Upload a single file to a remote directory."""
        try:
            self.scp.put(file,
                         recursive=True,
                         remote_path=self.remote_path)
        except SCPException as error:
            logger.error(error)
            raise error
        finally:
            logger.info(f'Uploaded {file} to {self.remote_path}')

    def download_file(self, file):
        """Download file from remote host."""
        self.conn = self._connect()
        self.scp.get(file)

    def execute_commands(self, commands):
        """
        Execute multiple commands in succession.

        :param commands: List of unix commands as strings.
        """
        self.conn = self._connect()
        for cmd in commands:
            stdin, stdout, stderr = self.client.exec_command(cmd)
            stdout.channel.recv_exit_status()
            response = stdout.readlines()
            for line in response:
                logger.info(f'INPUT: {cmd} | OUTPUT: {line}')
Exemplo n.º 47
0
#!/usr/bin/python

import scp
import json
from paramiko import SSHClient
from scp import SCPClient
from avi.sdk.avi_api import ApiSession

host = "10.91.55.11"
user = "******"
password = "******"

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(host, username=user, password=password)

scp = SCPClient(ssh.get_transport())
scp.put('../controller.17.2.11.pkg', remote_path='/tmp')

session = ssh.get_transport().open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command(
    'sudo cp /tmp/controller.17.2.11.pkg /var/lib/avi/upgrade_pkgs/controller.pkg'
)
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stdin.write(password + '\n')
stdin.flush()
print(stdout.read().decode("utf-8"))
session.close()
Exemplo n.º 48
0
    dump = file.read()
    dump = dump.splitlines()
    current_os = platform.system().lower()

    if current_os == "windows":
        param = "-n"
        output = "\Desktop\output.txt"
        homedir = os.environ['USERPROFILE']
    else:
        param = "-c"
        output = "/Desktop/output.txt"
        homedir = os.environ['HOME']

    for ip in dump:
        print(ip)
        client = SSHClient()
        client.set_missing_host_key_policy(AutoAddPolicy())
        client.connect(ip, username='******', password='******')
        stdin, stdout, stderr = client.exec_command(f'ping -c 3 x.x.x.x')
        res = f'STDOUT: |{ip}| {stdout.read().decode("utf8")}'
        print(f'STDERR: {stderr.read().decode("utf8")}')
        file = open(f'hotexamples_com/Desktop/output.txt', 'a')
        file.write(res)
        file.close()

    # Because they are file objects, they need to be closed
    stdin.close()
    stdout.close()
    stderr.close()

    # Close the client itself
Exemplo n.º 49
0
#!/usr/bin/python

from paramiko import SSHClient
from userpass import Userpass
import os, sys

if len(sys.argv) < 3:
    print "usage: {} <path_to_authfile> <router>".format(sys.argv[0])
    exit()

#create ssh handler and read in keys
ssh = SSHClient()
keypath = os.path.expanduser('~/.ssh/known_hosts')
ssh.load_host_keys(keypath)

#load userpass file
userpass = Userpass(sys.argv[1])

#connect
host = sys.argv[2]
ssh.connect( host, username=userpass.user, password=userpass.passwd )

stdin, stdout, stderr = ssh.exec_command( 'show version' )
for line in stdout:
  print '... ' + line.strip('\n')

print "==="

stdin, stdout, stderr = ssh.exec_command( 'show chassis hardware' )
for line in stdout:
  print '... ' + line.strip('\n')
Exemplo n.º 50
0

#loop through all servers
for serverip in servers:
	out = check_output(["nmap", "-p", "22", serverip])
	
	if out.find("down") == -1:
	    print "" + serverip + " \033[92m OPEN\033[0m"
	    runServerChecks(serverip)
	else:
		print "" + serverip + " \033[91m CLOSED\033[0m"
		print "" #whitespace

#check the connectionspeed between switches 3 and 19
port = 22
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect("10.10.2.19", port, username="******", password="******")

#heck current available congestion controls
print "Connection between switches"
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("ethtool eth1")

while True:
	line = ssh_stdout.readline()

	if line == '':
		break

	print line
Exemplo n.º 51
0
"""
This file grabs a log from the roboRIO and copies it a local directory.

@author Arvind
"""

# Imports
from paramiko import SSHClient
from scp import SCPClient

# Declartion
ssh = SSHClient()

# Initialization
ssh.load_system_host_keys()
ssh.connect("172.22.11.2", "lvuser")

# Get log
with SCPClient(ssh.get_transport()) as scp:
    scp.get("~/log/test-log.log",
            "C:/Users/BUZZ-175/Development/RoboRIOLogs/test-log.log")
Exemplo n.º 52
0
    def connect(
        self,
        client: paramiko.SSHClient,
        hostname: str,
        port: int = 22,
        log: bool = True,
        *,
        sock: paramiko.ProxyCommand | paramiko.Channel | socket.socket
        | None = None,
    ) -> None:
        """Connect SSH client object using credentials.

        :param client: SSH Client (low level)
        :type client: paramiko.SSHClient
        :param hostname: remote hostname
        :type hostname: str
        :param port: remote ssh port
        :type port: int
        :param log: Log on generic connection failure
        :type log: bool
        :param sock: socket for connection. Useful for ssh proxies support
        :type sock: typing.Optional[typing.Union[paramiko.ProxyCommand, paramiko.Channel, socket.socket]]
        :raises PasswordRequiredException: No password has been set, but required.
        :raises AuthenticationException: Authentication failed.
        """
        kwargs: dict[str, typing.Any] = {}

        if self.__passphrase is not None:
            kwargs["passphrase"] = self.__passphrase
        if sock is not None:
            kwargs["sock"] = sock

        for index, key in sorted(enumerate(self.__keys),
                                 key=lambda i_k: i_k[0] != self.__key_index):
            kwargs["pkey"] = key
            try:
                # noinspection PyTypeChecker
                client.connect(
                    hostname=hostname,
                    port=port,
                    username=self.username,
                    password=self.__password,
                    key_filename=self.
                    __key_filename,  # type: ignore[arg-type]  # types verified by not signature
                    **kwargs,
                )
                if index != self.__key_index:
                    self.__key_index = index
                    LOGGER.debug(
                        f"Main key has been updated, public key is: \n{self.public_key}"
                    )
                return
            except paramiko.PasswordRequiredException:
                if self.__password is None:
                    LOGGER.exception("No password has been set!")
                    raise
                LOGGER.critical(
                    "Unexpected PasswordRequiredException, when password is set!"
                )
                raise
            except (paramiko.AuthenticationException,
                    paramiko.BadHostKeyException):
                continue
        msg: str = "Connection using stored authentication info failed!"
        if log:
            LOGGER.exception(msg)
        raise paramiko.AuthenticationException(msg)
Exemplo n.º 53
0
#!/usr/bin/env python
from os import path
from sys import exit, argv, stderr
from configparser import ConfigParser
from argparse import ArgumentParser
from subprocess import run
from paramiko import SSHClient, ssh_exception, AutoAddPolicy

argparser = ArgumentParser()
confparser = ConfigParser()
sshcli = SSHClient()
config_path = "creds.config"


class MAINW:
    def __init__(self):
        sshcli.set_missing_host_key_policy(AutoAddPolicy())
        argparser.add_argument("-STA",
                               "--start",
                               help="Wokes up server",
                               action="store_true")
        argparser.add_argument("-STP",
                               "--stop",
                               help="Stops server",
                               action="store_true")
        if len(argv) == 1:
            argparser.print_help(stderr)
            exit(1)
        elif argparser.parse_args().start:
            self.door(argumentto=1)
        elif argparser.parse_args().stop:
Exemplo n.º 54
0
from scp import SCPClient
from paramiko import SSHClient, AutoAddPolicy

import boto3
import os
import sys

# Fetching environment variables from .env file
load_dotenv()

# AWS Services Session
bs = boto3.client('elasticbeanstalk')
ec2 = boto3.resource('ec2')

# Setting up SSH Client
ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())

# Variables
keypair_path = os.getenv("KEYPAIR_PATH")
pkey_passphrase = os.getenv("PKEY_PASSPHRASE")
environment_name = os.getenv("ENVIRONMENT_NAME")
destination_folder = os.getenv("DESTINATION_FOLDER")


def get_instanceIDs(environment):
    response = bs.describe_environment_resources(EnvironmentName=environment)
    temp = response['EnvironmentResources']['Instances']
    return [inst['Id'] for inst in temp]

Exemplo n.º 55
0
class LRFSClient(object):
    def __init__(self, host, port=22, user='******', progress=None):
        self.ssh = SSHClient()
        self.ssh.load_system_host_keys()
        self.ssh.set_missing_host_key_policy(AutoAddPolicy())
        self.ssh.connect(host, port, user)
        self.sftp = SFTPClient.from_transport(self.ssh.get_transport())
        self.scp = SCPClient(self.ssh.get_transport(), progress=progress)

    def get_local_files(self, games_dir):
        return sorted(os.listdir(games_dir))

    def get_remote_files(self, games_dir):
        return sorted(self.sftp.listdir(games_dir))

    def copy_from_remote_to_local(self, rom_from, rom_to):
        self.scp.get(rom_from, rom_to)

    def copy_from_local_to_remote(self, rom_from, rom_to):
        self.ssh.exec_command("rm \"{}\"".format(rom_to))
        self.scp.put(rom_from, rom_to)

    def get_local_filesize(self, path):
        return os.stat(path).st_size

    def get_remote_filesize(self, path):
        _, raw_out, _ = self.ssh.exec_command("wc -c < \"{}\"".format(path))
        return self._get_first_element(raw_out)

    def md5_is_equal(self, home_local, home_remote):
        md5_local = self._get_local_md5sum(home_local)
        md5_remote = self._get_remote_md5sum(home_remote)
        return _str(md5_local) == _str(md5_remote)

    def _get_local_md5sum(self, path):
        hash_obj = hashlib.md5()
        try:
            with open(path, "rb") as f:
                for chunk in iter(lambda: f.read(4096), b""):
                    hash_obj.update(chunk)
            return hash_obj.hexdigest()
        except IOError:
            return 0

    def _get_remote_md5sum(self, path):
        _, raw_out, _ = self.ssh.exec_command("md5sum \"{}\"".format(path))
        return str(self._get_first_element(raw_out)).split(" ", 1)[0]

    def _get_first_element(self, raw_out):
        try:
            return _str(raw_out.read()).splitlines()[0]
        except IndexError:
            return 0
Exemplo n.º 56
0
def stream_exec(ssh: paramiko.SSHClient, cmd: str):
    # https://stackoverflow.com/a/32758464/223486
    # one channel per command
    stdin, stdout, stderr = ssh.exec_command(cmd)
    # get the shared channel for stdout/stderr/stdin
    channel = stdout.channel

    # we do not need stdin.
    stdin.close()
    # indicate that we're not going to write to that channel anymore
    channel.shutdown_write()

    # read stdout/stderr in order to prevent read block hangs
    stdout_chunks = []
    content = stdout.channel.recv(len(stdout.channel.in_buffer))
    log_output_line(content.decode(), end='')
    stdout_chunks.append(content)

    stderr_chunks = []
    #content = stderr.channel.recv(len(stderr.channel.in_buffer))
    #log_output_line(content.decode(), end='')
    #stderr_chunks.append(content)

    # chunked read to prevent stalls
    while not channel.closed or channel.recv_ready(
    ) or channel.recv_stderr_ready():
        # stop if channel was closed prematurely, and there is no data in the buffers.
        got_chunk = False
        readq, _, _ = select.select([stdout.channel], [], [])
        for c in readq:
            if c.recv_ready():
                content = stdout.channel.recv(len(c.in_buffer))
                log_output_line(content.decode(), end='')
                stdout_chunks.append(content)
                got_chunk = True
            if c.recv_stderr_ready():
                # make sure to read stderr to prevent stall
                content = stderr.channel.recv_stderr(len(c.in_stderr_buffer))
                log_output_line(content.decode(), end='')
                stderr_chunks.append(content)
                got_chunk = True
        '''
        1) make sure that there are at least 2 cycles with no data in the input
           buffers in order to not exit too early (i.e. cat on a >200k file).
        2) if no data arrived in the last loop, check if we already received the exit code
        3) check if input buffers are empty
        4) exit the loop
        '''
        if (not got_chunk and stdout.channel.exit_status_ready()
                and not stderr.channel.recv_stderr_ready()
                and not stdout.channel.recv_ready()):
            # indicate that we're not going to read from this channel anymore
            stdout.channel.shutdown_read()
            # close the channel
            stdout.channel.close()
            break  # exit as remote side is finished and our bufferes are empty

    # close all the pseudofiles
    stdout.close()
    stderr.close()

    return Result(stdout.channel.recv_exit_status(), b''.join(stdout_chunks),
                  b''.join(stderr_chunks))
Exemplo n.º 57
0
# remote_path = '/home/aucsie07/aucsie01_backup/webcam/video_org/manufacture_main.avi'

# local_path = './video_org/delivery_place_delivery.avi'
# remote_path = '/home/aucsie07/aucsie01_backup/webcam/video_org/delivery_place_delivery.avi'

# local_path = './video_org/retailer_sell.avi'
# remote_path = '/home/aucsie07/aucsie01_backup/webcam/video_org/retailer_sell.avi'

local_path = './video_org/buyer_buy.avi'
remote_path = '/home/aucsie07/aucsie01_backup/webcam/video_org/buyer_buy.avi'

out = cv2.VideoWriter(local_path, fourcc, fps, frame_size_tuple)
#mp4 cannot work
# out = cv2.VideoWriter('./video_org/output_yieldy1.mp4', fourcc, 1, (640, 360))

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('192.168.1.133', 22, 'aucsie07', '1234')
scp = SCPClient(ssh.get_transport())

stage_task = [
    'adjust_brightness',
]

mqtt_service_channel = [
    "sensor_login",
    "sensor_data",
    "sensor_logout",
]

Exemplo n.º 58
0
 def connect(self):
     ssh = SSHClient()
     ssh.set_missing_host_key_policy(AutoAddPolicy())
     ssh.connect(hostname=Config.client, port=22, username=Config.un, password=Config.pw)
     self.ssh = ssh
Exemplo n.º 59
0
    def _execute(self, func, **kwargs):
        """
        Do processing after connected to sftp server

        Args:
            func (fucntion): function which processing is written.
            kwargs (dict): arguments when function is executed

        Raises:
            ValueError: argumets are empty
            IOError: failed to get data
        """

        if not func:
            raise ValueError("Function must not be empty.")

        for _ in range(self._retryTimes):
            ssh = None
            sftp = None

            try:
                ssh = SSHClient()
                ssh.set_missing_host_key_policy(AutoAddPolicy())
                if self._password:
                    ssh.connect(
                        hostname=self._host,
                        username=self._user,
                        password=self._password,
                        port=self._port,
                        timeout=self._timeout,
                    )
                else:
                    ssh.connect(
                        hostname=self._host,
                        username=self._user,
                        key_filename=self._key,
                        port=self._port,
                        timeout=self._timeout,
                    )

                sftp = ssh.open_sftp()

                # call the argument function
                ret = func(sftp=sftp, **kwargs)

                return ret

            except Exception as e:
                self._logger.warning(e)
                self._logger.warning(kwargs)

            finally:
                if sftp is not None:
                    sftp.close()
                if ssh is not None:
                    ssh.close()

            self._logger.warning(
                "Unexpected error occurred. Retry will start in 10 sec.")
            sleep(10)

        raise IOError(errno.ENOENT, "SFTP failed.")
Exemplo n.º 60
-1
def __connect_with_agent(host, port, user):
  '''
    Open an SSH connection. If setting up the connection fails with 'Error reading SSH protocol banner'
    (see inline comment in code), wait a random amount of time and retry.
  '''
  max_attempts = 10
  attempts = 0

  while attempts < max_attempts:
    try:
      client = SSHClient()
      client.set_missing_host_key_policy(AutoAddPolicy())
      client.connect(hostname=host, port=port, username=user, allow_agent=True)
      break
    except SSHException as sshe:
      # NB. If you get errors like "Error reading SSH protocol banner ... Connection reset by peer",
      # the server may be enforcing a maximum number of concurrent connections (eg. MaxStartups in OpenSSH).
      # This sleeping and retrying should only be a workaround
      # Ideally, in a bulk submission scenario, all commands would reuse a single SSH connection
      # or an SSH connection pool
      if 'Error reading SSH protocol banner' in str(sshe):
        attempts += 1
        time.sleep(random.uniform(0.1, 10))
      else:
        raise sshe

  if attempts >= max_attempts:
    raise Exception('Repeated attempts to connect to %s failed' % host)
  return client