Pythonic way to use a variable as any integer

Multi tool use
Multi tool use


Pythonic way to use a variable as any integer



I am having trouble with the parameter of an SNMP query in a python script. An SNMP query takes an OID as a parameter. The OID I use here is written in the code below and, if used alone in a query, should return a list of states for the interfaces of the IP addresses I am querying onto.



What I want is to use that OID with a variable appended to it in order to get a very precise information (if I use the OID alone I will only get a list of thing that would only complexify my problem).



The query goes like this:


oid = "1.3.6.1.4.1.2011.5.25.119.1.1.3.1.2."
variable = "84.79.84.79"
query = session.get(oid + variable)



Here, this query will return a corrupted SNMPObject, as in the process of configuration of the device I am querying on, another number is added, for some reason we do not really care about here, between these two elements of the parameter.



Below is a screenshot showing some examples of an SNMP request that only takes as a parameter the OID above, without the variable appended, on which you may see that my variable varies, and so does the highlighted additional number:



SNMP interface states query responses



Basically what I am looking for here is the response, but unfortunately I cannot predict for each IP address I am querying what will that "random" number be.



I could use a loop that tries 20 or 50 queries and only saves the response of the only one that would have worked, but it's ugly. What would be better is some built-in function or library that would just say to the query:



"SNMP query on that OID, with any integer appended to it, and with my variable appended to that".



I definitely don't want to generate a random int, as it is already generated in the configuration of the device I am querying, I just want to avoid looping just to get a proper response to a precise query.



I hope that was clear enough.





Regex is definitely inappropriate here. Perhaps you only need to randomize a number and insert it in between.
– nhahtdh
Jul 3 at 7:41






I understand that regex isn't made for that purpose, I am just trying to find a practical way to avoid looping 100 or 200 times in order to get a proper query response.
– Ugohihi
Jul 3 at 8:02





The reason we use OIDs is that they are strictly defined. If you don't understand the OID you are trying to manipulate, the solution is to fix your lack of understanding.
– tripleee
Jul 3 at 19:42





Having said that, if you cannot predict the number of a locally generated resource, and the device doesn't offer a way to query for it programmatically - through another nearby OID, perhaps? - looping until you find the number is probably your only recourse.
– tripleee
Jul 3 at 19:44





There is no way in standard SNMP to say "I know you have only one of these, so just give me that".
– tripleee
Jul 3 at 19:46





1 Answer
1



Something like this should work:


from random import randint
variable = "84.79.84.79"
numbers = "1.3.6.1.4.1.2011.5.25.119.1.1.3.1.2"
query = session.get('.'.join([numbers, str(randint(1,100)), variable])





I actually don't want to generate a random number as it already exists, the problem is that it can vary from one query to another. I am just trying to tell the program to just forget about that random number and consider it is a number between 1 and, let's say, 100.
– Ugohihi
Jul 3 at 7:53





@Ugohihi replace randint(1,100) with any number you like =)
– lenik
Jul 3 at 7:55


randint(1,100)





I wouldn't ask if it was that easy. Thanks anyway.
– Ugohihi
Jul 3 at 7:59





@Ugohihi it's absolutely unclear, what you're trying to do and what is the problem, please, provide more details if you need the better answer
– lenik
Jul 3 at 8:04





From what I understand, the middle part is not random but he can't access to it. So he doesn't want to randomize this part. For me the best way to achieve is to try every number between a min and a max, and try catch when it fails
– Exomus
Jul 3 at 8:19






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

OQNiFqoUzqeuOXjntwgz,fX57XXpxP Fi8SFg,SYJbGFtlD
OuSWYY,TjKPkm6fA7o

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications