Python Variable based on port/interface being active or not

jimburt372
Conversationalist

Python Variable based on port/interface being active or not

I have a Python App I am trying to update with a little additional logic.  We are working with a large number of MS120 8 port switches.  Ports 9 & 10 are the uplinks.  My current code is hard coded for 1 uplink (port 9 at present) and then it performs a number of operations that work great, as long as Port 9 is connected and active.  I would like to add some logic to verify if port 9 is online, and THEN perform the operations, or else verify port 10 and proceed.  

 

I am very new to developing, but I have this app that was given to me, and I am trying to improve an already good app, and use it as a learning opportunity, but I am stumped. 

 

In my head, what I think I need, but don't know the proper way to achieve is something like this:

 

port_id = ("9" if portactive=true, else "10" if portactive=false)

 

I can not work out a test to validate if port 9 is active or not.

 

Anyone have suggestions, please? Thanks in advance.

5 Replies 5
RaphaelL
Kind of a big deal
Kind of a big deal

Hi, 

 

Have you tried this endpoint : https://developer.cisco.com/meraki/api-v1/#!get-device-switch-ports-statuses ?

 

It will return you if the port is enabled and is active : 

 

"enabled": true, "status": "Connected",

jimburt372
Conversationalist

Thanks for the reply.  I have looked at it, and "played" with it using the Meraki add-on for Google Sheets that a buddy told me about.  

 

My challenge/limitation is that I do not yet know/understand enough to parse the out put from that get request down to evaluate the status of portId:9, and then use that result to set my variable to 9 or 10 depending on the status returned.  I can run it and look at it cleanly on my screen, and that is basically what I am using as a working solution for the time being.  I search the switchports for port:9-10 and then sort for status disconnected, so that I see which switches are NOT connected on port 9, and skip those in my initial pass of the app.  Then I change the port I process on to 10, and run against the remaining switches.  This is a workable solution, but my goal is to "improve" my process, to avoid the multiple passes, and manual changes in the process.

The switch will only be online (dashboard status) if port 9 OR 10 is connected (or both, the ideal condition), but my app will crash out without much grace if it attempts to process a switch that does not have port 9 connected.  I hope that makes sense.

AutomationDude
Building a reputation

Hey Jim,

 

This is a very basic python problem and should be easily solvable. I would recommend searching on youtube and getting a slightly better understanding of if/then statements. 

 

My guess is that the API returns has a very different response for when the port is online or offline. You can use that response to determine whether the port is online and then decide if you want to execute that part of the code or proceed to port 10.

 

As an example, you can check if the API includes a certain string: https://flexiple.com/python-string-contains

 

For future reference, its best to share a part of your code as this helps us understand and assist you a lot quicker 😉

 

Good luck!

 

jimburt372
Conversationalist

Thanks for the reply.  I do understand that it would be helpful to share the code that I am working with, but....

I am trying to get "permission" to share parts of the code from the team that we had develop it.  I was asked to "keep it private" unless I got approval. 

 

In the code, as it was given to me, we have an external file containing our environmental variable, like username, password, and a variable for "portId" that is currently hard coded to a single port.  Then the main program just processes against that individual port.

 

I have found (since I initially opened this question) that the API returns a status of "isUplink" and that is apparently what I need to key on.  I am now attempting to build a function (possibly not the correct name, so forgive if I misspeak, newbie here) that will have an If/Then (your refernce material will be helpful in this) statement that will evaluate:

 

If port:9 AND "isUplink:"True" then set portId=9, Else portId=10

 

That is my crude, psuedo code if you will of what I THINK will give me the setting I need.  It is simplistic, but I would need to call that for each switch I process, as we typically deploy these roughly 100 at a time, and that is why I was thinking to place this is a function.  Then I could call that function on each repetition of the logic.

 

This project is my first time working with Meraki equipment, and first time attempting to program anything.

AutomationDude
Building a reputation

Hey Jim, please check this out this course I made. I think it should have everything you need:

 

https://go.boundlessdigital.com/meraki-api-with-python-course/registration

 

Get notified when there are additional replies to this discussion.