In writing a program for work I need to determine if a BGP ASN is private or private. If it is private I want to print it out and if it’s public I don’t want anything to happen. Here’s how I did it-
def checkASN(asn_number): asn16_start = 64512 asn16_end = 65534 asn32_start = 4200000000 asn32_end = 4294967294 if asn16_start <= asn_number <= asn16_end: print(asn_number) elif asn32_start <= asn_number <= asn32_end: print(asn_number) else: pass
First we create a function call checkASN and pass it the variable asn_number. That variable is assigned in a different part of the code. If you run this in the Python interpreter, you can simply assign it with something like ‘asn_number = 656463’. We then set four (local) variables in the function (asn16_start, asn16_end, asn32_start and as32_end). They are the beginning and ending private ASN numbers for 32 bit and 64 bit respectively. Next is a simple if-else block check to see if our asn_number fits between the start and end ASN numbers (the variables). If it does we print it out. If it doesn’t fall in the range that means it is a public ASN and nothing should be printed out.
>>>asn_number = 4200000860 >>>checkASN(asn_number) 4200000860 >>>asn_number = 900 checkASN(asn_number) >>> >>>asn_number = 64700 >>>checkASN(asn_number) 64700