0

I have the following code:

#!/usr/bin/python

import time, uuid, hmac, hashlib, base64, 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://www.vpn.trimble.cloud:443'
API_TOKEN = 'gvwrfQZQPryTbX3l03AQMwTyaE0aFywE'
API_SECRET = 'B0vZp5dDyOrshW1pmFFjAnIUyeGtFy9y'
LOG_PATH = '/var/log/developer_vpn/'
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, srv_id in zip(Name, 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" and saves them on the local system.

I want to do some additional grep-style processing before the files are written to the local system. Specifically I'd like to exclude any text exactly containing "52.39.62.8" from being written. When I run the code above, it looks like the regex is not being processed as I still see "52.39.62.8" in my output files.

2 Answers 2

1

If the IP address is always flanked by specific characters, e.g.: (52.39.62.8):, you can use in for exact contains:

if '(52.39.62.8):' not in log:
    logfile.write(log + '\n')
Sign up to request clarification or add additional context in comments.

1 Comment

The brackets are a good guard against false positives, but the original code did not attempt to avoid them, so if '52.39.62.8' not in log: would accomplish what the OP seems to have had in mind, simply and elegantly. (Of course, the program will then also not print e.g. 152.39.62.8 or 52.39.62.81.)
0
re.search(r'(?!52\.39\.62\.8)', log)

You're matching any empty string that is not followed by the ip address - every string will match, as this will match the end of any string.

reverse your logic and output the line to the log only if re.search for the ip address comes back as None.

if re.search(r'(?<!\d)52\.39\.62\.8(?!\d)', log) is None:
    logfile.write("%s\n" % log)

note that this also includes it's own negative look-behind and look-ahead assertions to ensure no digits precede or follow the ip address.

1 Comment

full disclosure: this was done on my phone and so has not been tested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.