Python script triggered by UPM doesn't work properly

  • 0
  • 1
  • Problem
  • Updated 3 years ago
  • Solved
  • (Edited)
Hi everybody.
I use x480 with 16.1.2.

I have simple .py script like this.
import pexpect
import exsh
exsh.clicmd ('create log message "Starting NSCBackup!"')
for i in range(1,10):
print(i)
p = pexpect.spawn('/exos/bin/ssh -r 2 USERNAME@172.20.1.' + str(i))
idx = p.expect(['password', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('SECRETPASS')
idx = p.expect(['sCore', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('create log message "Not so cold backuper"')
idx = p.expect(['sCore', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('save')
idx = p.expect(['(y/N)', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('y')
idx = p.expect(['sCore', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('scp2 vr "VR-Default" sCore' + str(i) + '.cfg USERNAME@172.20.1.10:sCore' + str(i) + '.cfg')
idx = p.expect(['password', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('SECRETPASS')
p.sendline('exit')
Not so beautiful, but it works!

So... I can run it with "load script NSCB.py". It works properly.

Then I created and scheduled UPM profile like this.
create upm profile NSCB
load script NSCB.py
.
create upm timer NSCBtimer
configure upm timer NSCBtimer profile NSCB
configure upm timer NSCBtimer at 11 5 2015 23 50 0 every 86400
And I run it:
run upm profile "NSCB" 
I see some log messages, but script stucked.
11/06/2015 15:55:19.94 <Info:System.userComment> Starting NSCBackup!
11/06/2015 15:55:19.54 <Noti:UPM.Msg.upmMsgExshLaunch> Launched profile NSCB for the event user-request

Any suggestions?
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb

Posted 3 years ago

  • 0
  • 1
Photo of OscarK

OscarK, ESE

  • 7,792 Points 5k badge 2x thumb
You do a print, but in upm I dont know what happens to that print, does it work if you remove that print(i) ?
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
Unfortunately no.
Photo of OscarK

OscarK, ESE

  • 7,792 Points 5k badge 2x thumb
ok, I think you should add some checks where you send some info to the log to keep track where it is stuck.
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
Yeah, I'm doing it right now. )
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
It stucked right after for loop begins.
for i in range(1,10):
Photo of OscarK

OscarK, ESE

  • 7,792 Points 5k badge 2x thumb
Maybe it is a rights thing and is ssh not executable by a script launched by UPM.
Photo of Matthew Helm

Matthew Helm, Employee

  • 200 Points 100 badge 2x thumb
So it looks like the purpose of this is to have a core switch ssh into downstream connected switches and save their configurations periodically right?

I'll take your script and attempt to make it into a python EXOS application process instead. I'll then test having a UPM process restart the py process. I'll let you know how it works out.

Let me know if I've misinterpreted what you want to do.

Thanks.
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
Yes, the concept is right.
Photo of Matthew Helm

Matthew Helm, Employee

  • 200 Points 100 badge 2x thumb
I was able to create a test.py application/process that uses pexpect with /exos/bin/ssh similarly to your script and I used a UPM profile and timer to restart it every 5 minutes. It worked.

Here is the UPM configuration:

## Module upm configuration.
#
create upm profile test
disable cli prompt
restart process test

.
create upm timer test
configure upm timer test profile test
configure upm timer test after 1 every 300

...

And here is the py file for the process and the command to create it. (I assumed that the ssh targets were sub-tended EXOS switches and so I sent the "disable cli prompting" command to avoid unnecessary pexpect lines in the process script.)

#vi test.py

import pexpectimport exos.api
def exosCmd(cmd):
   reply = exos.api.exec_cli([str(cmd)], ignore_errors=True)
   return str(reply)

def logMsg(m):
    exosCmd('create log message "{0}"'.format(m))

logMsg("Starting NSCBackup!")
p = pexpect.spawn('/exos/bin/ssh -r 2 admin@10.0.0.202')
idx = p.expect(['password', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('admin')
idx = p.expect(['x202', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('create log message "test test test"')
idx = p.expect(['x202', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('disable cli prompt')
idx = p.expect(['x202', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('save')
idx = p.expect(['x202', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('exit')

#^wq!
...

The process was started with this line that I issued before starting the UPM timer:

create process test py test start auto

...

Hope this helps.
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
Thank you for advice! I'm going to create similar proccess and give you feedback as soon as possible.
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
Sorry for waiting... Something is going wrong.

I've created py script, but when I run it I have this.
Traceback (most recent call last):
  File "/config/NSCB.py", line 2, in <module>
    import exos.api
  File "/exos/tools/lib/python2.7/site-packages/exos/api/__init__.py", line 21, in <module>
  File "/exos/tools/lib/python2.7/site-packages/exos/api/ems.py", line 10, in <module>
ImportError: No module named _exos_ext_ems

I've tried to import XOS api with "from exos import api" but...
Traceback (most recent call last):
File "/config/NSCB.py", line 2, in <module>
from xos import api
ImportError: No module named xos

It's not clear for me... What am I doing wrong?
Photo of Grosjean, Stephane

Grosjean, Stephane, Employee

  • 12,784 Points 10k badge 2x thumb
You need to run EXOS 15.7 minimum in order to create Python App (process). Are you running 15.6 ?
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
I'm running 16.1.2.14.
Photo of Grosjean, Stephane

Grosjean, Stephane, Employee

  • 12,784 Points 10k badge 2x thumb
oh, I misread, sorry.

Try that :

from exos import api

instead of

from xos import api
(Edited)
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
Same result...

Traceback (most recent call last):
File "/config/NSCB.py", line 2, in <module>
from exos import api
File "/exos/tools/lib/python2.7/site-packages/exos/api/__init__.py", line 21, in <module>
File "/exos/tools/lib/python2.7/site-packages/exos/api/ems.py", line 10, in <module>
ImportError: No module named _exos_ext_ems

What XOS version do you run?
(Edited)
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
It's not the version problem. I've tried 16.1.1.4, same result. The script doesn't run with upm profile.

create upm profile NSCB
run script NSCB-old.py

run upm profile "NSCB"
11/17/2015 11:26:53.88 <Info:System.userComment> Starting NSCBackup!
11/17/2015 11:26:53.44 <Noti:UPM.Msg.upmMsgExshLaunch> Launched profile NSCB for the event user-request

I can't even execute single-line script "from exos import api". You can see traceback in previous post.
(Edited)
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
Yeah! I've fixed it! Thanks everybody!
Photo of Drew C.

Drew C., Community Manager

  • 38,306 Points 20k badge 2x thumb
What was the problem? :)
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
It was interesting. I've learned that there are two python environments in EXOS actually. This thread. So I can't execute "import exos.api" just by typing "run script blalbalba.py", but I can do it when I create process with this .py script. It's a bit confusing.

Moreover the "from exos import api" string doesn't work at all.

Sorry for my poor English )
Photo of Grosjean, Stephane

Grosjean, Stephane, Employee

  • 12,784 Points 10k badge 2x thumb
Did you read the Python Getting Started Guide?

http://www.extremenetworks.com/wp-content/uploads/2015/02/Python_Getting_Started_Guide.pdf

It might help you.
Photo of eyeV

eyeV

  • 2,484 Points 2k badge 2x thumb
Yes, I read it. Very useful document actually, but, as I say above, "from exos import api" line from this guide doesn't work for me. I guess I just don't fully understand something.
(Edited)