0

I have to create a Python script which connects to a MS-SQL DB (This is completed and extracting the information I need), and extracts a list of IP addresses from the table and use it to complete the server path to search an specific file on each server and then do modifications on 5 parameters on this file, then jump to next server and do the same changes and so on. Any help in the iteration creation will be more than welcome At the moment what I have so far is this:

import pyodbc
####   --->>>Connection to SQL Server
conn = pyodbc.connect('Driver={SQL Server};'
                          'Server=****Mi servidor****;'
                          'UID=***Usuario****;'
                          'PWD=***password***;'
                          'database=voiceData;'
                          'Trusted_Connection=yes;'
                          )

cursor = conn.cursor()
cursor.execute('EXECUTE dbo.GET_localOfficeAvayaFilePaths')


####  --->>>Changes on the 46xxsettings.txt file
for row in cursor:
    print('', row[3])
    string_map = {'## SET SLMSRVR': 'SET SLMSRVR 192.168.1.1', 
                  '## SET SLMSTAT 1': 'SET SLMSTAT 1',
                  '## SET SLMPERF 1': 'SET SLMPERF 1',
                  '## SET SLMCAP 1': 'SET SLMCAP 2',
                  'SET SLMCTRL 1': 'SET SLMCTRL 1'}

    for line in cursor.readlines():
        if line.startswith('## SET'):
            for original, new in string_map.items():
                if original in line:
                    line = new
                    break
        print(line.strip())

cursor.close()
del cursor
Yogev Neumann
  • 2,099
  • 2
  • 13
  • 24
  • Hi @gonzalo-vargas if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Yogev Neumann Apr 26 '19 at 15:38

1 Answers1

0

I'm not completely sure what you are trying to achieve,

If you are querying a database to get a list of 5 IP address, Suppose you saved it into a variable called 'ips_list',

Then, as you said, you would like to add a constant path to the IP address and download the file,

Then edit (I assume) 5 lines in that configuration file,

You didn't mention what are you going to do with that file, for simplicity I just saved it with appropriate name.

Take a look at the following code:

import urllib.request

FILE_PATH = "http://{}/path/to/file.ini"
STRING_MAP = {"## SET SLMSRVR": "SET SLMSRVR 192.168.1.1", 
        "## SET SLMSTAT 1": "SET SLMSTAT 1",
        "## SET SLMPERF 1": "SET SLMPERF 1",
        "## SET SLMCAP 1": "SET SLMCAP 2",
        "SET SLMCTRL 1": "SET SLMCTRL 1"}

def func(ips_list):
    for ip_address in ips_list: # Assume: type(ip_address) == str
        conf_file = urllib.request.urlopen(FILE_PATH.format(ip_address)).read() # You should handle exceptions
        with open("./conf_files/{}/.ini".format(ip_address), "w", encoding="utf8") as outfile:
            for line in conf_file.splitlines():
                for key in STRING_MAP:
                    if line.startswith(key):
                        line = STRING_MAP[key]
                        break
                outfile.write(line)
Yogev Neumann
  • 2,099
  • 2
  • 13
  • 24