0

In a bit of a bind here. I am attempting to dump a dictionary called DATA into a json file. I have a few keys in this dictionary, one of them being called options. I'm populating this dictionary with windows environment variables although that shouldn't matter too much. Here is my code:

TOOL = sys.argv[1]
TARGET = sys.argv[2]
HASH = sys.argv[3]
TESTCASE = sys.argv[4]
OPTIONS = sys.argv[5]

DATA = {'test': TOOL.replace("'", ""), 'target': TARGET.replace("'", ""),
        'HASH': HASH.replace("'", ""),
        'options': OPTIONS.replace("'", ""),
        'TESTCASE_EXECUTIONRECORD_NAME': TESTCASE.replace("'", "")}


def write_to_json():
    """Serialize cli args and tool options in json format.

    Write stream to json file.
    """
    with open(r'C:\Users\tommy12\PycharmProjects\SomethingNew\envs.json', 
    'w') as json_file:
    json.dump(DATA, json_file)


write_to_json()

I'll run the program with the following command

python jsonprogram.py %TOOL% %TARGET% 434999 test %OPTIONS%

Here are the environment variables tool = 'nmap', target = 'scanme.nmap.org' and options = '-cA "-sS -p1-1000 "'

Everything will format correctly expect for the options. For my json option value, I will have just -cA. I've tried using quotes in different places but nothing has helped. Does anyone know how to keep options in tact? I need the options format to be -cA "-sS -p-1000 " with the trailing space after 1000. The double quotes are necessary as well.

UCProgrammer
  • 517
  • 7
  • 21

2 Answers2

1

Double quoting your environment variables should preserve spaces (I tested it in linux and it works):

python jsonprogram.py "%TOOL%" "%TARGET%" 434999 test "%OPTIONS%"

This other answer (https://stackoverflow.com/a/1851129/10026557) seems to confirm.

cornacchia
  • 473
  • 2
  • 9
  • Will that interact oddly with double-quotes contained within the variable value, though? – glibdud Jul 18 '18 at 15:46
  • ya it does. I added double quotes to the variable itself and that worked too but your way is better. Thanks – UCProgrammer Jul 18 '18 at 15:48
  • @glibdud I'm passing all of these over to another machine via a paramiko script I created. I'll just strip end quotes when I load the json file over there – UCProgrammer Jul 18 '18 at 15:49
0

I am not sure if this is the perfect answer for your problem but I made this simple change in inputs and it worked. original input

python test.py nmap scanme.nmap.org 434999 cA "-sS -p1-1000 "

changed input

python test.py 'nmap' 'scanme.nmap.org' '434999' 'test' 'cA "-sS -p1-1000 "'