How to use netmask_to_cidr method in avocado

Best Python code snippet using avocado_python

ipDiscovery.py

Source:ipDiscovery.py Github

copy

Full Screen

...70 return True71 except ValueError:72 pass73 return False74def netmask_to_cidr(netmask):75 return sum([bin(int(x)).count('1') for x in netmask.split('.')])76def getDescription(router,community,id):77 errorIndication, errorStatus, errorIndex, varBinds = next(78 getCmd(SnmpEngine(),79 CommunityData(community),80 UdpTransportTarget((router, 161)),81 ContextData(),82 ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.2.'+str(id))),lexicographicMode=False)83 )84 if errorIndication:85 #print(errorIndication)86 #break87 return "NoDescription"88 elif errorStatus:89 #print('%s at %s' % (errorStatus.prettyPrint(),90 #errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))91 #break92 return "No Description"93 else:94 for varBind in varBinds:95 return varBind[1]96def print_html_header():97 print ("<html><head>")98 print ("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.10.5/css/jquery.dataTables.css\">")99 print ("</head><body>")100 print ("<table id=\"ipInventory\" class=\"cell-border compact\" cellspacing=\"0\" width=\"100%\"><thead><tr><th>Router</th><th>Id-Name</th><th>Ip/Mask</th></tr></thead>")101 print ("<tfoot><tr><th>Router</th><th>Id-Name</th><th>Ip/Mask</th></tr></tfoot><tbody>")102def print_body(rtype):103 hosts=zapi.host.get(selectInventory=True)104 if not hosts:105 print ("Empty list.")106 exit()107 for s in zapi.host.get(output="extend", groupids=[groupid], filter={"status": 0}):108 name=(s['name'])109 hostid=(s['hostid'])110 for i in zapi.hostinterface.get(hostids=[hostid],filter={"type": 2}):111 ip=(i['ip'])112 name=name.replace(" ","_")113 gen_report(ip,community,name,rtype)114def print_html_footer():115 print ("</tbody></table>")116 print (" <script type=\"text/javascript\" charset=\"utf8\" src=\"http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js\"></script>")117 print (" <script type=\"text/javascript\" charset=\"utf8\" src=\"http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.10.5/jquery.dataTables.min.js\"></script>")118 print ("<script>")119 print (" $(function(){")120 print (" $(\"#ipInventory\").dataTable(")121 print ("{")122 print ("\"rowCallback\": function( row, data, index ) {")123 print (" if ( \"5\" == \"5\" )")124 print (" {")125 print (" $('td', row).css('background-color', '"+tableColor+"');")126 print (" }")127 print ("}")128 print ("}")129 print (");")130 print (" })")131 print (" </script>")132 print ("</body></html>")133#Delete all existing discovery rules that match ROUTER-* pattern134def delete_existing_rules(name):135 rname=name+"-*"136 actual=zapi.drule.get(search={"name": rname}, searchWildcardsEnabled=True)137 for a in actual:138 zapi.drule.delete(a['druleid'])139#Main routine140def gen_report(router, community, name, rtype):141 global nameCounter142 rcounter=0143 ipRange=""144 if (rtype == "api"):145 delete_existing_rules(name)146 for (errorIndication,147 errorStatus,148 errorIndex,149 varBinds) in nextCmd(SnmpEngine(),150 CommunityData(community),151 UdpTransportTarget((router, 161)),152 ContextData(),153 ObjectType(ObjectIdentity('1.3.6.1.2.1.4.20.1.1')),154 ObjectType(ObjectIdentity('1.3.6.1.2.1.4.20.1.2')),155 ObjectType(ObjectIdentity('1.3.6.1.2.1.4.20.1.3')),156 lexicographicMode=False):157 if errorIndication:158 #print(errorIndication)159 break160 elif errorStatus:161 #print('%s at %s' % (errorStatus.prettyPrint(),162 # errorIndex and varBinds[int(errorIndex)-1][0] or '?'))163 break164 else:165 zipped = zip(varBinds[0::3],varBinds[1::3],varBinds[2::3])166 for ((t,ip),(t,id),(t,mask)) in zipped:167 description=getDescription(router,community,id)168 if (rtype == "html"):169 print ("<tr><td>"+str(name)+"</td><td>"+str(id)+"-"+str(description)+"</td><td>"+rfc1902.IpAddress.prettyPrint(ip)+"/"+str(netmask_to_cidr(rfc1902.IpAddress.prettyPrint(mask))) +"</td></tr>")170 elif (rtype == "csv"):171 print (str(name)+","+str(id)+"-"+str(description)+","+rfc1902.IpAddress.prettyPrint(ip)+"/"+str(netmask_to_cidr(rfc1902.IpAddress.prettyPrint(mask))))172 elif ((rtype == "api") and (netmask_to_cidr(rfc1902.IpAddress.prettyPrint(mask))!=32) and (netmask_to_cidr(rfc1902.IpAddress.prettyPrint(mask)) >=16 ) and isInIncludeRange(rfc1902.IpAddress.prettyPrint(ip)+"/"+str(netmask_to_cidr(rfc1902.IpAddress.prettyPrint(mask)))) and not (isInExcludeRange(rfc1902.IpAddress.prettyPrint(ip)+"/"+str(netmask_to_cidr(rfc1902.IpAddress.prettyPrint(mask))))) ):173 if(rcounter==0):174 ipRange=rfc1902.IpAddress.prettyPrint(ip)+"/"+str(netmask_to_cidr(rfc1902.IpAddress.prettyPrint(mask)))175 else:176 ipRange=ipRange+","+rfc1902.IpAddress.prettyPrint(ip)+"/"+str(netmask_to_cidr(rfc1902.IpAddress.prettyPrint(mask)))177 rcounter=rcounter+1178 if (rcounter==maxNetPerRule):179 update_discovery(name,ipRange)180 ipRange=""181 rcounter=0182 if ((rtype == "api") and (rcounter != 0)):183 update_discovery(name,ipRange)184 ipRange=""185 rcounter=0186 nameCounter=0187def update_discovery(dname,ipRange):188 global nameCounter189 template=zapi.drule.get(output="extend", selectDChecks="extend", filter={"name": templateDiscoveryRule})190 if not template:...

Full Screen

Full Screen

test_addr.py

Source:test_addr.py Github

copy

Full Screen

...20 self.assertTrue(SpecialEthAddr.LLDP_MULTICAST.value.is_multicast)21 self.assertEqual(str(SpecialEthAddr.PAE_MULTICAST.value), "01:80:c2:00:00:03")22 def testUtils(self):23 mask = IPv4Address("255.255.252.0")24 l = netmask_to_cidr(mask)25 self.assertEqual(l, 22)26 self.assertEqual(mask, cidr_to_netmask(l))27 self.assertEqual(infer_netmask(IPAddr("10.0.0.1")), 8)28 self.assertEqual(infer_netmask(IPAddr("192.168.1.24")), 24)29 self.assertEqual(infer_netmask(IPAddr("149.43.80.25")), 16)30 self.assertEqual(infer_netmask(IPAddr("0.0.0.0")), 0)31 self.assertEqual(str(cidr_to_netmask(24)), "255.255.255.0")32 addr,netbits = parse_cidr("149.43.80.25/22", allow_host=True)33 self.assertEqual(addr, IPv4Address("149.43.80.25"))34 self.assertEqual(netbits, 22)35 self.assertEqual(netmask_to_cidr("255.255.0.0"), 16)36 with self.assertRaises(AddressValueError) as _:37 netmask_to_cidr("320.255.255.0")38 with self.assertRaises(RuntimeError) as _:39 netmask_to_cidr(2**32+1000)40 with self.assertRaises(RuntimeError) as _:41 parse_cidr("1.2.3.4/40")42 with self.assertRaises(RuntimeError) as _:43 parse_cidr("1.2.3.4/40")44 with self.assertRaises(RuntimeError) as _:45 parse_cidr("1.2.3.1/24")46if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful