Esempio n. 1
0
def download_log(remote_path, file_name, local_path):
    global cli
    try:
        with SCPClient(cli.get_transport()) as scp:
            scp.get(remote_path+file_name, local_path)
    except SCPException as e:
        print("Operation error : %s"%e)
    try:
        with open(local_path+file_name) as f:
            text = f.read()
    except:
        try:
            with open(local_path+file_name, encoding='ISO-8859-1') as f:
                text = f.read()
        except Exception as e:
            print("Opreation error at reading file %s : %s"%(file_name, e))

    try :
        text = re.sub(r'[0-9]+', '', text)
    except Exception as e:
        print("Operation error at re.sub : %s"%e)
        os.remove(local_path+file_name)
        return ''
    os.remove(local_path+file_name)
    return text
 def scp_get_files(self):
     """"""
     if self.conf != None:
         '''SET CONNECTION PREFS'''
         #self.set_connection_params()
         '''SET SSH CLIENT AND TRANSPORT'''
         self.create_Paramiko_SSHClient()
         scp = SCPClient(self.ssh_client.get_transport())
         self.logger.info("<== SCP CLASS STARTED GETTING files ==> ")
         scp.get(remote_path=self.ssh_scp_content_location,
                 local_path=self.ssh_target_dir)
         scp.close()
         self.logger.info("<== SCP CLASS HAS GOT ==> ")
         '''Check if it is actually here'''
         f_name = self.str_split_get_pop_elem(
             str_in=self.ssh_scp_content_location,
             delim_in='/',
             which_elem='LAST')
         copied_file = Path(self.ssh_target_dir + '\\' + f_name)
         if copied_file.is_file():
             self.logger.debug("<== File with name  ==> ")
             self.logger.debug("<==" + str(copied_file) + "==> ")
             self.logger.debug(
                 "<==  seems to be in place Now the question is it the right one  ---> "
             )
         else:
             raise ValueError(
                 "Configuration file is not PRESENT in the CLASS!!!")
     return
Esempio n. 3
0
def download_log(remote_path, file_name, local_path):
    with open('../server_info.yaml') as f:
        server_info = yaml.load(f)['log']
    try:
        cli = paramiko.SSHClient()
        cli.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        cli.connect(server_info['ip'],
                    port=22,
                    username=server_info['id'],
                    password=server_info['pwd'])
        with SCPClient(cli.get_transport()) as scp:
            scp.get(remote_path + file_name, local_path)
    except SCPException as e:
        print("Operation error : %s" % e)
    try:
        with open(local_path + file_name) as f:
            text = f.read()
    except:
        try:
            with open(local_path + file_name, encoding='ISO-8859-1') as f:
                text = f.read()
        except Exception as e:
            print("Opreation error at reading file %s : %s" % (file_name, e))
    os.remove(local_path + file_name)
    return text
Esempio n. 4
0
def grab_backup():
    # Make backup directory first
    os.makedirs(LOCAL_LOCATION)
    # Connect
    SSH_CLIENT = paramiko.SSHClient()
    SSH_CLIENT.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    SSH_CLIENT.connect(HOST,
                       port=SSH_PORT,
                       username=SSH_USER,
                       key_filename=SSH_KEY)
    scp = SCPClient(SSH_CLIENT.get_transport())
    scp.get(DB_DUMP_LOCATION, LOCAL_LOCATION)
    scp.close()
    SSH_CLIENT.close()
    cleanup()
Esempio n. 5
0
    def test_up_and_down(self):
        '''send and receive files with the same client'''
        previous = os.getcwd()
        testfile = os.path.join(self._temp, 'testfile')
        testfile_sent = os.path.join(self._temp, 'testfile_sent')
        testfile_rcvd = os.path.join(self._temp, 'testfile_rcvd')
        try:
            os.chdir(self._temp)
            with open(testfile, 'w') as f:
                f.write("TESTING\n")
            put(self.ssh.get_transport(), testfile, testfile_sent)
            get(self.ssh.get_transport(), testfile_sent, testfile_rcvd)

            assert open(testfile_rcvd).read() == 'TESTING\n'
        finally:
            os.chdir(previous)
Esempio n. 6
0
    def test_up_and_down(self):
        '''send and receive files with the same client'''
        previous = os.getcwd()
        testfile = os.path.join(self._temp, 'testfile')
        testfile_sent = os.path.join(self._temp, 'testfile_sent')
        testfile_rcvd = os.path.join(self._temp, 'testfile_rcvd')
        try:
            os.chdir(self._temp)
            with open(testfile, 'w') as f:
                f.write("TESTING\n")
            put(self.ssh.get_transport(), testfile, testfile_sent)
            get(self.ssh.get_transport(), testfile_sent, testfile_rcvd)

            with open(testfile_rcvd) as f:
                self.assertEqual(f.read(), 'TESTING\n')
        finally:
            os.chdir(previous)
Esempio n. 7
0
def main():

    HOST = '172.21.39.121'
    PORT = "22"
    USER = '******'
    PSWD = '0000'

    path1='/Users/yasu/darknet/'

    room_number='A309'

    while True:

        print('------------conect to raspberryPi and get room images----------')

        #setting of SSH
        ssh = SSHClient()
        ssh.set_missing_host_key_policy(AutoAddPolicy())
        ssh.connect(HOST, port=PORT,username=USER, password=PSWD)
        #SCP(download the room image from raspberry pi)
        scp = SCPClient(ssh.get_transport())
        scp.get('/home/pi/camera/roomA309.jpg')
        #change place to detect object by Yolov3
        shutil.copyfile(path1 + 'roomA309.jpg', path1 + '/room/roomA309.jpg')

        #なくてもよい(you can delete this code)
        os.remove(path1 + 'roomA309.jpg')

        print('---------------detect object!------YOLOOOOOOhh!----------------')

        #command to use yolov3          |grep person| wc -l  ←← count(extract?) number of people in the all objects!
        command = './darknet detect cfg/yolov3.cfg yolov3.weights room/roomA309.jpg | grep person | wc -l'
        proc = subprocess.Popen(command,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        #return value(stdout_data) is number of people
        stdout_data, stderr_data = proc.communicate()
        #change the data type to int type
        number_of_people = int(stdout_data.decode('ascii'))
        #open the txt file to write room name and number of people
        t=open('/Users/yasu/darknet/count_people/'+ room_number + '.txt','w')
        #write room name and number of people
        t.write('Number of people(' + room_number + ')' + '\n' +str(number_of_people))
        print('There is ' + str(number_of_people) + ' people in the room A309')
        #wait 5min(300sec)
        time.sleep(120)
Esempio n. 8
0
def get_fault_history(vnf_num):
    #print("Get Fault history")
    fault_history = {}
    with open('../server_info.yaml') as f:
        server_info = yaml.load(f)['FaultHistory']
    cli = paramiko.SSHClient()
    cli.set_missing_host_key_policy(paramiko.AutoAddPolicy)
    cli.connect(server_info['ip'],
                port=22,
                username=server_info['id'],
                password=server_info['pwd'])
    try:
        with SCPClient(cli.get_transport()) as scp:
            scp.get('/home/ubuntu/fault_history.yaml', local_path)
    except SCPException:
        raise SCPException.message
    with open(local_path + 'fault_history.yaml') as f:
        fault_history = yaml.load(f)
    return fault_history[vnf_num]
Esempio n. 9
0
def download_save(ssh, scp, pwd, local_path, server_path):
    print("Downloading Server Save File...")

    print("Stopping factorio.service...")
    cmd = "echo " + pwd + " | sudo -S systemctl stop factorio.service"
    stdin, stdout, stderr = ssh.exec_command(cmd)
    time.sleep(3)

    print("Downloading save zip file via SCP...")
    scp.get(server_path, local_path)
    time.sleep(3)

    print("Restarting factorio.service...")
    cmd = "echo " + pwd + " | sudo -S systemctl restart factorio.service"
    stdin, stdout, stderr = ssh.exec_command(cmd)
    time.sleep(3)

    cmd = "echo " + pwd + " | sudo -S systemctl status factorio.service --no-pager"
    stdin, stdout, stderr = ssh.exec_command(cmd)
    print("".join(stdout.readlines()) + "\nDownload Complete!")
Esempio n. 10
0
def test():


    #scp pi@[172.21.39.116]:/home/pi/camera/A.jpg /Users/yasu

    HOST = '172.21.39.116'
    PORT = "22"
    USER = '******'
    PSWD = '0000'

    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(HOST, port=PORT,username=USER, password=PSWD)

    scp = SCPClient(ssh.get_transport())
    scp.get('/home/pi/camera/roomA309.jpg')

    path1='/Users/yasu/darknet/'
    #copy roomA309.jpg to ./room
    shutil.copyfile(path1 + 'roomA309.jpg', path1 + '/room/roomA309.jpg')

    os.remove(path1 + 'roomA309.jpg')
Esempio n. 11
0
def main():
    with open('C:\\Users\\ayc08\\Documents\\GitHub\\python_study\\src\\nonpass\\sample\\sample.csv', 'r') as f:
        read = csv.DictReader(f)
        for row in read:
            source_host = row.get('source_host')
            print(source_host)

            source_host_fqdn = source_host + '.b.' + source_host[-3:] + '.server'
            print(source_host_fqdn)

            with paramiko.SSHClient() as ssh:
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(hostname=source_host_fqdn, port=22, username="", password="")

                with scp.SCPClient(ssh.get_transport()) as scp:
                    scp.put('','')
                    source_dir = row.get('source_directory') + '/.ssh'

                    stdin, stdout, stderr = ssh.exec_command('python ' + GENERATE_PUB_KEY + ' ' + source_dir)
                    scp.get('','')
                    ssh.exec_command('rm ' + GENERATE_PUB_KEY)

            target_host = row.get('target_host')
            print(target_host)

            target_host_fqdn = target_host + '.b.' + target_host[-3:] + '.server'
            print(target_host_fqdn)

            with paramiko.SSHClient() as ssh:
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(hostname=target_host_fqdn, port=22, username="", password="")

                with scp.SCPClient(ssh.get_transport()) as scp:
                    scp.put('','')
                    target_dir = row.get('target_directory') + '/.ssh'

                    stdin, stdout, stderr = ssh.exec_command('python ' + SETTING_PUB_KEY + ' ' + target_dir)
                    ssh.exec_command('rm ' + SETTING_PUB_KEY)
password = "******"

ssh = createSSHClient(server, port, user, password)
scp = SCPClient(ssh.get_transport())
nowstring = (now.strftime("%m-%d-%Y-%H-%M"))
path = "C:/Users/user/backup.confs/confs/%s" % nowstring

if not os.path.exists(path):
    try:
        os.mkdir(path)
    except OSError:
        print("Could not make directory, made in C:/Temp instead.")
        path = "C:/Temp/confs/%s" % nowstring
        os.mkdir(path)

scp.get("/tmp/system.cfg", path + "/system.cfg")
scp.get("/tmp/running.cfg", path + "/running.cfg")

#email to teams, datetime, first and tailing 20 chars as confirmation
myTeamsMessage.text("Server:" + server + "  |  Date: " + nowstring +
                    "| Directory Name: " + path + "| system.cfg present: **" +
                    str(os.path.exists(path + "/system.cfg")) +
                    "** - system.cfg size: " +
                    str(os.path.getsize(path + "/system.cfg")) +
                    "| running.cfg present: **" +
                    str(os.path.exists(path + "/running.cfg")) +
                    "** - running.cfg size: " +
                    str(os.path.getsize(path + "/running.cfg")))
print(("Server: " + server + "  |  Date: " + nowstring +
       "  |  Directory Name: " + path + "  |  system.cfg present: " +
       str(os.path.exists(path + "/system.cfg")) + " - system.cfg size: " +
Esempio n. 13
0
def satisfies_constraints(constraints):
    try:
        fd = open('creds.txt')
    except FileNotFoundError:
        print(
            'Create file creds.txt in this directory with pipeline username on line 1 and password on line 2'
        )
    username = fd.readline().strip('\n')
    password = fd.readline().strip('\n')
    """ Returns list of #### strings representing the file numbers that satisfy
    the given dict of constraints.
    ...
    Opens an SSH connection to the mitastrometry server, grabs all the Rb and
    .LOG files, shoves them into temp directories. For each extra constraint
    specified, looks in the fits header to check whether constraint is
    satisfied. If file is good, keep it in temp directory. If file is bad,
    delete it and its .LOG counterpart.
    """
    print(constraints)

    def progress(filename, size, sent):
        sys.stdout.write("%s\'s progress: %.2f%%   \r" %
                         (filename, float(sent) / float(size) * 100))

    rpath = 'R'
    if os.path.exists(rpath):
        while os.path.exists(rpath):
            rpath = rpath + '0'
    os.system('mkdir ' + rpath)
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.connect('astrometry.mit.edu', username=username, password=password)
    with SCPClient(ssh.get_transport(),
                   sanitize=lambda x: x,
                   progress=progress) as scp:
        scp.get('/ast2/data/' + constraints['telescope'] + '/' +
                constraints['date'][:4] + '/' +
                constraints['date'].split('/')[0] + '/' +
                constraints['date'].split('/')[0] + '.R.*.gz')
    ssh.close()
    for file in glob.glob(constraints['date'].split('/')[0] + '.R.*.gz'):
        shutil.move(file, rpath + '/')
    filenames = os.listdir(rpath + '/')
    score = 0
    validated = []
    for f in filenames:
        if f[0] == '.':
            continue
        os.system("gunzip '" + rpath + '/' + f + "'")
        for key in constraints.keys():
            if key == 'instrument':
                value = fits.open(rpath + '/' + f[:-3])[0].header['INSTRUME']
                if value != constraints[key]:
                    score = score + 1
            elif key == 'object':
                value = fits.open(rpath + '/' + f[:-3])[0].header['OBJECT']
                if value != constraints[key]:
                    score = score + 1
        if score == 0:
            validated.append(f[-7:-3])
        else:
            print('removing ' + f)
            os.system("rm -rf " + rpath + "/" + f[:-3])
        score = 0
    return validated
import paramiko
import scp

#SSH Client
linux_ipaddress = '10.1.1.10'
ssh_client = paramiko.SSHClient()

#SSH Connection
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=linux_ipaddress,
                   port=22,
                   allow_agent=False,
                   username='******',
                   password='******',
                   look_for_keys=False)

#SCP setup
scp = scp.SCPClient(ssh_client.get_transport())

#Put files on the remote host
scp.put('FILE', 'DESTINATION PATH')

#Put directory on the remote host
scp.put('DIRECTORY', recursive=True, remote_path='DESTINATION PATH')

#Get files from the remote host
scp.get('FILE PATH TO COPY THE FILE/DIRECTORY', 'FILE TO GET')

#Close the scp conneciton
scp.close()
Esempio n. 15
0
 def do_scp_get(self, remotefile, localfile):
     if remotefile and localfile:
         for host, conn in zip(self.hosts, self.connections):
             with scp.SCPClient(conn.get_transport()) as scp:
                 scp.get(remotefile, localfile + '_' + host[0])
Esempio n. 16
0
outname = fastafile.replace('.fasta', '.tblout.scan')
cpu = int(cpu)

shutil.rmtree(dirname+'inputfiles', ignore_errors=True)
os.makedirs(dirname+'inputfiles')

shutil.rmtree(dirname+'outputfiles', ignore_errors=True)
os.makedirs(dirname+'outputfiles')


c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('abdoserver2.cvmbs.colostate.edu')

scp = scp.SCPClient(c.get_transport())
scp.get('/home/lakinsm/hmm_testing/cs_cluster_files/parts/project7/{}'.format(fastafile), dirname+'inputfiles/{}'.format(fastafile))

## FIXME: check to see if models are intact/present

p = subprocess.Popen(HMMER_CMD.format(cpu, dirname, outname, dirname, dirname, fastafile), stderr=subprocess.PIPE, shell=True)

stderrlines = list(p.stderr)
sys.stderr.write('\n'.join([str(x) for x in stderrlines])+'\n')
p.wait()

if is_complete(dirname+'outputfiles/{}'.format(outname)):
    scp.put(dirname+'outputfiles/{}'.format(outname), '/home/lakinsm/hmm_testing/cs_cluster_files/output/project7/groupIII/{}'.format(outname))

scp.close()
c.close()
Esempio n. 17
0
def output():
    import paramiko
    send = open("settings.ink", "r")
    ilk = send.readline()
    hostname = ilk.rstrip()

    ilk = send.readline()
    username = ilk.rstrip()

    ilk = send.readline()
    password = ilk.rstrip()

    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        client.connect(hostname=hostname, username=username, password=password)
    except:
        print("[!] Cannot connect to the SSH Server")

    name = open("filenamefortruba.ink", "r")
    line = name.readlines()
    lines = str(line)
    search = lines.find("#NuNuTRUBA_DONENDOSYALAR=")
    search = search + 25
    i = search
    file = []

    while True:
        search = search + 1
        if (lines[search] == ","):
            file = lines[i:search]
            from scp import SCPClient
            scp = SCPClient(client.get_transport())
            liste = ['/truba', 'home', username, file]
            alinan = '/'.join(liste)

            name = open("filenamefortruba.ink", "r")
            adres = name.readline()
            adres = str(adres)
            adres = adres[::-1]
            a = 0
            while True:
                a = a + 1
                if (adres[a] == "/"):
                    break
            adres = adres[a + 1:]
            adres = adres[::-1]
            liste2 = [adres, file]
            gelen = '/'.join(liste2)
            scp.get(alinan, gelen)

            i = search + 1

        if (lines[search] == ":"):
            file = lines[i:search]
            from scp import SCPClient
            scp = SCPClient(client.get_transport())
            liste = ['/truba', 'home', username, file]
            alinan = '/'.join(liste)

            name = open("filenamefortruba.ink", "r")
            adres = name.readline()
            adres = str(adres)
            adres = adres[::-1]
            a = 0
            while True:
                a = a + 1
                if (adres[a] == "/"):
                    break
            adres = adres[a + 1:]
            adres = adres[::-1]
            liste2 = [adres, file]
            gelen = '/'.join(liste2)
            scp.get(alinan, gelen)

            i = search + 1

            break
def sync():
    os.chdir(local_dir)
    scp.get(remote_dir, recursive=True)
Esempio n. 19
0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:wangtaihe
# datetime:2020/7/29 14:54
# software: PyCharm

import paramiko
import scp
from paramiko import SSHClient
from scp import SCPClient
import sys, tarfile, os

ssh = SSHClient()
ssh.connect(hostname='', username='', password='')


def progress(filename, size, sent):
    sys.stdout.write("%s \'s progresss: %.2f%%     \r" %
                     (filename, float(sent) / float(float(size)) * 100))


scp = SCPClient(ssh.get_transport(), progress=progress())
scp.get("")
Esempio n. 20
0
import paramiko
import scp
from datetime import datetime

now = datetime.now()

with paramiko.SSHClient() as ssh:
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print('ssh接続')
    ssh.connect(hostname='133.19.62.9',
                username='******', password='******')

    print('SCP転送開始')
    # scp clientオブジェクト生成
    with scp.SCPClient(ssh.get_transport()) as scp:
        scp.get("store_pcd/202011/20201106_152639.tar.gz", "pcd/20201106_152639.tar.gz")

    ssh.close()

print('ファイルを取得しました')
Esempio n. 21
0
File: utils.py Progetto: durd07/bulk
 def do_scp_get(self, remotefile, localfile):
     if remotefile and localfile:
         for host, conn in zip(self.hosts, self.connections):
             with scp.SCPClient(conn.get_transport()) as scp:
                 scp.get(remotefile, localfile + '_' + host[0])