need a logic to make this function dynamic ..

fosiul01
fosiul01 used Ask the Experts™
on
Hi,
Bellow is a function

hi,
bellow is a static version of  my function.
I am using this : https://github.com/makewhatis/dnsmadeeasy-python/blob/master/dme.py as Base .. and now i am trying to expand  it .

def view_A_records_only():
    dns = dme("xxxxx", "xxxxx")
    print ("A recrod only")
    First_A_record = dns.get_record_byid('fosiul.co.uk','6885096')
    Second_A_record = dns.get_record_byid('fosiul.co.uk','6885097')
    
    for key,value in First_A_record.items():
        if key=='data':
            print key ,value
    
    for key,value in Second_A_record.items():
        if key=='data':
            print key ,value

Open in new window


so the output will be :
data 11.22.33.44
data 55.66.77.88


Now problem is :
we have more then 50 domains.
and I need to modifity this  funtion so that .
its take argument (domain {i} ,key {i} )  example :

def view_A_records_only(domain {i} ,key {i} ):
    dns = dme("xxxxx", "xxxxx")
    print ("A recrod only")

  for ........................ to loop though all the domains and key provided argument :

    First_A_record = dns.get_record_byid('domain {i}','key {i}')
    Second_A_record = dns.get_record_byid('domain {i}','key {i}')
    
    for key,value in First_A_record.items():
        if key=='data':
            print key ,value
    
    for key,value in Second_A_record.items():
        if key=='data':
            print key ,value

Open in new window



so the output willbe :

domian1:
data 11.22.33.44
data 55.66.77.88
domain2:
data 11.22.33.46
data 55.66.77.45
doamin3
data 11.22.33.44
data 55.66.77.88
..............................
............................

can you please help me with this code ??

i would really appreciate ..for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I am not an expert for the networking... What do you mean by domain {i}  (as it is not a Python expression).  Where to you get the 50+ domains and keys?  What data type is used for expressing the domain information and the key information?
Top Expert 2009

Author

Commented:
Hi Glad you response!!

domain {i} : Sorry this new EE will not allow to type "[ ]"
i was try to write , pass Array or dictionary into the function .

but lets try an easy way first ... instead of pushing value into function, lets define that one into the function ,

So I have defined a dictionary like :
domain_key = {'fosiul.co.uk':'5885096','fosiul.co.uk':'6885097','myprojectwork.co.uk':'8958222','myprojectwork.co.uk':'586925'}

Now, if you want to get a REcord ( any record , dont confuses by "A" record) i need to pass that one via this command

First_A_record = dns.get_record_byid('fosiul.co.uk','6885096')

but it will give you an output like  

{u'name': u'', u'type': u'A', u'ttl': 60, u'data': u'11.22.33.55', u'id': 6885096, u'gtdLocation': u'DEFAULT'}

which is a dictionary format. but I only need value associated with  'data'
so i had to use the  for loop to get the data.

which is fine , if you define domain and key by hand.
but i have more then 50 domain . so  i am plaing to create a dictionary in the function for time being.
and some how
combile the for loop and the command (  First_A_record = dns.get_record_byid('fosiul.co.uk','6885096'))  together from dictionaly .

does it make sense ??

domain_key = {'fosiul.co.uk':'5885096','fosiul.co.uk':'6885097','myprojectwork.co.uk':'8958222','myprojectwork.co.uk':'586925'}
    
    dns = dme("xxxx", "xxxxxx")
    print ("A recrod only")
    
    #Bellow lines is getting A record by executing command individualy .

    First_A_record = dns.get_record_byid('fosiul.co.uk','6885096')
    Second_A_record = dns.get_record_byid('fosiul.co.uk','6885097')
    # The output is comming like this 
    #{u'name': u'', u'type': u'A', u'ttl': 60, u'data': u'78.33.39.98', u'id': 6885096, u'gtdLocation': u'DEFAULT'}
    #So I need to pass the output into a loop to get only value associated by 'data'
    for key,value in First_A_record.items():
        if key=='data':
            print key ,value
    
    for key,value in Second_A_record.items():
        if key=='data':
            print key ,value

Open in new window

Top Expert 2009

Author

Commented:
Basically

instead of this command wherw I am inserting domain and key by hand (static)

irst_A_record = dns.get_record_byid('fosiul.co.uk','6885096')

Domain name (fosiul.co.uk)
and key (6885096)

i want to pass that value dynamic from dictionary (example)
domain_key = {'fosiul.co.uk':'5885096','fosiul.co.uk':'6885097','myprojectwork.co.uk':'8958222','myprojectwork.co.uk':'586925'}

if thats easy to do .. or if you have anything else in mind .. you can advise me ..
Top Expert 2009

Author

Commented:
Some thing like bellow , but it does not work ...


def view_A_records_only_v1():
    
    #Define a dictionary with domain and the key I want to get Record.
    
    domain_key = {'fosiul.co.uk':'5885096','fosiul.co.uk':'6885097','fosiul.com':'6885098','fosiul.com':'6885099'}
    
    dns = dme("xxxxx", "xxxxx")
    print ("A recrod only")
    
    #Bellow lines is getting A record by executing command individualy .

    #First_A_record = dns.get_record_byid('fosiul.co.uk','6885096')
    #Second_A_record = dns.get_record_byid('fosiul.co.uk','6885097')
    # The output is comming like this 
    #{u'name': u'', u'type': u'A', u'ttl': 60, u'data': u'78.33.39.98', u'id': 6885096, u'gtdLocation': u'DEFAULT'}
    #So I need to pass the output into a loop to get only value associated by data'
    
    for key,value in domain_key.items():
        record = dns.get_record_byid('key','value')
        for key,value in record.items():
            if key=='data':
                print key ,value

Open in new window

Top Expert 2009

Author

Commented:
note : another problem.. i th ink , i cant have same key 2 times in a dictonary ...

so i cant make dictionary like this

domain_key = {'fosiul.co.uk':'5885096','fosiul.co.uk':'6885097','fosiul.com':'6885098','fosiul.com':'6885099'}

its has 2 key same value ..

what to do ??
You cannot store the pairs domain+key in a dictionary this way. The reason is that only the key (of a dictionary) is always unique, and assignment of another value with the same key rewrites the older value.

Are your pairs like ('fosiul.co.uk', '5885096') stored in a file?  Or do you want to have them hardwired in the Python source?  For the later, you should store them as the list of tuples, for example.  Or you may want to have a list of access keys for a single domain as the value of a dictionary with the domain name used as a key in the dictionary.  You should clarify your situation.
Top Expert 2009

Author

Commented:
i will put them in to db , because

the script will delete Record aswell and when it will add again the key will change .. so  , I need to store the new value into db ( by changing the old one )

domain + key , in to a db

so when i will parse the value in to  "view_A_records_only" function

value will come from DB ..

but for time being .. to test the script, i want to pass it by tuple or list .. what ever you want ..

or shall i create db and get the value from db ..

which one do you think wiill be easy ??
thanks
Top Expert 2009

Author

Commented:
Ok, i have created mysql database .. and table..

mysql> desc domain_key;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int(11)     | NO   | PRI | NULL    | auto_increment |
| domain_name | varchar(50) | YES  |     | NULL    |                |
| domain_key  | int(11)     | NO   |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> select * from domain_key
    -> ;
+----+--------------+------------+
| id | domain_name  | domain_key |
+----+--------------+------------+
|  1 | fosiul.co.uk |    6885096 |
|  2 | fosiul.co.uk |    6885100 |
|  3 | fosiul.com   |    6885098 |
|  4 | fosiul.com   |    6885099 |
+----+--------------+------------+
4 rows in set (0.00 sec)

this should keep this easy ...
Top Expert 2009

Author

Commented:
Ok nearly there.. but now i need your help..
here is the updated one

def view_A_records_only_v1():
    # Open database connection
    db = MySQLdb.connect("xxxx","root","xxxxx","dnsmadeeasy" )
    
    
       
    dns = dme("xxxxx", "xxxxx")
    print ("A recrod only")
    # prepare a cursor object using cursor() method
    cursor = db.cursor()
    # Prepare SQL query to INSERT a record into the database.
    sql = "SELECT * FROM domain_key"
    try:
        # Execute the SQL command
        cursor.execute(sql)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        for row in results:
            
            record = dns.get_record_byid(row[1],row[2])
            #print record
            for record_key,record_value in record.items():
                if record_key=='data':
                    print record_key ,record_value
            
    except:
        print "Error: unable to fecth data"

    # disconnect from server
    db.close()
    

Open in new window



now the output is :

data 22.22.39.222

data 333.70.192.333

data 11.22.33.44

data 55.66.77.55


word "data" is comming from of key  of dictunary ..
Some how i just have to insert the domain name in there
so something like


fosiul.co.uk  11.33.39.98

fosiul.co.uk  22.70.192.104

fosiul.com 11.22.33.44

fosiul.com  55.66.77.55

or

Fosiul.co.uk
A : 11.22.33.33
A : 22.22.225.22

fosiul.com
A : 22.33.55.22
A : 22.33.66.54

what ever way ..
thanks
Some enhancement.  The results = cursor.fetchall() returns the list of the rows.  Then you loop through the list.  It is more natural to loop directly through the rows -- without pushing the rows to the list first.  If you know the row has two elements, you can immediately name the values instead of indexing the row later (instead of printing the record_key).  Then you can use the domain also in the loop below, like this:
...
        # The cursor object is capable to iterate through the rows.
        for domain, key in cursor:
            record = dns.get_record_byid(domain, key)
...

Open in new window

Top Expert 2009

Author

Commented:
ok bellow looks allright to me ..

print ("A recrod only")
    # prepare a cursor object using cursor() method
    cursor = db.cursor()
    # Prepare SQL query to INSERT a record into the database.
    sql = "SELECT * FROM domain_key"
    try:
        # Execute the SQL command
        cursor.execute(sql)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        for row in results:
            
            record = dns.get_record_byid(row[1],row[2])
            #print record
            
            for record_key,record_value in record.items():
                if record_key=='data':
                    print "%s : %s  " % (row[1], record_value) 
            #domain = row[1]
            #key = row[2]
            # Now print fetched result
            #print "domain=%s,key=%s" % (domain, key )
    except:
        print "Error: unable to fecth data"

    # disconnect from server
    db.close()

Open in new window


so the output is comming :

A recrod only

fosiul.co.uk : 11.33.39.11

fosiul.co.uk : 11.70.192.11

fosiul.com : 11.22.33.44

fosiul.com : 55.66.77.55


is there any way to make the output like this :

fosiul.co.uk:
 11.33.39.11

11.70.192.11

fosiul.com :
11.22.33.44

55.66.77.55



also :
when i use this

for domain, key in cursor:

its does not get any output ..
replace the line 14  (with the same indentation as the line 13)

    print '\n' + row[0] + ':'

replace the line 18
   
    print record_value

with the same indentation as the original line.

> for domain, key in cursor:
> its does not get any output ..


Does it get into the loop?  Does it report some error?  If used, you have to use domain instead of row[0] and key instead of row[1]
Top Expert 2009

Author

Commented:
Hi,
as you said, i have changed.. but the output i am getting

def view_A_records_only_v1():
   
          
    dns = dme("xxxx", "xxx")
    print ("A recrod only")
    # prepare a cursor object using cursor() method
    cursor = db.cursor()
    # Prepare SQL query to INSERT a record into the database.
    sql = "SELECT * FROM domain_key"
    try:
        # Execute the SQL command
        cursor.execute(sql)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        for row in results:
            
            record = dns.get_record_byid(row[1],row[2])
            #print record
            print '\n' + row[1] + ':'
            
            for record_key,record_value in record.items():
                if record_key=='data':
                    print record_value 
            #domain = row[1]
            #key = row[2]
            # Now print fetched result
            #print "domain=%s,key=%s" % (domain, key )
    except:
        print "Error: unable to fetcth data"

    # disconnect from server
    db.close()
    

Open in new window



A recrod only


fosiul.co.uk:
44.55.39.98


fosiul.co.uk:
44.70.192.104


fosiul.com:
11.22.33.44

fosiul.com:
55.66.77.55
I see.  I do not know what the record is.  Isn't it possible to replace the loop on the lines 21 - 23
            for record_key,record_value in record.items():
                if record_key=='data':
                    print record_value 

Open in new window


by the following?
print record['data']

Open in new window


If you want to group more IP addresses with one name only, then you have basically two possibilities:
1)  build a dictionary where the name is the key and the list of IP addresses is the value,
2)  if the rows are sorted by the name, print the name only when it changes.
Top Expert 2009

Author

Commented:
ok if i do this

try:
        # Execute the SQL command
        cursor.execute(sql)
        # Fetch all the rows in a list of lists.
        results = cursor.fetchall()
        for row in results:
            
            record = dns.get_record_byid(row[1],row[2])
            #print record
            
            print record['data']
            #print '\n' + row[1] + ':'
            
            #for record_key,record_value in record.items():
            #    print record['data']
                #if record_key=='data':
                #    print record_value 
            #domain = row[1]
            #key = row[2]
            # Now print fetched result
            #print "domain=%s,key=%s" % (domain, key )
    except:
        print "Error: unable to fetcth data"

    # disconnect from server
    db.close()

Open in new window


i get this :

A recrod only

22.33.39.22

22.70.192.22

11.22.33.44

55.66.77.55


so first 2 is for fosiul.co.uk and last 2 is for fosiul.com
Top Expert 2009

Author

Commented:
i think , if we can insert the value comming from record['data']   and row[1]

in to another dictionary ,
then sort or something from that dictionary , it will that result ..
Can you print the whole row to show me what the row contains?

Yes. If row[1] contains the domain name then you can do something like (try the change in the for loop):
try:
        # Execute the SQL command
        cursor.execute(sql)
        d = {}       # dictionary for domain --> list of IPs
        for row in cursor:
            record = dns.get_record_byid(row[1],row[2])
            lst = d.setdefault(row[1], [])     # existing or new empty list for the domain name       
            lst.append(record['data'])         # append the next IP
    except:
        print "Error: unable to fetch data"

    # disconnect from server
    db.close()
    
    # Display the collected data from the d dictionary.
    for domain in sorted(d):
        print
        print domain + ':'
        for ip in d[domain]:
            print ip

Open in new window

Top Expert 2009

Author

Commented:
owwww thats perfect ..

the output is

A recrod only





fosiul.co.uk:
11.33.11.11
111.70.11.104

fosiul.com:
11.22.33.44
55.66.77.55

dont know .. there is big GAP between A Record only and the output

when i was Executing the script, its like its trying to execute the loop and it did not match anything so it made a empty space  then it found 4 record ..
I do not know what the code around is.  Isn't there any empty print command or print command that prints some empty content of a variable?
Top Expert 2009

Author

Commented:
i think its the output of
record = dns.get_record_byid('domainname','key'])

Original author usingi json .. dont know if thats has any impact or not ..

but is there any way to remove the empty space from the output of

" for domain in sorted(d):"

??
> but is there any way to remove the empty space from the output of
> " for domain in sorted(d):"


The empty space does not come from here.  The sorted(d) returns sorted keys of the dictionary (the domain names).  The empty print just below the for line only separates the next record.  You can move it below the record, but it will not make a big difference.

The truth is that the https://github.com/makewhatis/dnsmadeeasy-python/blob/master/dme.py contains a print command producing potentially empty line -- see the lines 129, 57, 59.  Do comment the last one out.
Top Expert 2009

Author

Commented:
thats great
thanks
after comment that one out
its does not show any blank

I will create anther question shortly

it will be samet hing
but insert into db ..

i need to finish this by tomorrow as i need the tools tomorrow
thanks
You can continue here.  Basically, you just update the table with the values of the domain, and IP.  Or you can do it from the d dictionary, or you can update it on the fly instead of filling the d dictionary.
Top Expert 2009

Author

Commented:
thanks great ..
i never use python with mysql before.. today is first time.. so please tell me if my way of wirtting is wrong ..

here, what i am trying to do is :
get all the dns record from dnsmadeeasy for a particular domain  and insert into a table .


def Import_dns_record():
    # Open database connection
    db = MySQLdb.connect("xxxx","xxxx","xxx","dnsmadeeasy" )
    cursor = db.cursor()      
    dns = dme("xxxx", "xxxxx")
     
    [b]records = dns.get_records('fosiul.co.uk') [/b]  ------------------------------- 
    for record in records:
        
        print record['data']
        sql = "INSERT INTO test_dns_record(domain_id,record_name, type, ttl, data,dyndns_id,gtdlocation) \
        VALUES [b]([u]'1'[/u][/b], '%s', '%s', '%s', '%s', '%s', '%s' )" % (record['name'], record['type'],record['ttl'], record['data'],record['id'],record['gtdLocation'])
        cursor.execute(sql)
        db.commit()
        

Open in new window


here , i am inserting fosiul.co.uk by hand..also values (1) by hand  but i want to pass those  2 values from from test_domain database....

mysql> desc test_dns_record;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| domain_id   | int(2)      | YES  |     | NULL    |       |
| record_name | varchar(20) | YES  |     | NULL    |       |
| type        | varchar(3)  | YES  |     | NULL    |       |
| ttl         | varchar(3)  | YES  |     | NULL    |       |
| data        | varchar(15) | YES  |     | NULL    |       |
| dyndns_id   | varchar(20) | YES  |     | NULL    |       |
| gtdlocation | varchar(15) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> desc test_domains
    -> ;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int(11)     | NO   | PRI | NULL    | auto_increment |
| domain_name | varchar(50) | YES  |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


does it make sense ??
Top Expert 2009

Author

Commented:
wait, let me try to write few more lines
then i will post it here again ..
wait..
Top Expert 2009

Author

Commented:
got it working ..

i never use mysql with python , i am doing this today only.. so if you find any bad coding please advise .....

def Import_dns_record():
    # Open database connection
    db = MySQLdb.connect("xxx","root","xxxx","xxxx" )
    cursor = db.cursor()   
      
    dns = dme("xxxx", "xxxx")
    sql_test_domains="SELECT * FROM test_domains"
    
    
    cursor.execute(sql_test_domains)
    for row in cursor:
        records = dns.get_records(row[1])
        for record in records:
            sql = "INSERT INTO test_dns_record(domain_id,record_name, type, ttl, data,dyndns_id,gtdlocation) VALUES ('%d', '%s', '%s', '%s', '%s', '%s', '%s' )" % (row[0],record['name'], record['type'],record['ttl'], record['data'],record['id'],record['gtdLocation'])
            cursor.execute(sql)
            db.commit()

Open in new window


Now i  need help with my delete dns entry ... which i th ink might be bit hard...


let me figured out few this.. then i will write again...

Please stay with me..now i will need you help
Top Expert 2009

Author

Commented:
ok basically the logic is : have a look at tables

mysql> select  * from test_dns_record;
+-----------+-------------+------+------+----------------+-----------+-------------+
| domain_id | record_name | type | ttl  | data           | dyndns_id | gtdlocation |
+-----------+-------------+------+------+----------------+-----------+-------------+
|         1 |             | A    | 60   | 78.xx.39.xx    | 6885096   | DEFAULT     |
|         1 |             | A    | 10   | 194.xxx.192.xx | 6885101   | DEFAULT     |
|         2 |             | A    | 10   | 194.xx.192.xx | 6885104   | DEFAULT     |
|         2 |             | A    | 10   | 78.xx.39.xx7    | 6885105   | DEFAULT     |
+-----------+-------------+------+------+----------------+-----------+-------------+
4 rows in set (0.00 sec)

mysql> select * from test_domains;
+----+--------------+
| id | domain_name  |
+----+--------------+
|  1 | fosiul.co.uk |
|  2 | fosiul.com   |
+----+--------------+
2 rows in set (0.00 sec)


we have 2 set of ISP and 2 set of ips.
one starts with 194 and other one starts with 78

now, when isp 1(194) will be down, We need to delete the A record which is related to 194 ( you will be able to see from the table)

the basic command is to delete A record is :


def Set_A_records_to_Daemon():
    dns = dme("xxx", "xxx")
    record = dns.delete_record_byid('fosiul.co.uk','6885101')
   record = dns.delete_record_byid('fosiul.m','6885104')


but when i will try to do this dynamically.

i will have to look in to test_domains table + test_dns_records and I need to delete those recrod where dyndns_id starts with 194 ....

i think it will be pure mysql ..
let me try ..
i will try after going home..
after 2 hrs..
will come back ..
thanks

but if have any good way to do .. please let me know ..
I am not an expert at MySQL.  Anyway, the following command...
sql = "INSERT INTO test_dns_record(domain_id,record_name, type, ttl, data,dyndns_id,gtdlocation) VALUES ('%d', '%s', '%s', '%s', '%s', '%s', '%s' )" % (row[0],record['name'], record['type'],record['ttl'], record['data'],record['id'],record['gtdLocation'])

Open in new window


...can be converted to a separate template with named placeholders.  It can also be broken to multiline string to ease the maintenance later:
insert_tpl = """INSERT INTO test_dns_record(domain_id,
                                            record_name, 
                                            type, 
                                            ttl,
                                            data, 
                                            dyndns_id, 
                                            gtdlocation) 
                                    VALUES ('%(domain_id)s', 
                                            '%(name)s', 
                                            '%(type)s', 
                                            '%(ttl)s',
                                            '%(data)s', 
                                            '%(id)s', 
                                            '%(gtdLocation)s')"""
...
    for row in cursor:
        records = dns.get_records(row[1])
        for record in records:
            record['domain_id'] = row[0]     # check, whether this works
            cursor.execute(insert_tpl % record)  # values from the dict to the template
            db.commit()

Open in new window


Possibly  you can add a valid column (or the name like "alive") with a boolean value and just mark the records that cannot be used instead of removing them.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial