def connection_test():
    creds = request.get_json() or {}
    host = creds['host']
    try:
        ping(host, 443)
    except Exception as e:
        return jsonify(error=str(e)), 404

    verify = True
    secured = request.headers.get('Secured')
    if secured == 'false':
        verify = False
    try:
        url = "https://" + host + "/rest/login-sessions"
        headers = {'X-Api-Version': '200', 'Content-Type': 'application/json'}
        data = {'userName': creds['username'], 'password': creds['password']}
        response = requests.post(url,
                                 data=json.dumps(data),
                                 headers=headers,
                                 verify=verify)
    except Exception as e:
        if 'SSLError' in str(e):
            return jsonify(error=str(e)), 403
        else:
            return jsonify(error=str(e)), 401

    if not response.status_code == 200:
        return jsonify(error=response.json()['message']), 400

    return jsonify(response.json())
def connection_test():
    body = request.get_json() or {}
    host = body['host']
    try:
        ping(host, 22)
        return jsonify('Success')
    except Exception as e:
        return jsonify(error=str(e)), 404
Exemple #3
0
def setUp(dt):

    THREADS[str(dt)] = util.TestingServer(PORT)
    THREADS[str(dt)].start()
    count = 0
    while not util.ping(PORT) and count < 50:
        count += 1
Exemple #4
0
def SAM7X_telnet(ip_addr, cmd, *args):
    '''
    Обратиться по протоколу telnet к устройству %ip_addr% и выполнить команду %cmd%
    @param ip_addr - ip-адрес устройства
    @param cmd - команда
    @return результат выполнения команды cmd
    '''
    if len(args):
        cmd = ' '.join([cmd] + list(args))
        cmd = cmd.strip()
    if not ping(ip_addr):
        print('Failed to ping  %s' % ip_addr)
        return
    try:
        tn = Telnet(ip_addr)
        tn.read_until(b'#> ', 2)
        cmd += '\n'
        tn.write(cmd.encode('ascii'))
        s = tn.read_until(b'#> ', 2)
        tn.close()
        '''
        try:
            tn.write(b'exit\n')
            tn.read_until(b'#> ', 2)
            tn.close()
        except:
            pass
        '''
        def splitstr(s, b):
            r = re.compile(b)
            ss = r.split(s)
            ss = list(filter(lambda x: len(x), ss))
            return ss

        s = s.decode('ascii')
        ss = splitstr(s, '[\[\] \t\n\r:]+')
        ss0 = ss[0]
        if ss0 in ['0', '1']:
            ss = splitstr(s, '[\t\n\r]+')
            ss1 = ss[0]
            ss1 = ss1.replace('[%s]' % ss0, '')
            ss1 = ss1.strip()
            return ss1
        ss = s.split('\n')
        print('Failed to parse', ss)
        return
    except:
        print('telnet error')
        #print(sys.exc_info())
        return
Exemple #5
0
def tryAddress(the_queue, result_dict, the_pid):
    the_file = open(DIR + str(the_pid) + "_output.txt", 'w')
    while not the_queue.empty():
        address = the_queue.get()
        print "Trying %s..." % address
        if util.ping(address):
            path = util.traceroute(address)
            if len(path) is not 0:
                the_file.write("Traced %s: \n" % address)
                for litstep in path:
                    the_file.write(litstep + " ")
                the_file.write("%d\n" % len(path))
                result_dict[address] = path
    the_file.close()
Exemple #6
0
def STM32ETH_telnet(ip_addr, cmd, *args):
    '''
    Обратиться по протоколу telnet к устройству %ip_addr% и выполнить команду %cmd%
    @param ip_addr - ip-адрес устройства
    @param cmd - команда
    @return результат выполнения команды cmd
    '''
    if len(args):
        cmd = ' '.join([cmd] + list(args))
        cmd = cmd.strip()
    if not ping(ip_addr):
        print('Failed to ping %s' % ip_addr)
        return
    try:
        tn = Telnet(ip_addr)
        tn.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        s = tn.read_until(b'#> ', 1)
        if not s:
            tn.close()
            raise Exception('telnet error')

        cmd += '\n\r'
        tn.write(cmd.encode('ascii'))
        s = tn.read_until(b'#> ', 5)
        tn.close()

        def splitstr(s, b):
            r = compile(b)
            ss = r.split(s)
            ss = list(filter(lambda x: len(x), ss))
            return ss
        s = s.decode('ascii')
        ss = splitstr(s, '[\[\] \t\n\r:]+')
        ss0 = ss[0]
        if ss0 in ['0', '1']:
            ss = splitstr(s, '[\t\n\r]+')
            ss1 = ss[0]
            ss1 = ss1.replace('[%s]' % ss0, '')
            ss1 = ss1.strip()
            return ss1
        ss = s.split('\n')
        print('Failed to parse', ss)
        return
    except:
        print('telnet error')
        #print(sys.exc_info())
        return
Exemple #7
0
def tearDown(dt):

    THREADS[str(dt)].shutdown()
    count = 0
    while util.ping(PORT) and count < 50:
        count += 1