Jan-31-2024, 10:18 AM
Hello,
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. Well I'm happy to create a new file rather than try and open an existing file and insert.
I'm a novice at Python, but getting this working will help. VScode is amazing to.
The csv puts all the IP addresses on the first column:
I'm currently using this to open and view it:
I did have a play with the outfile = open and outfile.write options, but get lost as this just writes a new .csv.
Any help to copy this data, format it, and create a new file with the addition config file would be most welcome.
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. Well I'm happy to create a new file rather than try and open an existing file and insert.
I'm a novice at Python, but getting this working will help. VScode is amazing to.
The csv puts all the IP addresses on the first column:
ip_address 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4 ect...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. Plus add the rest of the config.
[[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 = 0Could 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've been reading through - https://realpython.com/python-csv/
I did have a play with the outfile = open and outfile.write options, but get lost as this just writes a new .csv.
Any help to copy this data, format it, and create a new file with the addition config file would be most welcome.