skip to Main Content

I need to copy the contents of a .csv, that gets dumped from SQL each night to a folder location on my Ubuntu server that contains 100s of IP addresses to an area within a .conf file.
The csv puts all the IP addresses on the first row, separated with a space between each IP:

192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4

When I open the csv in Excel it looks like:

['192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4']

And I need it to put into a file called something like routers.conf like this on row 2, but I need it be formatted like below and include the " instead of ‘around the IP, add the :161 etc and inject into row 2 and replace what is there:

  [[inputs.snmp]]
    agents ["192.168.0.1:161","192.168.0.2:161","192.168.0.3:161","192.168.0.4:161"]
    version = 2
    community = "publicblah"
    interval = "120s"
    timeout = "5s"
    retries = 0

Could Python do this?

I’m currently using this to open and view it:

    with open("ip.csv", 'r', encoding='utf-8-sig') as infile:
        reader = csv.reader(infile, delimiter=" ")
        header = next(reader)
        print(header)

I did have a play with the outfile = open and outfile.write options, but get lost as this just writes a new .csv.

Any hope to parse this data, format it, and inject/replace the data in another file would be most welcome.

2

Answers


  1. This should do the trick, let me know if you need any help!

    import csv
    
    # Step 1: Read the IP addresses from the CSV file
    with open("ip.csv", 'r', encoding='utf-8-sig') as infile:
        reader = csv.reader(infile, delimiter=" ")
        ip_addresses = next(reader)
    
    # Step 2: Format the IP addresses
    formatted_ips = [f'"{ip}:161"' for ip in ip_addresses]
    
    # Step 3: Join the IP addresses
    ip_string = ', '.join(formatted_ips)
    ip_string = f'agents = [{ip_string}]'
    
    # Step 4: Read the routers.conf file
    with open('routers.conf', 'r') as file:
        lines = file.readlines()
    
    # Step 5: Replace the second line
    lines[1] = ip_string + 'n'
    
    # Step 6: Write the modified lines back into the file
    with open('routers.conf', 'w') as file:
        file.writelines(lines)
    
    Login or Signup to reply.
  2. You don’t have to worry about the single quotation mark as you see, because a string like "abcdef" or a character like "a" doesn’t contain the QUOTATION MARK.
    Python uses quotation marks to remind you that this is a string (or a character). Below is my code.

    FILE_NAME = "ip.csv" #Your csv file
    infile = open(FILE_NAME, "r", encoding="utf-8-sig")
    IPs = infile.readlines()[0].split(" ") # Now Ips is a list:  ['192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4']
    infile.close()
    ips_str = '['
    for ip in IPs:
        ips_str = ips_str +'"' +ip + ':161",'
    ips_str = ips_str[0:-1:1] + "]" 
    #Now ips_str is: '["192.168.0.1:161","192.168.0.2:161","192.168.0.3:161","192.168.0.4:161"]'
    # But a string or character doesn't contain the QUATATION MARKS
    output_text = '[[inputs.snmp]]ntagents ' + ips_str +'n'+'tversion = 2ntcommunity = "publicblah"ntinterval = "120s"nttimeout = "5s"ntretries = 0'
    OUTPUT_FILE = "config.conf" #REPLACE it with your config file name
    
    with open(OUTPUT_FILE, "w") as out:
        out.write(output_text)
    

    More about string in Python, or in some other languages:

    • You could use len() to get the length of a string.
      e.g.,
    string1 = 's'
    print(string1)
    

    You’ll get 1 rather than 3, so obviously the quotation marks aren’t included.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search