2

I have the following code:

#!/usr/bin/python

import time
import uuid
import hmac
import hashlib
import base64
import json
import urllib3
import certifi
import datetime
import requests
import re
from datetime import datetime

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED')  # Force certificatecheck.ca_certs = certifi.where(), #Path to the Certifi bundle.

# Get the status response from pritunl api

BASE_URL = 'https://<REDACTED>'
API_TOKEN = '<REDACTED>'
API_SECRET = '<REDACTED>'
LOG_PATH = '/var/log/logfiles/'


def auth_request(
    method,
    path,
    headers=None,
    data=None,
    ):
    auth_timestamp = str(int(time.time()))


auth_nonce = uuid.uuid4().hex
auth_string = '&'.join([API_TOKEN, auth_timestamp, auth_nonce,
                       method.upper(), path]
                       + (([data] if data else [])))
auth_signature = base64.b64encode(hmac.new(API_SECRET, auth_string,
                                  hashlib.sha256).digest())
auth_headers = {
    'Auth-Token': API_TOKEN,
    'Auth-Timestamp': auth_timestamp,
    'Auth-Nonce': auth_nonce,
    'Auth-Signature': auth_signature,
    }
if headers:
    auth_headers.update(headers)
return http.request(method, BASE_URL + path, headers=auth_headers,
                    body=data)

response1 = auth_request('GET', '/server')
if response1.status == 200:
    pritunlServResponse = json.loads(response1.data)  # print pritunlServResponse# print response1.data

    Name = [y['name'] for y in pritunlServResponse]
    Server_id = [x['id'] for x in pritunlServResponse]

    for srv_name in Name:
        for srv_id in Server_id:
            response2 = auth_request('GET', '/server/' + srv_id
                    + '/output')
            pritunlServResponse2 = json.loads(response2.data)
            py_pritunlServResponse2 = pritunlServResponse2['output']

            print ('value of srv_id: ', srv_id, '\n')
            print ('value of srv_name: ', srv_name, '\n')

            logfile = open(LOG_PATH + srv_name + '_vpn_out.log', 'w')
            for log in py_pritunlServResponse2:
                if re.search(r'(?!52\.39\.62\.8)', log):
                    logfile.write('%s\n' % log)

            logfile.close()
else:

    raise SystemExit

This code visits a website using authentication (the address has been redacted), grabs some text formatted in JSON, and parses two values from the output: "srv_name" and "srv_id". This code then uses the "srv_id" to construct additional HTTP requests to get log files from the server. It then grabs the log files - one for each "srv_id" and names them with the values obtained from "srv_name".

The code as written is syntactically correct, but it's not what I need. As it's written, there is a nested for loop (for srv_id in Server_id:) inside another for loop (for srv_name in Name:).

What I need is to keep the for loop (for srv_id in Server_id:), but "increment" the server name variable (move it to the next value) after each pass though the for loop for srv_id (for srv_id in Server_id:) so that I can have a separate log file with the appropriate server name.

The "fixed" code could look something like:

#!/usr/bin/python

import time
import uuid
import hmac
import hashlib
import base64
import json
import urllib3
import certifi
import datetime
import requests
import re
from datetime import datetime

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED')  # Force certificate check.ca_certs = certifi.where(), #Path to the Certifi bundle.

# Get the status response from pritunl api

BASE_URL = 'https://<REDACTED>'
API_TOKEN = '<REDACTED>'
API_SECRET = '<REDACTED>'
LOG_PATH = '/var/log/logfiles/'


def auth_request(
    method,
    path,
    headers=None,
    data=None,
    ):
    auth_timestamp = str(int(time.time()))


auth_nonce = uuid.uuid4().hex
auth_string = '&'.join([API_TOKEN, auth_timestamp, auth_nonce,
                       method.upper(), path]
                       + (([data] if data else [])))
auth_signature = base64.b64encode(hmac.new(API_SECRET, auth_string,
                                  hashlib.sha256).digest())
auth_headers = {
    'Auth-Token': API_TOKEN,
    'Auth-Timestamp': auth_timestamp,
    'Auth-Nonce': auth_nonce,
    'Auth-Signature': auth_signature,
    }
if headers:
    auth_headers.update(headers)
return http.request(method, BASE_URL + path, headers=auth_headers,
                    body=data)

response1 = auth_request('GET', '/server')
if response1.status == 200:
    pritunlServResponse = json.loads(response1.data)  # print pritunlServResponse# print response1.data

    Name = [y['name'] for y in pritunlServResponse]
    Server_id = [x['id'] for x in pritunlServResponse]


    for srv_id in Server_id:
        response2 = auth_request('GET', '/server/' + srv_id
                + '/output')
        pritunlServResponse2 = json.loads(response2.data)
        py_pritunlServResponse2 = pritunlServResponse2['output']

        print ('value of srv_id: ', srv_id, '\n')
        print ('value of Name: ', Name, '\n')

        logfile = open(LOG_PATH + srv_name + '_vpn_out.log', 'w')
        for log in py_pritunlServResponse2:
            if re.search(r'(?!52\.39\.62\.8)', log):
                logfile.write('%s\n' % log)

        logfile.close()

        <CODE TO ADVANCE "Name">
else:

    raise SystemExit
slantalpha
  • 515
  • 1
  • 6
  • 18
  • Are Name and Server_id the same length and will it always be the same length? And let me get it straight you want to increment Name with server_id 1 to 1? so like server_id goes "up" one you want name to go "up" one? – MooingRawr Sep 08 '16 at 20:12
  • Yes, that's correct. I'd like to have server_id go "up" by one, and also want the name go "up" by one. – slantalpha Sep 08 '16 at 20:17
  • see Craig's answer. it's what I would have done. – MooingRawr Sep 08 '16 at 20:17

2 Answers2

3

Maybe loop it a bit different like this?

for item in pritunlServResponse:
    srv_id = item['id']
    name = item['name']

This way you wouldn't have to create these in the first place

Name = [y['name'] for y in pritunlServResponse]
Server_id = [x['id'] for x in pritunlServResponse]
Jaakko
  • 4,674
  • 2
  • 26
  • 20
2

Use:

for srv_name, srv_id in zip(Name, Server_id):
    ...

Better yet, replace:

pritunlServResponse = json.loads(response1.data)

Name = [y['name'] for y in pritunlServResponse]
Server_id = [x['id'] for x in pritunlServResponse]

for srv_name in Name:
    for srv_id in Server_id:
        ...

with:

for srv_name, srv_id in [(resp['name'], resp['id']) for resp in json.loads(response1.data)]:
    ...
Craig Burgler
  • 1,749
  • 10
  • 19