Exemplo n.º 1
0
def pushBareConfig(veos_host, veos_ip, veos_config):
    """
    Pushes a bare config to the EOS device.
    """
    # Write config to tmp file
    deviceConfig = "/tmp/" + veos_host + ".cfg"
    with open(deviceConfig, "a") as tmpConfig:
        tmpConfig.write(veos_config)

    DEVREBOOT = False
    veos_ssh = paramiko.SSHClient()
    veos_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    veos_ssh.connect(hostname=veos_ip,
                     username="******",
                     password="",
                     port="50001")
    scp = SCPClient(veos_ssh.get_transport())
    scp.put(deviceConfig, remote_path="/mnt/flash/startup-config")
    scp.close()
    veos_ssh.exec_command('FastCli -c "{0}"'.format(cpStartRun))
    veos_ssh.exec_command('FastCli -c "{0}"'.format(cpRunStart))
    stdin, stdout, stderr = veos_ssh.exec_command(
        'FastCli -c "{0}"'.format(ztp_cmds))
    ztp_out = stdout.readlines()
    if 'Active' in ztp_out[0]:
        DEVREBOOT = True
        pS(
            "INFO",
            "Rebooting {0}...This will take a couple minutes to come back up".
            format(veos_host))
        #veos_ssh.exec_command("/sbin/reboot -f > /dev/null 2>&1 &")
        veos_ssh.exec_command('FastCli -c "{0}"'.format(ztp_cancel))
    veos_ssh.close()
    return (DEVREBOOT)
Exemplo n.º 2
0
def remote_exec_file(script, host, port, user, password, test_name, result_query, test_detail, log_file):
    start = time.asctime()
    cl = paramiko.SSHClient()
    cl.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        cc = cl.connect(host, port=port, username=user, password=password)
    except paramiko.ssh_exception.AuthenticationException:
            print "Auth Error"
    except paramiko.ssh_exception.SSHException:
            print "Protocol Error"
    except paramiko.transport:
            print "General Error"
    except socket.error:
            print "Socket Error"
    scp = SCPClient(cl.get_transport())
    scp.put(script,script)
    cl.exec_command("chmod +x ./{0}".format(script))
    stdin, stdout, stderr =  cl.exec_command("./{0}".format(script))
    a = stdout.readlines()
    cmd = str(a)
    if result_query not in cmd:
        print "Failed {0}\n".format(test_name)
        print "Test detail: \n" + test_detail + "\n"
        print "Result: \n" + cmd + "\n"
        stop = time.asctime()
        test_logger(log_file, test_name, start, stop, "Fail")
    if result_query in cmd:
        print "Pass {0}".format(test_name)
        print "Test detail: \n" + test_detail + "\n"
        print "Result: \n" + cmd + "\n"
        stop = time.asctime()
        test_logger(log_file, test_name, start, stop, "Pass")
Exemplo n.º 3
0
def sendFiles():
    # Open the file with the password located in it and prepare for use in SSH and SCP
    passwordFile = open("password.txt", "r")
    passwordSave = passwordFile.read()
    password = passwordSave.replace("\n", "")

    # initialize the SSH client and connect to the other RPi
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.connect(hostname='192.168.0.26', username="******", password=password)

    # initialize the SCP client to prepare to send files
    scp = SCPClient(ssh.get_transport())

    # prepare all the images to send from one Pi to the Other
    scp.put('images',
            recursive=True,
            remote_path='/home/pi/Documents/flaskServer/static/')

    # Once done close the SCP client
    scp.close()

    # Remove all images from one raspberry pi and recreate the images folder
    sp.call(["sudo", "rm", "-r", "images"])
    sp.call(["mkdir", "images"])

    # notify in console that the job has been completed.
    print("SSH Complete")
def job():
    #usage = get_storage_usage().replace('%', '')
    #Get current healthbot timestamp
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    print ("Healthbot timestamp {}".format(st))
    usage = 63
    print ("healthbot storage {}".format(usage))
    print ("healthbot pwd {}".format(os.getcwd()))
    if int(usage) >= 90:
        ssh = createSSHClient(details['dest']['server'], details['dest']['port'], details['dest']['user'], details['dest']['password'])
        scp = SCPClient(ssh.get_transport())
        scp.put(details['source_path'], details['dest']['path'])
        ssh.close()
        hb_status = subprocess.run(['healthbot', 'status'], stdout=subprocess.PIPE).stdout.decode('utf-8')
    
        print ("hb_status {}".format(hb_status))
        send_email(details['sender'], details['receiver'], details['sender_password'], usage, hb_status)
        print ("Mail sent")

    if datetime.datetime.now().hour == 00 and datetime.datetime.now().minute == 00: 
        print ("Healthbot storage is good and taking regular backup at 00:00")
        ssh = createSSHClient(details['dest']['server'], details['dest']['port'], details['dest']['user'], details['dest']['password'])
        scp = SCPClient(ssh.get_transport())
        scp.put(details['source_path'], details['dest']['path'])
        ssh.close()
        res = backup_influx()
Exemplo n.º 5
0
    def transfer_file(self, pull=False):
        if pull is False:
            if not self.local_file_exists():
                raise FileTransferError(
                    'Could not transfer file. Local file doesn\'t exist.')

            if not self.enough_remote_space():
                raise FileTransferError(
                    'Could not transfer file. Not enough space on device.')

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(
            hostname=self.device.host,
            username=self.device.username,
            password=self.device.password,
            port=self.port,
            allow_agent=False,
            look_for_keys=False)

        scp = SCPClient(ssh.get_transport())
        try:
            if pull:
                scp.get(self.remote, self.local)
            else:
                scp.put(self.local, self.remote)
        except Exception as e:
            print(e)
            raise FileTransferError
        finally:
            scp.close()

        return True
Exemplo n.º 6
0
class Sync:
    client = None
    scp = None
    writting = False
    sync_queue = []
    channel = None

    def __init__(self, channel):
        Logger.Logger.debug('Init sync mon created [{}]'.format(channel.host))
        logging.getLogger('paramiko').setLevel(logging.DEBUG)

        host = str(channel.host)

        self.client = SSHClient()
        self.client.load_system_host_keys()

        try:
            self.client.connect(hostname=host,
                                username=channel.user,
                                key_filename=channel.key)
            self.scp = SCPClient(self.client.get_transport())
        except Exception as e:
            Logger.Logger.debug('Exception in scp syncer {}'.format(e))

        Logger.Logger.debug('Mon created')

    def sync(self, filename):
        Logger.Logger.debug('Call sync for file {}'.format(filename))

        if self.writting:
            self.sync_queue.append(filename)
            self.sync_queue = list(set(self.sync_queue))
            return

        self.writting = True
        self.sync_queue.append(filename)
        self.sync_queue = list(set(self.sync_queue))
        self.start()
        return True

    @throttle(3)
    def start(self):
        self.writting = True
        while len(self.sync_queue) > 0:
            filename = self.sync_queue.pop(0)
            Logger.Logger.debug('Sync {} to server {}'.format(
                filename, self.channel.host))
            try:
                self.scp.put(
                    self.channel.watch_path + filename,
                    filename.replace(self.channel.watch_path,
                                     self.channel.server_path))
            except Exception as e:
                Logger.Logger.debug('EXCEPTION: {}'.format(e))

            if len(self.sync_queue) == 1:
                self.sync_queue = []

        self.writting = False
        Logger.Logger.debug('Sync done {}')
Exemplo n.º 7
0
def scp_compressed():
    """A function that uses the paramiko library to ssh into the required server
                and sends the file via Secure Copy Protocol (SCP) to /root/ftpinbox/
    """
    try:
        global filename, success
        filename = request.form['filename']

        print("Logging into SCP with compression")
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(server,
                    port=22,
                    username='******',
                    password='******',
                    compress=True)
        ssh.load_system_host_keys()
        print("Login to " + server + " Successful")

        print("Starting Transfer")
        scp_put = SCPClient(ssh.get_transport())
        scp_put.put(filename, remote_path='/root/ftpinbox/' + filename)
        print("Transfer complete")
        success = True

    except:
        print("Unable to complete using SCP-C connection")
Exemplo n.º 8
0
    def copy(self):
        """Push it !"""
        from scp import SCPClient

        ssh_connection = self.get_connection(self.destination.hostname,
                                             self.destination.port,
                                             self.destination.username)

        try:
            scp = SCPClient(ssh_connection.get_transport())
        except Exception as e:
            LOGGER.error("Failed to initiate SCPClient: " + str(e))
            ssh_connection.close()
            raise

        try:
            scp.put(self.origin, self.destination.path)
        except OSError as osex:
            if osex.errno == 2:
                LOGGER.error(
                    "No such file or directory. File not transfered: %s. Original error message: %s",
                    self.origin, str(osex))
            else:
                LOGGER.error("OSError in scp.put: " + str(osex))
                raise
        except Exception as e:
            LOGGER.error("Something went wrong with scp: " + str(e))
            LOGGER.error("Exception name {}".format(type(e).__name__))
            LOGGER.error("Exception args {}".format(e.args))
            raise
        finally:
            scp.close()
Exemplo n.º 9
0
class Sender(object):
    def __init__(self,server,username,password,dest_path,from_path):
        self.dest_path = dest_path
        self.from_path = from_path
        self.recorder = Recorder.Recorder()
        self.ssh = SSHClient()
        self.ssh.load_system_host_keys()
        self.ssh.connect(server,username=username,password=password)
        self.sftp = self.ssh.open_sftp()
        self.scp = SCPClient(self.ssh.get_transport())

    def send(self,file_path):
        if op.exists(file_path):
            file_modify_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(file_path).st_mtime))
            if not self.recorder.isSynced(file_path,file_modify_time):
                new_file_path = os.path.join(self.dest_path+'/',file_path.split(self.from_path+os.sep)[1])
                new_file_path = new_file_path.replace('\\','/')
                new_file_dir,new_file = op.split(new_file_path)
                if not rexists(self.sftp,new_file_dir):
                    rmkdir(self.sftp,new_file_dir)
                print 'uploading %s .....' % (file_path)
                self.scp.put(file_path,new_file_path)
                self.recorder.record(file_path,file_modify_time)
            else:
                pass
Exemplo n.º 10
0
 def Update(sip, S_md5, App, path):
     ssh = paramiko.SSHClient()
     key = paramiko.RSAKey.from_private_key_file(key_file)
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     for ip in sip:
         ip = ip.strip()
         Redis.lpush(Key, '-' * 60)
         Redis.lpush(Key, 'publish ' + path + ' to ' + ip + '\n')
         try:
             ssh.connect(ip, 22, username, pkey=key, timeout=15)
             cmd = "mkdir -p {0}".format(web_path)
             ssh.exec_command(cmd)
             scp = SCPClient(ssh.get_transport())
             scp.put('%s%s.zip' % (web_path, App), web_path)
             cmd = '/usr/bin/md5sum %s/%s.zip' % (web_path, App)
             stdin, stdout, stderr = ssh.exec_command(cmd)
             R_md5 = stdout.read().split()[0]
             if R_md5 == S_md5:
                 Redis.lpush(Key, '%s  md5 verify        --->pass!' % App)
                 Redis.lpush(Key, 'unzip start ......')
                 cmds = ['cd %s && /usr/bin/unzip -qo %s.zip && /bin/rm -f %s.zip' % (web_path, App, App),
                         '[ -e %s ] && echo ok' % tag_path]
                 for cmd in cmds:
                     stdin, stdout, stderr = ssh.exec_command(cmd)
                     result_zip = stdout.read().strip()
                 if result_zip == 'ok':
                     Redis.lpush(Key, '%s         --->unzip success!' % App)
                     Redis.lpush(Key, 'backup start ......')
                     cmds = ('/bin/mkdir -p %s' % bak_path,
                             '[ -e %s/%s ] && /bin/rm -rf %s/%s' % (bak_path, App, bak_path, App),
                             '[ -e %s%s ] && /bin/mv %s%s  %s/%s' % (web_path, App, web_path, App, bak_path, App))
                     for cmd in cmds:
                         stdin, stdout, stderr = ssh.exec_command(cmd)
                         result_backup = stderr.read()
                     if result_backup:
                         Redis.lpush(Key, result_backup)
                         Redis.lpush(Key, '%s         --->backup fail !' % App)
                     else:
                         Redis.lpush(Key, '%s         --->backup success!' % App)
                         Redis.lpush(Key, 'publish start ......')
                         cmd = '/bin/mv %s %s%s' % (tag_path, web_path, App)
                         stdin, stdout, stderr = ssh.exec_command(cmd)
                         result_rsync = stderr.read()
                         if result_rsync:
                             Redis.lpush(Key, result_rsync)
                             Redis.lpush(Key, '%s         --->publish fail !' % App)
                         else:
                             Redis.lpush(Key, '%s         --->publish success!' % App)
                             ssh.close()
                         Redis.lpush(Key, '-' * 60)
                 else:
                     Redis.lpush(Key, '%s         --->unzip fail !' % App)
             else:
                 Redis.lpush(Key, 'S_MD5:%r' % S_md5)
                 Redis.lpush(Key, 'R_MD5:%r' % R_md5)
                 Redis.lpush(Key, ' verify %s on %s fail !!!' % (App, ip))
         except Exception as e:
             Redis.lpush(Key, 'Update:{0}'.format(e))
             ssh.close()
             sys.exit()
Exemplo n.º 11
0
def create(tpr, cluster, shell, job_name="workdir", duration="24:00:00", nodes=1, processes=16, script=None, partition=None, ntasks_per_node=16):
    """

      - Argument validation
      - Copy from cwd to locker (on local machine)
      - Copy from local locker to remote locker
      - Submit 

    """
    if not os.path.exists(configuration.config.lockers): os.mkdir(configuration.config.lockers)
    
    # Create workdir, copy files over there
    assert not os.path.exists("workdir")
    id0 = str(uuid.uuid4())
    workdir = "%s/%s" % (configuration.config.lockers, id0)
    
    local_dir = os.getcwd()
    ignore = shutil.ignore_patterns("\#*", "workdir*", "analysis*", "test*", "trash*")
    print("local copy:", "src=", local_dir, "dst=", workdir)
    shutil.copytree(local_dir, workdir, symlinks=False, ignore=ignore)
    os.symlink(workdir, "workdir")

    # Setup SSH client to copy files over via scp
    dst = "%s/.lockers/" % (cluster.path)
    print("remote copy:", "src=", workdir, "dst=", cluster.name,":",)
    scp = SCPClient(shell.get_transport(), socket_timeout = 600)
    try:
        scp.put(workdir, dst, recursive=True)
    except SCPException, e:
        print "SCPException",e
        shutil.rmtree(workdir)
        os.remove("workdir")
        return None
Exemplo n.º 12
0
def copySCPfiles(hashed_file_name):
    try:
        ScreenOutput("Copying Test", "To Server")
        time.sleep(3)
        hashed_file_local_path1 = log_files + "/" + hashed_file_name + "_Upload"
        hashed_file_local_path2 = log_files + "/" + hashed_file_name + "_Download"
        hashed_file_remote_path1 = hashed_file_name + "_Upload"
        hashed_file_remote_path2 = hashed_file_name + "_Download"
        ssh = SSHClient()
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(scphost, port=int(scpport), username=scpuser, password=scppass)
        scp = SCPClient(ssh.get_transport())
        ScreenOutput("Copying Test", "Upload")
        time.sleep(1)
        scp.put(hashed_file_local_path1, hashed_file_remote_path1)
        ScreenOutput("Copying Test", "Download")
        time.sleep(1)
        scp.put(hashed_file_local_path2, hashed_file_remote_path2)
        scp.close()
        ScreenOutput('Test Copied', 'To Server')
        time.sleep(1)
    except Exception, e:
        ScreenOutput("Copy To", "Server Failed")
        time.sleep(3)
        ScreenOutput("Restarting", "Speed Test")
        time.sleep(2)
        executeTesting()
def main(file, key, encrypt, host, username, password):
    """
    The main function. Takes multiple parameters which are prompted if not given on the command line.
    :param file: The paht of the file to encrypt or decrypt and send.
    :param key: The key to encrypt or decrypt.
    :param encrypt: Tells if the operation is an encryption or a decryption.
    :param host: The host where to send the file.
    :param username: Username on the host.
    :param password: Password if needed. If not needed '-' should be used to tell that there is no password needed.
    """
    ssh = SSHClient()
    ssh.load_system_host_keys()
    if password != "-":
        ssh.connect(host, username=username, password=password)
    else:
        ssh.connect(host, username=username)

    scp = SCPClient(ssh.get_transport())

    if encrypt:
        print("Encrypting... ", end="")
        to_send = encrypt_file(file, key)
        print("Done.")
        print("Sending to {}...".format(host), end="")
        scp.put(to_send)
        print("Done.")
    else:
        print(decrypt_file(file, key))
Exemplo n.º 14
0
def transfer_file(module, dest):
    file_size = os.path.getsize(module.params['local_file'])

    if not enough_space(module):
        module.fail_json(
            msg='Could not transfer file. Not enough space on device.')

    hostname = module.params['host']
    username = module.params['username']
    password = module.params['password']

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostname, username=username, password=password)

    full_remote_path = '{}{}'.format(module.params['file_system'], dest)
    scp = SCPClient(ssh.get_transport())
    try:
        scp.put(module.params['local_file'], full_remote_path)
    except:
        time.sleep(10)
        temp_size = verify_remote_file_exists(
            module, dest, file_system=module.params['file_system'])
        if int(temp_size) == int(file_size):
            pass
        else:
            module.fail_json(msg='Could not transfer file. There was an error '
                             'during transfer. Please make sure remote '
                             'permissions are set.',
                             temp_size=temp_size,
                             file_size=file_size)
    scp.close()
    ssh.close()
    return True
Exemplo n.º 15
0
def send_file(local_file, remote_path, ip, username, password, logger=None):
	# Отсылает файл local_file в remote_path удаленной машины по scp
	if remote_path[len(remote_path) - 1] != '/': remote_path += '/'
	ssh = SSHClient()
	ssh.set_missing_host_key_policy(AutoAddPolicy())
	ssh.load_system_host_keys()
	if logger is not None: logger.info("SCP SEND: connecting to %s" % (ip))
	try:
		ssh.connect(ip, username=username, password=password)
	except:
		if logger is not None: logger.info("SCP SEND: failed to connect to %s" % (ip))
		return False
	else:
		if logger is not None: logger.info("SCP SEND: connected to %s" % (ip))
	try:
		if logger is not None: logger.info("SCP SEND: sending file %s" % (local_file))
		scp = SCPClient(ssh.get_transport())
		scp.put(local_file, remote_path)
	except:
		if logger is not None: logger.error("SCP SEND: error: failed to send file %s" % (local_file))
		ssh.close()
		return False
	else:
		if logger is not None: logger.info("SCP SEND: file sent to %s@%s:%s " % (username, ip, remote_path))
	ssh.close()		
	return True
Exemplo n.º 16
0
class Target:
    def __init__(self, hostname, uname, pword, num, port=22):
        self.hostname = hostname
        self.uname = uname
        self.pword = pword
        self.target_num = num
        self.port = port
        self.ssh = None
        self.is_open = False
        self.scp = None

    def conn(self):
        # print("Opening SSH connection to target...")
        self.ssh = paramiko.SSHClient()  # use ssh.exec_command("") to perform an action.
        self.ssh.load_system_host_keys()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(self.hostname, port=self.port, username=self.uname, password=self.pword)
        self.scp = SCPClient(self.ssh.get_transport())  # don't call this, but use the above function instead.
        self.is_open = True

    # TODO: fix rm -rf bug
    def scpFiles(self, filename, a, recur=True):  # call this with a filename and false if it is a single file
        print "files: "
        print (a)
        self.ssh.exec_command("rm ", a)
        self.scp.put(a, recursive=recur)

    def close(self):
        self.is_open = False
        self.ssh.close()
Exemplo n.º 17
0
 def scp2(Key, arg, wline, swline):
     try:
         ssh = paramiko.SSHClient()
         key = paramiko.RSAKey.from_private_key_file(key_file)
         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
         for ip in ips:
             ip = ip.strip()
             Redis.lpush(Key, 'sync ' + line + ' to ' + ip + '\n')
             ssh.connect(ip, 22, username, pkey=key, timeout=30)
             scp = SCPClient(ssh.get_transport())
             cmd = 'mkdir -p ' + wline
             stdin, stdout, stderr = ssh.exec_command(cmd)
             if os.path.isdir(swline):
                 scp.put(swline, wline, recursive=True)
             else:
                 scp.put(swline, swline)
             ssh.close()
             if arg == 'publish':
                 if os.path.isdir(swline):
                     dirs = create_paths(swline)
                     for path in dirs:
                         Verify(Key, ip, path)
                 else:
                     Verify(Key, ip, swline)
     except Exception as e:
         Redis.lpush(Key, 'scp2:{0} fail'.format(e))
         ssh.close()
Exemplo n.º 18
0
 def test_vdbench_run(self, test_vars):  # noqa: F811
     log = logging.getLogger("test_vdbench_run")
     node_ip = test_vars["deploy_vd_outputs"]["node_0_ip_address"]["value"]
     with SSHTunnelForwarder(
             test_vars["public_ip"],
             ssh_username=test_vars["controller_user"],
             ssh_pkey=test_vars["ssh_priv_key"],
             remote_bind_address=(node_ip, 22),
     ) as ssh_tunnel:
         sleep(1)
         try:
             ssh_client = create_ssh_client(
                 test_vars["controller_user"],
                 "127.0.0.1",
                 ssh_tunnel.local_bind_port,
                 key_filename=test_vars["ssh_priv_key"])
             scp_client = SCPClient(ssh_client.get_transport())
             try:
                 scp_client.put(test_vars["ssh_priv_key"], r"~/.ssh/id_rsa")
             finally:
                 scp_client.close()
             commands = """
                 ~/copy_idrsa.sh
                 cd
                 ./run_vdbench.sh inmem32node3.conf uniquestring1
                 """.split("\n")
             run_ssh_commands(ssh_client, commands)
         finally:
             ssh_client.close()
Exemplo n.º 19
0
    def scp(self, local_path, remote_path, get=False, timeout=10):
        """Copy files from local_path to remote_path or vice versa.

        connect() method has to be called first!

        :param local_path: Path to local file that should be uploaded; or
        path where to save remote file.
        :param remote_path: Remote path where to place uploaded file; or
        path to remote file which should be downloaded.
        :param get: scp operation to perform. Default is put.
        :param timeout: Timeout value in seconds.
        :type local_path: str
        :type remote_path: str
        :type get: bool
        :type timeout: int
        """
        if not get:
            logger.trace('SCP {0} to {1}:{2}'.format(
                local_path,
                self._ssh.get_transport().getpeername(), remote_path))
        else:
            logger.trace('SCP {0}:{1} to {2}'.format(
                self._ssh.get_transport().getpeername(), remote_path,
                local_path))
        # SCPCLient takes a paramiko transport as its only argument
        scp = SCPClient(self._ssh.get_transport(), socket_timeout=timeout)
        start = time()
        if not get:
            scp.put(local_path, remote_path)
        else:
            scp.get(remote_path, local_path)
        scp.close()
        end = time()
        logger.trace('SCP took {0} seconds'.format(end - start))
Exemplo n.º 20
0
 def scp2(Key,arg,wline,swline):
     try:
         ssh = paramiko.SSHClient()
         key = paramiko.RSAKey.from_private_key_file(key_file)
         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
         for ip in ips:
             ip = ip.strip()
             Redis.lpush(Key,'sync '+line+' to '+ip+'\n')
             ssh.connect(ip,22,username,pkey=key,timeout=30)
             scp = SCPClient(ssh.get_transport())
             cmd ='mkdir -p '+wline
             stdin, stdout, stderr = ssh.exec_command(cmd)
             if os.path.isdir(swline):
                 scp.put(swline,wline,recursive=True)
             else:
                 scp.put(swline,swline)
             ssh.close()
             if arg == 'publish':
                 if os.path.isdir(swline):
                     dirs = create_paths(swline)
                     for path in dirs:
                         Verify(Key,ip,path)
                 else:
                     Verify(Key,ip,swline)
     except Exception as e:
         Redis.lpush(Key,'scp2:{0}'.format(e))
         ssh.close()
Exemplo n.º 21
0
def retry_upload_files(ssh_session, file_name=FAILED_UPLOAD_FILE):
    '''
    if files are not uploaded sucessfull, they will be reupload here.
    The list of failed uploaded files are in FAILED_UPLOAD_FILE
    :param scp: scp object from paramiko transport
    :param file_name: the file contains the list of all failed uploading files
    :return:
    '''
    # if that file does not exist. Nice! get out immediately
    if not Path(FAILED_UPLOAD_FILE).is_file():
        return
    sshSession = getSSHSession(remote)
    scp = SCPClient(sshSession.get_transport(), progress=__progress)
    all_files = open(file_name)
    # remove the \n line
    all_files = [line for line in all_files if len(line) > 1]
    logger.info("RE-Uploading file to server %s ", str(all_files))
    for line in all_files:
        src_dest_pair = line.split('@@')
        dest_folder = src_dest_pair[-1].strip()
        source_file = src_dest_pair[0].strip()
        try:
            scp.put(source_file, remote_path=dest_folder)
            logger.debug('uploading %s to %s', source_file, dest_folder)
        except SCPException as e:
            logger.debug('Cannot upload %s to %s', source_file, dest_folder)
            logger.debug(str(e))
    scp.close()
def pushtestfiles(target):
    ssh = paramiko.SSHClient()
    # ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # ssh.connect(remoteip, username="******", password = "******")
    privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
    mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
    ssh.connect(hostname="192.168.1.225", username="******", pkey=mykey)

    scp = SCPClient(ssh.get_transport())
    scp.put('diskspeed2.py')
    scp.close()

    # host="raspberrypi.local"
    # user="******"
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
    # client.load_system_host_keys()
    mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
    client.connect(hostname="192.168.1.225", username="******", pkey=mykey)
    # client.connect(host, username=user)
    stdin, stdout, stderr = client.exec_command('python diskspeed2.py')
    pwd = stdout.read().splitlines()
    for elem in pwd:
        print(elem)
    scp.close()
    return None
Exemplo n.º 23
0
class FileManager:
    def __init__(self, host="10.4.20.69", port=22, user_name="", passwd=""):
        self.host = host
        self.port = port
        self.user = user_name
        self.passwd = passwd
        self.ssh_client = paramiko.SSHClient()
        self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        self.ssh_client.connect(host, port, user_name, passwd)
        self.scpclient = SCPClient(self.ssh_client.get_transport(),
                                   socket_timeout=15.0)

    def upload_file(self, local_path, remote_path):
        try:
            self.scpclient.put(local_path, remote_path)
        except FileNotFoundError as e:
            logger.error("Can't find local file at: " + local_path)
            logger.error(e)
        else:
            logger.info("Successful upload 1 file! ")

    def get_file(self, remote_path, local_path):
        try:
            self.scpclient.get(remote_path, local_path)
        except FileNotFoundError as e:
            logger.error("Can't find remote file at: " + remote_path)
            logger.error(e)
        else:
            logger.info("Successful download 1 file! ")

    def close(self):
        self.ssh_client.close()
Exemplo n.º 24
0
def copy_python(ssh_client, ip, date):
    if update_shell_flag == 1:
        rm_cmd = 'rm -rf %s/%s' % (scp_dir, old_shell_name)
        stdin, stdout, stderr = ssh_client.exec_command(rm_cmd)
        out_info = stdout.readlines()
    scp_client = SCPClient(ssh_client.get_transport(), socket_timeout=15)
    re_try = 5
    while re_try > 0:
        stdin, stdout, stderr = ssh_client.exec_command('ls %s' % (scp_dir))
        res = stdout.readlines()
        if 'pirate_arm.py\n' in res:
            stdin, stdout, stderr = ssh_client.exec_command(
                'cat %s/%s |grep "%s"' % (scp_dir, shell_name, shell_date))
            res = stdout.readlines()
            if len(res) == 1 and res[0].find('No such file or directory') == (
                    -1):
                scp_client.close()
                return True
            else:
                scp_client.put(shell_name, scp_dir)
                print(ip + ' Instead Python done')
                time.sleep(1)
        else:
            scp_client.put(shell_name, scp_dir)
            print(ip + ' New Dev,Copy Python done')
            time.sleep(1)
    scp_client.close()
    return False
Exemplo n.º 25
0
    def transfer_file(self, pull=False):
        if pull is False:
            if not self.local_file_exists():
                raise FileTransferError(
                    'Could not transfer file. Local file doesn\'t exist.')

            if not self.enough_remote_space():
                raise FileTransferError(
                    'Could not transfer file. Not enough space on device.')

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=self.device.host,
                    username=self.device.username,
                    password=self.device.password,
                    port=self.port,
                    allow_agent=False,
                    look_for_keys=False)

        scp = SCPClient(ssh.get_transport(), socket_timeout=30.0)
        try:
            if pull:
                scp.get(self.remote, self.local)
            else:
                scp.put(self.local, self.remote)
        except Exception as e:
            raise FileTransferError
        finally:
            scp.close()

        return True
Exemplo n.º 26
0
class HaWebHook():
    def __init__(self,
                 webhook_url,
                 ssh_host=None,
                 ssh_username=None,
                 target_dir=None):
        self.webhook_url = webhook_url
        self.ssh_host = ssh_host
        if self.ssh_host:
            ssh = SSHClient()
            ssh.load_system_host_keys()
            ssh.connect(ssh_host, username=ssh_username)
            self.scp = SCPClient(ssh.get_transport())
            self.target_dir = target_dir

    @retry(wait_fixed=2000, stop_max_delay=60000)
    def send(self, label, img_path):
        log.info(str(label))
        if self.ssh_host:
            log.info("scp %s to %s on %s" %
                     (img_path, self.target_dir, self.ssh_host))
            self.scp.put(img_path, self.target_dir)
        url = self.webhook_url.format(quote_plus(label),
                                      os.path.basename(img_path))
        requests.post(url)
Exemplo n.º 27
0
def drop(ssh, localFile, remoteLocation):
    scp = SCPClient(ssh.get_transport())

    # Drop list of files
    if (isinstance(localFile, list) and isinstance(remoteLocation, list)):
        localFileList = localFile
        remoteLocList = remoteLocation
        for lfile, rloc in zip(localFileList, remoteLocList):
            try:
                scp.put(lfile, rloc)
            except Exception as e:
                print("[-] ", str(e))

            print_green("[+] [IP] - " + ssh.get_transport().getpeername()[0] +
                        " | Successfully dropped: " + lfile + " | Dest: " +
                        rloc)

    # Drop single file
    else:
        try:
            scp.put(localFile, remoteLocation)
        except Exception as e:
            print("[-] ", str(e))

        print_green("[+] [IP] - " + ssh.get_transport().getpeername()[0] +
                    " | Successfully dropped: " + localFile + " | Dest: " +
                    remoteLocation)
Exemplo n.º 28
0
    def AtualizaFw(self):
        try:
            ssh = SSHClient()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            ssh.connect(self.ip, username=self.usuario, password=self.senha)
            scp = SCPClient(ssh.get_transport())

            scp.put(self.fw, '/tmp/fwupdate.bin')
            stdin, stdout, stderr = ssh.exec_command('/sbin/fwupdate -m\n')
            print("Firmware atualizado - %s" % self.ip)
            ssh.close()

        except ConnectionResetError:
            ssh.close()
        except ConnectionRefusedError:
            ssh.close()
        except OSError:
            ssh.close()
        except ssh_exception.AuthenticationException:
            ssh.close()
        except ssh_exception.SSHException:
            ssh.close()
        except AttributeError:
            ssh.close()
        except Exception:
            ssh.close()
        except:
            ssh.close()
Exemplo n.º 29
0
class WebCam:
    def __init__(self, cam_num, ssh):
        self.cam_num = cam_num
        self.last_frame = np.zeros((1,1))
        self.ssh = ssh
        self.scp = SCPClient(self.ssh.get_transport(), socket_timeout=None)

    def initialize(self):
        self.cap = cv2.VideoCapture(self.cam_num)
        self.frame_width = int(self.cap.get(3))
        self.frame_height = int(self.cap.get(4))

    def getSingleCap(self):
        ret, self.last_frame = self.cap.read()
        return self.last_frame

    def getMultiFrames(self, frames, name):
        VID_NAME = '{}.mp4'.format(name)
        vid_out = cv2.VideoWriter(VID_FOLDER + VID_NAME, cv2.VideoWriter_fourcc(*'MP4V'), 10, (self.frame_width, self.frame_height))
        for i in range(frames):
            frame = self.getSingleCap()
            vid_out.write(frame)
        vid_out.release()
        self.videoTransfer(VID_FOLDER + VID_NAME, VID_NAME)

    def videoTransfer(self, filename, VID_NAME):
        try:
            self.scp.put(filename, REMOTE_VID_FOLDER + VID_NAME)
        except SCPException:
            raise IOError("Remote directory not available")

    def close_camera(self):
        self.cap.release()
        return self.ssh
Exemplo n.º 30
0
def task(zeros, ranges, margin, public_ip):
    key = paramiko.RSAKey.from_private_key_file("")  #pls put key file location
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ec2 = boto3.resource('ec2')
    try:
        client.connect(hostname=public_ip, username="******", pkey=key)
        scp = SCPClient(client.get_transport())
        scp.put('Task_T.py', recursive=True, remote_path='/home/ubuntu')
        stdin, stdout, stderr = client.exec_command('python3 Task_T.py' + ' ' +
                                                    str(zeros) + ' ' +
                                                    str(ranges) + ' ' +
                                                    str(margin))

        data = stdout.read().splitlines()
        for line in data:
            if (line.decode()):
                print(line.decode())
                client.close()
                ec2.instances.filter(Filters=[{
                    'Name': 'instance-state-name',
                    'Values': ['running']
                }]).terminate()
    except Exception as e:
        print(e)
Exemplo n.º 31
0
    def upload_test(self, filenames, recursive, expected=[]):
        destination = b'/tmp/upp\xC3\xA9' + next(unique_names)
        chan = self.ssh.get_transport().open_session()
        chan.exec_command(b'mkdir ' + destination)
        assert chan.recv_exit_status() == 0
        previous = os.getcwd()
        try:
            os.chdir(self._temp)
            scp = SCPClient(self.ssh.get_transport())
            scp.put(filenames, destination, recursive)

            chan = self.ssh.get_transport().open_session()
            chan.exec_command(
                b'echo -ne "' +
                destination.decode('iso-8859-1')
                    .encode('ascii', 'backslashreplace') +
                b'" | xargs find')
            out_list = b''
            while True:
                data = chan.recv(1024)
                if not data:
                    break
                out_list += data
            prefix = len(destination) + 1
            out_list = [l[prefix:] for l in out_list.splitlines()
                        if len(l) > prefix]
            self.assertEqual(normalize_paths(out_list), set(expected))
        finally:
            os.chdir(previous)
            chan = self.ssh.get_transport().open_session()
            chan.exec_command(b'rm -Rf ' + destination)
            assert chan.recv_exit_status() == 0
Exemplo n.º 32
0
    def put_file_to_scp_service(self,
                                file_name,
                                remote_path="/A8/",
                                local_path="D:\python_eg"):
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        ssh_client.connect(self.__host, self.__port, self.__username,
                           self.__password)
        scpclient = SCPClient(ssh_client.get_transport(), socket_timeout=15.0)
        # local_path = file_path + "\\" + img_name
        file_path_lo = local_path + '/' + file_name
        file_path_re = remote_path

        print(file_path_lo)
        print(file_path_re)
        try:
            scpclient.put(file_path_lo, file_path_re)  # 上传到服务器指定文件
            #scpclient.get(file_path_re, file_path_lo)  # 从服务器中获取文件
        except FileNotFoundError as e:
            print(e)
            print("system could not find the specified file" + local_path)
            result = "system could not find the specified file" + local_path
        else:
            print("文件上传成功")
            result = "File uploaded successfully"
        ssh_client.close()
        return result
Exemplo n.º 33
0
    def push(self, src_path, dest_path, push_timeout=None):
        """
        Push file on dut

        :param src_path: str
        :param src_path: Source path of file

        :type dest_path: str
        :param dest_path: Destination path on DUT

        :type push_timeout: int
        :param push_timeout: timeout to push the file

        :rtype: tuple
        :return: Boolean indicating operation succeed or not, message
        """
        verdict = False
        msg = "fail to push file %s onto %s, no opened connection" % (src_path, dest_path)
        if self.__protocol is not None:
            try:
                scp_client = SCPClient(self.__protocol.get_transport())
                scp_client.put(src_path, dest_path)
                verdict = True
                msg = "Pushed file %s onto %s" % (src_path, dest_path)
            except Exception as ex:
                msg = "Error happen when pushing file %s onto %s :%s" % (src_path, dest_path, str(ex))
        return verdict, msg
Exemplo n.º 34
0
def scp_file(ip, uname, pswd, image):
	ssh = paramiko.SSHClient()
	ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
	ssh.load_system_host_keys()
	ssh.connect(ip, username=uname, password=pswd)
	scp = SCPClient(ssh.get_transport())
	scp.put(image, '/tmp/flashimg')
def backup_influx():
    db_user = details['influxDB']['username']
    db_pwd = details['influxDB']['password']
    db_details = details['influxDB']['database']
    dbs = db_details.keys()

    print ("dbs: {}".format(dbs))
    for db in dbs:
        print ("db: {}".format(db))
        for msrmt in db_details[db]:
            print ("measurement {}".format(msrmt))
            csv_file = get_filename(db, msrmt)
            cli = "influx -username {} -password \"{}\" -database '{}' -host 'localhost' -execute 'SELECT * FROM \"{}\".\"{}\".\"{}\" where time > now() -1d order by time desc' -format 'csv' > {}".format(db_user, db_pwd, db, db, db, msrmt, csv_file)
            print ("influx cli {}".format(cli))       
            #exec_influx = subprocess.run([cli], stdout=subprocess.PIPE).stdout.decode('utf-8')
            exec_influx = os.system(cli)
            
            #scp_cli = 'scp {} -P 13022 [email protected]:/root/hb_backup/.'.format(csv_file) 
            ssh = createSSHClient(details['dest']['server'], details['dest']['port'], details['dest']['user'], details['dest']['password'])
            scp = SCPClient(ssh.get_transport())
            scp.put('/home/juniper/'+str(csv_file), details['dest']['path'])
            ssh.close()
            os.remove(csv_file)

    return "Success"
Exemplo n.º 36
0
 def set_sshcfg(self, **kw):
     node = kw['node']
     nodeinfo = kw['nodeinfo']
     tmp_remote_dir = "/tmp/%s/.ssh/" % nodeinfo['username']
     try:
         ssh_client = paramiko.SSHClient()
         ssh_client.set_missing_host_key_policy(
             paramiko.MissingHostKeyPolicy())
         ssh_client.connect(nodeinfo['bmcip'],
                            username=nodeinfo['username'],
                            password=nodeinfo['password'])
     except (NoValidConnectionsError) as e:
         return self.callback.error(
             "Unable to connect to bmc %s" % nodeinfo['bmcip'], node)
     if not ssh_client.get_transport().is_active():
         return self.callback.error(
             "SSH session to bmc %s is not active" % nodeinfo['bmcip'],
             node)
     if not ssh_client.get_transport().is_authenticated():
         return self.callback.error(
             "SSH session to bmc %s is not authenticated" %
             nodeinfo['bmcip'], node)
     ssh_client.exec_command("/bin/mkdir -p %s\n" % tmp_remote_dir)
     scp = SCPClient(ssh_client.get_transport())
     scp.put(self.copy_sh_file, tmp_remote_dir + "copy.sh")
     scp.put(self.local_public_key, tmp_remote_dir + "id_rsa.pub")
     ssh_client.exec_command("%s/copy.sh %s" %
                             (tmp_remote_dir, nodeinfo['username']))
     ssh_client.close()
     return self.callback.info("ssh keys copied to %s" % nodeinfo['bmcip'])
Exemplo n.º 37
0
def connect_clients(hostname, username, password, cmd):
    try:
        date_now = time.strftime('%Y_%m_%d_%H_%M_%S',
                                 time.localtime(time.time()))
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname=hostname, username=username, password=password)

        if upload_flag != 0 or download_flag != 0:
            scpclient = SCPClient(client.get_transport(), socket_timeout=15.0)
            print(cmd)
            if upload_flag == 1:
                scpclient.put(cmd, remote_path)
            elif download_flag == 1:
                scpclient.get(cmd, os.path.abspath("."))
        else:
            shell = client.invoke_shell()
            shell.sendall(cmd + "\n")
            shell.sendall("exit\n")

    except Exception as e:
        print("[%s] %s target failed, the reason is %s" %
              (date_now, hostname, str(e)))
    else:
        print("[%s] %s target success" % (date_now, hostname))
    finally:
        client.close()
Exemplo n.º 38
0
 def copy_to_server(self, in_folder, out_folder):
     self.ssh_client.connect(hostname=ADDR, username=USR_NAME, password=PWD)
     print('connecting to on_prem_server using ssh')
     stdin, stdout, stderr = self.ssh_client.exec_command('ls')
     print(stdout.readlines())
     scp_client = SCPClient(self.ssh_client.get_transport())
     scp_client.put(in_folder, recursive=True, remote_path=out_folder)
Exemplo n.º 39
0
    def transfer(self, evo_name):
        #compress

        #shutil.make_archive(self.temp_zip_dir+evo_name, 'zip', '../../../PymoNNto_Dev')
        zipDir('../../../PymoNNto_Dev/', self.temp_zip_dir + evo_name + '.zip',
               [
                   '.git', '.idea', '\\StorageManager\\', '\\NetworkStates\\',
                   '\\Evo\\', '\\__pycache__\\', '\\midis\\'
               ])

        #transfer
        #self.exec_cmd(self.get_transfer_cmd(evo_name))

        ssh = self.get_ssh_connection()
        scp = SCPClient(ssh.get_transport())
        src = self.temp_zip_dir + evo_name + '.zip'
        dst = self.main_path + evo_name + '.zip'
        scp.put(src, dst)

        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
            self.get_extract_cmd(evo_name))
        ssh_stdout.channel.recv_exit_status()

        #extract
        #self.exec_cmd(self.get_extract_cmd(evo_name))
        os.remove(self.temp_zip_dir + evo_name + '.zip')

        ssh.close()
        scp.close()
Exemplo n.º 40
0
    def transfer_file(self, dest):
        """Begin to transfer file by scp"""

        if not self.local_file_exists():
            self.module.fail_json(
                msg='Could not transfer file. Local file doesn\'t exist.')

        if not self.enough_spajctanner.network_cloudengine.ce():
            self.module.fail_json(
                msg='Could not transfer file. Not enough spajctanner.network_cloudengine.ce on devijctanner.network_cloudengine.ce.')

        hostname = self.module.params['provider']['host']
        username = self.module.params['provider']['username']
        password = self.module.params['provider']['password']
        port = self.module.params['provider']['port']

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=hostname, username=username, password=password, port=port)
        full_remote_path = '{0}{1}'.format(self.file_system, dest)
        scp = SCPClient(ssh.get_transport())
        try:
            scp.put(self.local_file, full_remote_path)
        exjctanner.network_cloudengine.cept Exjctanner.network_cloudengine.ception:
            time.sleep(10)
            file_exists, temp_size = self.remote_file_exists(
                dest, self.file_system)
            file_size = os.path.getsize(self.local_file)
            if file_exists and int(temp_size) == int(file_size):
                pass
            else:
                scp.close()
                self.module.fail_json(msg='Could not transfer file. There was an error '
                                      'during transfer. Please make sure the format of '
                                      'input parameters is right.')
Exemplo n.º 41
0
def copy_file(local_file_path, dest_path, host=cfg.HOST_CONF['HOST'], port=cfg.HOST_CONF['PORT'], user=cfg.HOST_CONF['USER'], password=cfg.HOST_CONF['PASSWORD'], mode = 'PUT'):

        transport = None
        if not path.exists(local_file_path):
                if mode is 'GET':
                        mkdir(local_file_path)
                else:
		        print "Local file path doesnt exist %s" %(local_file_path) 
	
        paramiko.util.log_to_file(cfg.PARAMIKO_LOG)
        transport = paramiko.Transport((host, port))
        try:
               transport.connect(username=user, password=password)
               transport.get_exception()
               scp = SCPClient(transport)
        except:
               print "Error while connecting to host %s:%s" %(host, port)
        #scp.put('test_eth.sh', 'test_eth_1.sh')
        #scp.get('test_eth_1.sh')
        if mode is 'PUT':
                #print "copying file from %s to %s on host %s " %(local_file_path, dest_path, host)
                scp.put(local_file_path, dest_path)
        else:
                #print scp.get(dest_path,local_file_path, recursive=True)
                scp.get(dest_path, recursive=True)
                #print scp.get(dest_path, local_path="path_to_directory where store this directory", recursive= True)
                
        scp.close()
Exemplo n.º 42
0
def put_magic_wllt(ssh, passphrase):
    print("LOCAL_WALLET_PATH", LOCAL_WALLET_PATH)

    # Check if local wallet exists
    if path.exists(LOCAL_WALLET_PATH): # replace with get_wallet_db()
        command = "gpg -c --batch --no-symkey-cache --compress-algo none --passphrase "+ passphrase + " " + REMOTE_WALLET_PATH + "/" + WALLET_NAME

        try:
            scp = SCPClient(ssh.get_transport(), progress4=progress4)
            scp.put(f"{get_wallet_db()}", REMOTE_WALLET_PATH)

            # Encrypt it
            #stdin, stdout, stderr = ssh.exec_command(command)
            #lines = stdout.readlines()
            #if lines:
            #    print('Lines:',lines)
            
            result = "Copied local wallet to device\n"

        except Exception as e:
            print("Couldn\'t connect, connection error:", e)
            result = "Failed to copy wallet to device"
    else:
        result = "Couldn\'t find local wallet.db file, exiting..."        
    scp.close()    
    ssh.close()
    print(result)
    return result
Exemplo n.º 43
0
 def put_file(self,mac_addr,function_name):
     scp = None
     file_path = self._lookup_function_files(function_name)
     print file_path
     if(os.path.exists(file_path)):
         if(os.path.isdir(file_path)):
             if(self.scp_clients.has_key(mac_addr)):
                 ssh = self.scp_clients[mac_addr]
                 scp = SCPClient(ssh.get_transport())
                 print scp
                 print file_path
                 try:
                     scp.put(file_path,recursive=True)
                 except:
                     print "SCPException"
             #files = os.listdir(file_path)
             ## assumes no directory in the element dir
             #for item in files:
             #    print item
             #    scp.put(file_path+'/'+item) #Not portable use os.path.join
         if(os.path.isfile(file_path)):
             if(self.scp_client.has_key(mac_addr)):
                 scp = self.scp_clients[mac_addr]
             scp.put(file_path)
         pass
     return True
Exemplo n.º 44
0
    def put(self, files, remote_path=None, out_stream=sys.stdout, verbose=False):
        """
        Copy a file from the local system to the remote system.

        :param files:
        :param remote_path:
        :param out_stream:
        :param verbose:
        :return: :rtype:
        """
        if remote_path is None:
            remote_path = files
        self.display("scp '{src}' '{dest}'".format(src=files, dest=remote_path),
                     out_stream=out_stream, verbose=verbose)
        ssh = SSHClient()
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(Project.address, Project.port, Project.user, Project.password)
        scp = SCPClient(ssh.get_transport())
        # scp = SCPClient(self.ssh.get_transport())
        # noinspection PyBroadException
        try:
            info("\nfiles: %s" % repr(files))
            info("remote_path: %s" % remote_path)
            output = scp.put(files, '"{dest}"'.format(dest=remote_path), recursive=True) or ''
        except Exception:
            try:
                output = scp.put(files, remote_path, recursive=True) or ''
            except Exception as ex:
                output = str(ex)
        self.display("\n" + output, out_stream=out_stream, verbose=verbose)
        return output
Exemplo n.º 45
0
    def push_config(self, client, restart=False):
        self.write_config()

        scp = SCPClient(client.get_transport())
        scp.put(str(self._local_config_path), str(self._remote_config_path))
        if restart:
            client.exec_command(self.RESTART_COMMAND)
            time.sleep(10)
Exemplo n.º 46
0
 def ssh_scp(path):
     ssh = _init_ssh()
     ha_path = app.config.get('HAPROXY_PATH')
     ha_cmd = "/usr/sbin/service haproxy restart"
     scp = SCPClient(ssh.get_transport())
     scp.put(path,ha_path)
     stdin, stdout, stderr = ssh.exec_command(ha_cmd)
     return stderr.read()
Exemplo n.º 47
0
def upload_iso(iso_path, storage_name):
    host_ip = get_mgmt_ip_by_storagename(storage_name)
    ssh = createSSHClient(host_ip, 22, "root", "arbre1234")
    scp = SCPClient(ssh.get_transport())
    if os.path.isfile(iso_path):
        scp.put(iso_path)
    else:
        return False
Exemplo n.º 48
0
    def run(self):
        try:
            # run a command and wait for it to finish
            def run(command):
                _, stdout, _ = ssh.exec_command(command)
                stdout.channel.recv_exit_status()

            # send the webapp a textual progress update
            listener = Listener()
            def log(text):
                listener.publish(('project', self.project.name, 'deploy', 'status'), { 'text': text })

            # prevent accidentally deploying on the server itself
            # (this doesn't prevent deploying to the server's public
            # IP address, so there's still a security problem here)
            if self.host in ['127.0.0.1', '0.0.0.0']:
                raise Exception('you\'re trying to deploy to the server!')

            # create temporary build file
            path = os.path.join(tempfile.mkdtemp(), 'deploy.launch')
            xml = roslaunch_xml_for_file(self.project.project_file_path, self.remote_deploy_path)
            open(path, 'w').write(xml)
            log('Created file: deploy.launch')

            # create a ssh session
            log('Connecting to %s@%s...' % (self.user, self.host))
            ssh = SSHClient()
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            ssh.connect(self.host, username=self.user, password=self.password)
            log('Connected to remote machine')

            # clean the deploy location
            run('rm -fr %s' % self.remote_deploy_path)
            run('mkdir -p %s' % self.remote_deploy_path)
            log('Cleaned deploy location')

            # copy the launch file over
            scp = SCPClient(ssh.get_transport())
            scp.put(path, remote_path=self.remote_deploy_path)
            log('Copied file: deploy.launch')

            # run the file
            command = 'cd %s && roslaunch deploy.launch' % self.remote_deploy_path
            channel = ssh.invoke_shell()
            channel.send(command + '\nexit\n')
            log('Ran command: roslaunch deploy.launch')

            # wait for the command to finish
            import time
            while not self.die:
                time.sleep(0.1)
        except:
            import traceback
            log('Exception:\n' + traceback.format_exc())

        ssh.close()
        log('Connection was closed')
Exemplo n.º 49
0
    def transfer_file(self, hostname=None, username=None, password=None, pull=False):
        """Transfer the file to the remote device over SCP.

        Note:
            If any arguments are omitted, the corresponding attributes
            of ``self.device`` will be used.

        Args:
            hostname (str): OPTIONAL - The name or
                IP address of the remote device.
            username (str): OPTIONAL - The SSH username
                for the remote device.
            password (str): OPTIONAL - The SSH password
                for the remote device.

        Returns:
            True if successful.

        Raises:
            FileTransferError: if the transfer isn't successful.
        """
        if pull is False:
            if not self.local_file_exists():
                raise FileTransferError(
                    'Could not transfer file. Local file doesn\'t exist.')

            if not self.enough_space():
                raise FileTransferError(
                    'Could not transfer file. Not enough space on device.')

        hostname = hostname or self.device.host
        username = username or self.device.username
        password = password or self.device.password

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(
            hostname=hostname,
            username=username,
            password=password,
            port=self.port,
            allow_agent=False,
            look_for_keys=False)

        full_remote_path = '{}{}'.format(self.file_system, self.dst)
        scp = SCPClient(ssh.get_transport())
        try:
            if pull:
                scp.get(full_remote_path, self.src)
            else:
                scp.put(self.src, full_remote_path)
        except:
            raise FileTransferError(
                'Could not transfer file. There was an error during transfer. Please make sure remote permissions are set.')
        finally:
            scp.close()

        return True
def addMacs(user, passwd, ip):
    '''Connects to DD-WRT router with ip address ip over ssh. Then SCPs the
    maclist into the DD-WRT /root/ directory. Feeds this file into a Shell-
    Variable, then updates the mac filterlist and commits the config.
    
    Input:      user: username, string
                passwd: password, string
                ip:    ip address to connect to, string
    
    Output: void'''
    
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(AllowAllKeys())
    
    try:
        statusChangeOneIP(ip, bcolors.OKBLUE + 'connecting' + bcolors.ENDC)
        client.connect(ip, username=user, password=passwd)
    except BadHostKeyException:
        statusChangeOneIP(ip, bcolors.FAIL + 'Host Key Exception' +\
                            bcolors.ENDC)
        client.close()
        return
    except AuthenticationException:
        user = raw_input('Authentication Exception, username: '******'Password: '******'SSH Exception' + bcolors.ENDC)
        client.close()
        return
    except socket_error:
        statusChangeOneIP(ip, bcolors.FAIL + 'cannot reach device' +\
                            bcolors.ENDC)
        client.close()
        return
        
    statusChangeOneIP(ip, bcolors.OKBLUE + 'connected' + bcolors.ENDC)
    try:
        scp = SCPClient(client.get_transport())
        
        '''Copy the parse mac addresses over to /root/, wait a second to make
        sure transfer is complete, then set the _maclist variables on the router
        and commit changes'''
        
        scp.put(installpath + '/macsParsed.txt', 'macs.txt')
        time.sleep(1)
        client.exec_command('nvram set ath0_maclist="$(cat macs.txt)" && '\
                    'nvram set wl0_maclist="$(cat macs.txt)" && nvram commit')
        
        statusChangeOneIP(ip, bcolors.OKGREEN + 'changes committed' +\
                            bcolors.ENDC)
        #print('Changes commited')
    except SSHException:
        statusChangeOneIP(ip, bcolors.FAIL + 'SSH Exception' + bcolors.ENDC)
        client.close()
        return
    client.close()
Exemplo n.º 51
0
 def put_file(self, source, destination):
     assert os.path.isfile(source)
     try:
         scp = SCPClient(self.__ssh.get_transport())
         scp.put(source, destination)
     except Exception as err:
         print err
         return {'status': 'KO'}
     return {'status': 'OK'}
Exemplo n.º 52
0
 def scp(self, filename, remote_path):
     config = SSHConfig()
     config.parse(open(os.path.expanduser('~/.ssh/config')))
     o = config.lookup('geodata')
     ssh_client = SSHClient()
     ssh_client.load_system_host_keys()
     ssh_client.connect(o['hostname'], username=o['user'])
     scp = SCPClient(ssh_client.get_transport())
     scp.put(filename, remote_path=remote_path)
Exemplo n.º 53
0
Arquivo: remote.py Projeto: tongqg/mcm
def copy(hostname, username, filepath, remotefile):
	ssh = SSHClient()
	# ssh.load_system_host_keys()
	ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
	ssh.connect(hostname, username=username)
	ssh.exec_command("mkdir -p " + remotefile[0:remotefile.rindex('/')])
	# SCPCLient takes a paramiko get_transportt as its only argument
	scp = SCPClient(ssh.get_transport())
	scp.put(filepath, remotefile)
Exemplo n.º 54
0
    def parseBytesFromIface(self, sense, hnbIndex):
        logger.info('getting time and bytes for sense: %s -- hnb: %s' % (sense, hnbIndex))
        bytesTimeConfig = ConfigParser.ConfigParser()
        bytesTimeConfFile = open('/var/tmp/bytes-time-%s-%d.conf' % (sense, hnbIndex), 'w')
        bytesTimeConfig.add_section('snapshot')
        ifbIfaces = []
        htbQueues = []
        edge = config.get('rol', 'edgeType')
        if  edge == 'UL':
            htbQueues = json.loads(config.get('hnbs', 'ulHtbQueues'))
            ifbIfaces = json.loads(config.get('hnbs', 'ulIfbIfaces'))
        else:
            htbQueues = json.loads(config.get('hnbs', 'dlHtbQueues'))
            ifbIfaces = json.loads(config.get('hnbs', 'dlIfbIfaces'))
        logger.info('htbQueues: %s -- ifbIfaces: %s' % (htbQueues, ifbIfaces))

        timeStamps = []
        ifaceBytes = []
        Pos = 0
        for hnbIter, hnb in enumerate(ifbIfaces):
            timeStampsIface = []
            ifaceBytesIface = []
            for ifaceIndex, iface in enumerate(hnb):
                output = subprocess.check_output('tc -s -d class show dev %s' % iface, shell=True)
                timeStamp = time.time()
                logger.info('Timestamp from iface %s: %f seconds' % (iface, timeStamp))
                queue = ''
                for row in output.split('\n'):
                    if queue != '':
                        ifaceByte = row.split(' ')[2]
                        logger.info('timeStamp: %s -- ifaceBytes: %s' % (timeStamp, ifaceByte))
                        timeStampsIface.append(timeStamp)
                        ifaceBytesIface.append(ifaceByte)
                        queue = ''
                    if 'class' in row:
                        queue = row.split(' ')[2]
                        if (hnb[ifaceIndex], queue) not in self.getCombinations(ifbIfaces, htbQueues):
                            queue = ''
                timeStamps += [timeStampsIface]
                ifaceBytes += [ifaceBytesIface]
        logger.info('ifbIfaces: %s' % ifbIfaces)
        logger.info('htbQueues: %s' % htbQueues)
        logger.info('timeStamps: %s' % timeStamps)
        logger.info('ifaceBytes: %s' % ifaceBytes)
        
        bytesTimeConfig.set('snapshot', 'ifbIfaces', json.dumps(ifbIfaces))
        bytesTimeConfig.set('snapshot', 'htbQueues', json.dumps(htbQueues))
        bytesTimeConfig.set('snapshot', 'timeStamps', json.dumps(timeStamps))
        bytesTimeConfig.set('snapshot', 'ifaceBytes', json.dumps(ifaceBytes))
        bytesTimeConfig.write(bytesTimeConfFile)
        bytesTimeConfFile.close()

        if edge == 'DL':
            nodes = json.loads(config.get('algorithms', 'nodes'))
            ssh = self.createSSHClient(nodes[0])
            scp = SCPClient(ssh.get_transport())
            scp.put('/var/tmp/bytes-time-%s-%d.conf' % (sense, hnbIndex), '/var/tmp/bytes-time-%s-%d.conf' % (sense, hnbIndex))
Exemplo n.º 55
0
def http_config(hostname, username, password):
    connect = createSSHClient(hostname, 22, username, password)

    """ Installing Apache on the servers"""
    print "\n Installing Apache on the Servers "    
    stdin, stdout, stderr = connect.exec_command('apt-get -y install apache2')
    print 'This is output =',stdout.readlines()
    print 'This is error =',stderr.readlines()
    
    """Copying the index.php file"""
    print "\n Copying the Index.php file remotely"
#    ssh = createSSHClient(hostname, 22, username, password)
    scp = SCPClient(connect.get_transport())
    scp.put(local_path, remote_path)

    """Customizing Apache on the servers"""
    print "\n Customizing Apache "    
    stdin, stdout, stderr = connect.exec_command('mv /var/www/html/index.html /var/www/html/index.html_backup')
    print 'This is output =',stdout.readlines()
    print 'This is error =',stderr.readlines()

    """Changing Permissions on index.php file"""
    print "\n Changing File permissions "    
    stdin, stdout, stderr = connect.exec_command('chown root:root /var/www/html/index.php')
    print 'This is output =',stdout.readlines()
    print 'This is error =',stderr.readlines()

    """Customizing Apache on the servers"""
    print "\n Changing File permissions "    
    stdin, stdout, stderr = connect.exec_command('chmod 644 /var/www/html/index.php')
    print 'This is output =',stdout.readlines()
    print 'This is error =',stderr.readlines()    
    
    """Restarting the Apache server"""
    print "\n Restarting the Apache Server " 
    stdin, stdout, stderr = connect.exec_command('service apache2 restart')
    print 'This is output =',stdout.readlines()
    print 'This is error =',stderr.readlines()
    
        
    """Verify that http server is up and can spit out Hello World"""
    print "\n Verifying http server"
    stdin, stdout, stderr = connect.exec_command('curl -sv "http://localhost"| grep -i hello')
    list_http = stdout.readlines()
    print 'This is output =',list_http
    print 'This is error =',stderr.readlines()
    i = 0
    for each in list_http:
        if re.search(r'Hello', each) != None:
            print "\n ***HTTP server is up and running with Hello World*** \n\n\n"
            exit
        else:
            i =+1
        if i > 0:
            print "\n ***HTTP Server does not have Hello World*** \n\n\n"
    
    connect.close()
Exemplo n.º 56
0
def copy(handler, src, dest, recursive=False):
    """
    :param client: NodeHandler
    :param src: source file path (local)
    :param dest: destination file path (remote)
    :recursive: folder + all subfolders, files?
    """
    # Context manager doesnt work properly? try later -_-
    scp = SCPClient(handler.client.get_transport())
    scp.put(src, dest, recursive=recursive)
Exemplo n.º 57
0
def backup_to_server(servers, tarfile):
	ssh = SSHClient()
	ssh.load_system_host_keys()
	ssh.set_missing_host_key_policy(AutoAddPolicy())
	for server in servers:
		ssh.connect(server['host'], port=server['port'], username=server['user'], password=server['password'])

		# SCPCLient takes a paramiko transport as its only argument
		scp = SCPClient(ssh.get_transport())
		scp.put(tarfile, server['dir'])
Exemplo n.º 58
0
def transferFile(origin, destination):
	hostname = 'fii.to'
	port = 22
	username = '******'
	password = '******'
	ssh = paramiko.SSHClient()
	ssh.load_system_host_keys()
	ssh.connect(hostname, username=username, password=password)
	scp = SCPClient(ssh.get_transport())
	scp.put(origin, destination)
Exemplo n.º 59
0
def scp(file, path, ip, user, password):
    """Uses paramiko and scp module built on top of it to SCP vEOS to the VM.
    Inputs are the .swi filename from arguments, and a path."""
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.connect(hostname=ip, username=user, password=password)
    scp = SCPClient(ssh.get_transport())
    print "\nWriting " + file + " to " + path
    scp.put(file, remote_path=path)
    scp.close()
Exemplo n.º 60
0
def transport_assembly_run( seed_name, tfilename, workdir='./build' ):

   # Push package: to-be processed packages are placed in 'put' directory.
   # Pulled package: finished work is in the 'get' directory.
   retry = 1
   # Credentials wide out in the open:
   ssh = createSSHClient(CL_SERVER_NAME, 22, CL_SERVER_USER, CL_SERVER_PW)
   while (retry < 10):
      try:
         scp = SCPClient(ssh.get_transport())
         scp.put(tfilename, 'run')
         # If it got this far, it succeeded
         break;
      except Exception as err:
         print '=============='
         print err[0]
         print '    Retry attemp #'+retry
         print '=============='
         sleep(5)

   # 
   # Server Processing Phase
   #
   waittime = 0
   while (waittime < 600):
      try:
         scp = SCPClient(ssh.get_transport())
         # Check the log file
         scp.get('get/'+seed_name+'_run.log', local_path=workdir)
         line  = tail("-1", os.path.join(workdir,seed_name+'_run.log') ).strip()
         print line
         if 'Assembly Complete' in line:
            break
         if 'ERROR' in line:
            exit(1)
      except Exception as err:
         print '=============='
         print err[0]
         print '=============='
         if 'No such file or directory' in err[0]:
            print "---- No status yet ----"
      sleep(2)
      waittime = waittime + 1
      scp.close()
    # Compile complete.  Download results.
   try:
      scp.get('get/'+seed_name+'_assembly.dat', local_path=workdir)
      os.rename( os.path.join(workdir, seed_name+'_assembly.dat'), os.path.join(workdir,'assembly.dat'))
   except Exception as err:
      print '=============='
      print err[0]
      print '=============='
      exit(1)

   return True