Python: Creating upm profile with script

  • 0
  • 1
  • Problem
  • Updated 2 years ago
  • Solved
I'm trying to write a python script to configure new switches and right now I'm struggling with creating UPM profiles through the script as running create upm profile [profilename] opens an interactive session, are there ways to make the python interpreter direct its output into this session or alternatively other ways to creating / editing upm profiles  without calling this interactive session?   
Photo of Nikolai Andre Ek Ovesen

Posted 2 years ago

  • 0
  • 1
Photo of Matthew Helm

Matthew Helm, Employee

  • 1,852 Points 1k badge 2x thumb
You could use a subprocess shell to send commands that need CLI interaction:

This example creates a upm profile named 'test':

import subprocess

p = subprocess.Popen(['/exos/bin/exsh','-n0', '-b', '-e','remote_serial'],
    stdin = subprocess.PIPE,
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE)

print >> p.stdin, 'create upm profile test'
print >> p.stdin, '.'
Doesn't seem to run on my end, I'm testing on a summit x440-24t running15.7.4.2 patch1-2

Getting the following error message when trying to run the script.

Traceback (most recent call last):
  File "/config/upmTest.py", line 3, in <module>
    p = subprocess.Popen(['/exos/bin/exsh','-n0', '-b', '-e','remote_serial'], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  File "/exos/tools/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/exos/tools/lib/python2.7/subprocess.py", line 1245, in _execute_child
    child_exception = pickle.loads(data)
  File "/exos/tools/lib/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "/exos/tools/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/exos/tools/lib/python2.7/pickle.py", line 971, in load_string
    self.append(rep.decode("string-escape"))
LookupError: unknown encoding: string-escape
Photo of Matthew Helm

Matthew Helm, Employee

  • 1,852 Points 1k badge 2x thumb
That method is only available in the later 16.x code. I actually have been using it in 22.x. I can revert to confirm, but you may want to upgrade to 16.1.x or 16.2.x.
Photo of Matthew Helm

Matthew Helm, Employee

  • 1,852 Points 1k badge 2x thumb
I take it back. This method is only available in 22.x. I tested it in 16.2.x and it failed similarly. Unfortunately, the X440 cannot use 22.x . Is this the ultimate platform on which you plan to use this script?
wherever we deploy new equipment we use x440-g2 switches with 21.x as well as x460-g2 where we are still using 15.7.x, we also re-deploy a significant amount of first generation x440 and x460 switches.

Due to the scale of our network ~1000 switches we're rather cautious with deploying newer firmware in production.

Unless there is some method to do this in 15.7.x I guess I'll just develop this script for 16.x and keep using our old solution until whenever we're rolling out 16.x or higher in prod.

Edit: I mean 22.x not 16.x
(Edited)