def connect_ftps():
    """
    FTP over TLS
    NOte:
        do NOT enable 'Require TLS session resumption on data connection when using PROT P',
        or you will get exception 'ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:727)'
        or server side will show '450 TLS session of data connection has not resumed or the session does not match the control connection'
    :return:
    """
    ftps = FTP_TLS()
    # ftps.set_debuglevel(2)
    ssl_version_stack = [ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3]
    tls_version_stack = [ssl.PROTOCOL_TLSv1_2, ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLS]
    is_connected = False
    for ssl_version in ssl_version_stack + tls_version_stack:  # also can use list.extend() or list slice instead of +
        ftps.ssl_version = ssl_version
        try:
            ftps.connect(host=server, port=port)
            is_connected = True
            break
        except socket.timeout:
            continue
        except socket.error as e:
            # such as '10061', '[Errno 10061]  Connection refused.'
            print(str(e), socket.errorTab.get(int(str(e).strip()[7:-1])))
            continue
    else:
        if not is_connected:
            raise RuntimeError("No SSL/TLS supported on ftp server, does server misconfigured?")

    ftps.login(user=username, passwd=password)
    ftps.prot_p()  # 'Force PROT P to encrypt file transfers when using FTP TLS'
    return ftps
def ftps_connect(host):
    ftps = FTP_TLS()
    ftps.ssl_version = ssl.PROTOCOL_SSLv23
    ftps.connect(host)
    ftps.login(settings.NC_FTP_USER, settings.NC_FTP_PASSWORD)
    ftps.prot_p()
    return ftps
def connect():
    ftp = FTP_TLS()
    ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
    ftp.debugging = 2
    ftp.connect("localhost", 2121)
    ftp.login("developer", "password")
    return ftp
Beispiel #4
0
def connect():
    config = configparser.ConfigParser()
    config.read('config.ini')
    ftp = FTP_TLS()
    ftp.ssl_version = ssl.PROTOCOL_SSLv23
    ftp.connect(config['node53']['address'], 21)
    ftp.login(config['node53']['user'], config['node53']['password'])
    print(ftp.prot_p())
    return ftp
Beispiel #5
0
def connect(user, password, ip_address='pensieve.usc.edu', port=41590):
    ftp = FTP_TLS()
    ftp.ssl_version = ssl.PROTOCOL_SSLv23
    ftp.debugging = 2
    ftp.connect(ip_address, port)
    ftp.login(user, password)
    ftp.prot_p()
    ftp.retrlines('LIST home')
    return(ftp)
Beispiel #6
0
 def send(self):
     ftp = FTP(self.ftp_destination)
     ftp.set_debuglevel(2)
     ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
     ftp.login()
     ftp.cwd("upload")
     ftp.prot_p()
     self.zip.close()
     self.zip_contents.seek(0)
     ftp.storbinary("STOR {}".format(self.zip_contents.name), self.zip_contents)
 def FTPconnect(self):
     #initial FTP object and login the HCA FTP server 
     try:
         ftp = FTP_TLS()
         ftp.set_pasv(True,self.host)
         ftp.ssl_version = ssl.PROTOCOL_TLSv1
         ftp.connect(host=self.host,port=21)
         ftp.set_debuglevel(2)
         ftp.auth()
         ftp.login(self.user,self.password)
         ftp.prot_p()
         print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())+' FTP login successful')
         return ftp
     except Exception as e:
         self.sns.informError()
         print (e)
Beispiel #8
0
    def make_msg(self, args):

        # make connection
        conn = None
        if args.ssl or args.starttls:
            conn = FTP_TLS()
            conn.ssl_version = PROTOCOL_TLSv1
        else:
            conn = FTP()

        if args.verbose:
            conn.set_debuglevel(1)

        conn.connect(args.host, args.port, args.timeout)

        if args.starttls:
            conn.auth()
            conn.prot_p()

        conn.login(args.user, args.password)
        self.conn = conn

        return args.message
def run_module():
    module_args = dict(src=dict(type="str", required=True),
                       wait=dict(type="bool", required=False),
                       location=dict(
                           type="str",
                           default="DATA_SET",
                           choices=["DATA_SET", "USS", "LOCAL"],
                       ),
                       volume=dict(type="str", required=False),
                       return_output=dict(type="bool",
                                          required=False,
                                          default=True),
                       wait_time_s=dict(type="int", default=60),
                       max_rc=dict(type="int", required=False))

    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
    result = dict(changed=False, )

    location = module.params["location"]
    volume = module.params["volume"]
    wait = module.params["wait"]
    src = module.params["src"]
    return_output = module.params["return_output"]
    wait_time_s = module.params["wait_time_s"]
    max_rc = module.params["max_rc"]

    if wait_time_s <= 0:
        module.fail_json(
            msg=
            "The option wait_time_s is not valid.  It must be greater than 0.",
            **result)

    if environ.get('FTP_SOCKS_PORT'):
        import socks
        import socket
        socks.set_default_proxy(socks.SOCKS5, "127.0.0.1",
                                int(environ.get('FTP_SOCKS_PORT')))
        socket.socket = socks.socksocket

    try:
        if environ.get('FTP_TLS_VERSION'):
            from ftplib import FTP_TLS
            import ssl
            cert_file_path = environ.get('FTP_TLS_CERT_FILE')
            if cert_file_path:
                if not path.isfile(cert_file_path):
                    module.fail_json(
                        msg="The TLS cartificate file not found: {0}".format(
                            repr(cert_file_path)),
                        **result)
                context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
                context.load_verify_locations(cert_file_path)
                context.check_hostname = False
                ftp = FTP_TLS(context=context)
            else:
                ftp = FTP_TLS()
            tls_version = environ.get('FTP_TLS_VERSION')
            if tls_version == '1.2':
                ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
        else:
            ftp = FTP()
        ftp.connect(environ.get('FTP_HOST'),
                    int(environ.get('FTP_PORT') or 21))
        ftp.login(environ.get('FTP_USERID'), environ.get('FTP_PASSWORD'))
        ftp.sendcmd("site filetype=jes")
        ftp.set_pasv(True)

        if environ.get('FTP_TLS_VERSION'):
            ftp.prot_p()

    except Exception as e:
        module.fail_json(
            msg="An unexpected error occurred during FTP login: {0}".format(
                repr(e)),
            **result)

    DSN_REGEX = r"^(?:(?:[A-Z$#@]{1}[A-Z0-9$#@-]{0,7})(?:[.]{1})){1,21}[A-Z$#@]{1}[A-Z0-9$#@-]{0,7}(?:\([A-Z$#@]{1}[A-Z0-9$#@]{0,7}\)){0,1}$"
    try:
        if location == "DATA_SET":
            data_set_name_pattern = re.compile(DSN_REGEX, re.IGNORECASE)
            if PY2:
                check = data_set_name_pattern.match(src)
            else:
                check = data_set_name_pattern.fullmatch(src)
            if check:
                if volume is None or volume == "":
                    jobId = submit_pds_jcl(src, ftp, module)
                else:
                    jobId = submit_jcl_in_volume(src, volume, ftp, module)
            else:
                ftp.quit()
                module.fail_json(
                    msg=
                    "The parameter src for data set is not a valid name pattern. Please check the src input.",
                    **result)
        else:
            jobId = submit_ftp_jcl(src, ftp, module)

    except SubmitJCLError as e:
        module.fail_json(msg=repr(e), **result)
    if jobId is None or jobId == "":
        result["job_id"] = jobId
        ftp.quit()
        module.fail_json(
            msg=
            "JOB ID Returned is None. Please check whether the JCL is valid.",
            **result)

    result["job_id"] = jobId
    if not wait:
        wait_time_s = 10

    # real time loop - will be used regardless of 'wait' to capture data
    starttime = timer()
    loopdone = False
    foundissue = None
    while not loopdone:
        try:
            job_output_txt = job_output(ftp, wait_time_s, job_id=jobId)
        except IndexError:
            pass
        except Exception as e:
            result["err_detail"] = "{1} {2}.\n".format(
                "Error during job submission.  The output is:", job_output_txt
                or " ")
            module.fail_json(msg=repr(e), **result)

        if bool(job_output_txt):
            jot_retcode = job_output_txt[0].get("ret_code")
            if bool(jot_retcode):
                job_msg = jot_retcode.get("msg")
                if re.search(
                        "^(?:{0})".format("|".join(JOB_COMPLETION_MESSAGES)),
                        job_msg):
                    loopdone = True
                    # if the message doesn't have a CC, it is an improper completion (error/abend)
                    if re.search("^(?:CC)", job_msg) is None:
                        foundissue = job_msg

        if not loopdone:
            checktime = timer()
            duration = round(checktime - starttime)
            if duration >= wait_time_s:
                loopdone = True
                result["message"] = {
                    "stdout":
                    "Submit JCL operation succeeded but it is a long running job, exceeding the timeout of "
                    + str(wait_time_s) + " seconds.  JobID is " + str(jobId) +
                    "."
                }
            else:
                sleep(1)

    # End real time loop ^^^

    if bool(job_output_txt):
        result["jobs"] = job_output_txt

    checktime = timer()
    duration = round(checktime - starttime)
    result["duration"] = duration
    result["changed"] = True

    if duration >= wait_time_s:
        result["message"] = {
            "stdout":
            "Submit JCL operation succeeded but it is a long running job. Timeout is "
            + str(wait_time_s) + " seconds.  JobID is " + str(jobId) + "."
        }
    else:
        if foundissue is not None:
            result["changed"] = False
            result["message"] = {
                "stderr": "Submit succeeded, but job failed: " + foundissue
            }
            result["failed"] = True
            module.fail_json(msg=result["message"], **result)
        elif (wait is True and return_output is True and max_rc is not None
              and max_rc < result.get("jobs")[0].get("ret_code").get("code")):
            result["message"] = {
                "stderr":
                "Submit succeeded, but the return code is more than max_rc: {0}"
                .format(max_rc)
            }
            result["failed"] = True
            module.fail_json(msg=result["message"], **result)
        else:
            result["message"] = {
                "stdout":
                "Submit JCL operation succeeded with id of " + str(jobId) + "."
            }

    module.exit_json(**result)
def run_module():
    module_args = dict(commands=dict(type="raw",
                                     required=True,
                                     aliases=["command"]), )

    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
    result = dict(changed=False, )

    commands = module.params['commands']

    if environ.get('FTP_SOCKS_PORT'):
        import socks
        import socket
        socks.set_default_proxy(socks.SOCKS5, "127.0.0.1",
                                int(environ.get('FTP_SOCKS_PORT')))
        socket.socket = socks.socksocket

    try:
        if environ.get('FTP_TLS_VERSION'):
            from ftplib import FTP_TLS
            import ssl
            cert_file_path = environ.get('FTP_TLS_CERT_FILE')
            if cert_file_path:
                if not path.isfile(cert_file_path):
                    module.fail_json(
                        msg="Certification file not found: {0}".format(
                            repr(cert_file_path)),
                        **result)
                context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
                context.load_verify_locations(cert_file_path)
                context.check_hostname = False
                ftp = FTP_TLS(context=context)
            else:
                ftp = FTP_TLS()
            tls_version = environ.get('FTP_TLS_VERSION')
            if tls_version == '1.2':
                ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
        else:
            ftp = FTP()
        ftp.connect(environ.get('FTP_HOST'),
                    int(environ.get('FTP_PORT') or 21))
        ftp.login(environ.get('FTP_USERID'), environ.get('FTP_PASSWORD'))
        ftp.sendcmd("site filetype=jes")
        ftp.set_pasv(True)

        if environ.get('FTP_TLS_VERSION'):
            ftp.prot_p()

    except Exception as e:
        module.fail_json(
            msg="An unexpected error occurred during FTP login: {0}".format(
                repr(e)),
            **result)

    try:
        result = run_tso_command(ftp, commands, module)
        ftp.quit()
        for cmd in result.get("output"):
            if cmd.get("rc") != 0:
                module.fail_json(msg='The TSO command "' +
                                 cmd.get("command", "") +
                                 '" execution failed.',
                                 **result)

        result["changed"] = True
        module.exit_json(**result)

    except Exception as e:
        ftp.quit()
        module.fail_json(msg="An unexpected error occurred: {0}".format(
            repr(e)),
                         **result)
def run_module():
    module_args = dict(
        job_id=dict(type="str", required=False),
        job_name=dict(type="str", required=False),
        owner=dict(type="str", required=False),
        ddname=dict(type="str", required=False),
    )

    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    job_id = module.params.get("job_id")
    job_name = module.params.get("job_name")
    owner = module.params.get("owner")
    ddname = module.params.get("ddname")

    if environ.get('FTP_SOCKS_PORT'):
        import socks
        import socket
        socks.set_default_proxy(socks.SOCKS5, "127.0.0.1",
                                int(environ.get('FTP_SOCKS_PORT')))
        socket.socket = socks.socksocket

    try:
        if environ.get('FTP_TLS_VERSION'):
            from ftplib import FTP_TLS
            import ssl
            cert_file_path = environ.get('FTP_TLS_CERT_FILE')
            if cert_file_path:
                if not path.isfile(cert_file_path):
                    module.fail_json(
                        msg="The TLS cartificate file not found: {0}".format(
                            repr(cert_file_path)),
                        **result)
                context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
                context.load_verify_locations(cert_file_path)
                context.check_hostname = False
                ftp = FTP_TLS(context=context)
            else:
                ftp = FTP_TLS()
            tls_version = environ.get('FTP_TLS_VERSION')
            if tls_version == '1.2':
                ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
        else:
            ftp = FTP()
        ftp.connect(environ.get('FTP_HOST'),
                    int(environ.get('FTP_PORT') or 21))
        ftp.login(environ.get('FTP_USERID'), environ.get('FTP_PASSWORD'))
        ftp.sendcmd("site filetype=jes")
        ftp.set_pasv(True)

        if environ.get('FTP_TLS_VERSION'):
            ftp.prot_p()

    except Exception as e:
        module.fail_json(
            msg="An unexpected error occurred during FTP login: {0}".format(
                repr(e)),
            **result)

    if not job_id and not job_name and not owner:
        module.fail_json(msg="Please provide a job_id or job_name or owner")

    try:
        result = {}
        wait_time_s = 10
        result["jobs"] = job_output(ftp, wait_time_s, job_id, owner, job_name,
                                    ddname)
        result["changed"] = False
    except Exception as e:
        module.fail_json(msg=repr(e))
    module.exit_json(**result)