Header Only - DO NOT REMOVE - Extreme Networks

Python script triggered by UPM doesn't work properly


Userlevel 3
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')[/code]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[/code]And I run it:
run upm profile "NSCB" [/code]I see some log messages, but script stucked.
11/06/2015 15:55:19.94 [i] Starting NSCBackup!
11/06/2015 15:55:19.54 Launched profile NSCB for the event user-request
[/code]
Any suggestions?

21 replies

Userlevel 6
You do a print, but in upm I dont know what happens to that print, does it work if you remove that print(i) ?
Userlevel 3
OscarK wrote:

You do a print, but in upm I dont know what happens to that print, does it work if you remove that print(i) ?

Unfortunately no.
Userlevel 6
OscarK wrote:

You do a print, but in upm I dont know what happens to that print, does it work if you remove that print(i) ?

ok, I think you should add some checks where you send some info to the log to keep track where it is stuck.
Userlevel 3
OscarK wrote:

You do a print, but in upm I dont know what happens to that print, does it work if you remove that print(i) ?

Yeah, I'm doing it right now. )
Userlevel 3
OscarK wrote:

You do a print, but in upm I dont know what happens to that print, does it work if you remove that print(i) ?

It stucked right after for loop begins.
for i in range(1,10):[/code]
Userlevel 6
OscarK wrote:

You do a print, but in upm I dont know what happens to that print, does it work if you remove that print(i) ?

Maybe it is a rights thing and is ssh not executable by a script launched by UPM.
Userlevel 1
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.
Userlevel 3
Matthew Helm wrote:

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.

Yes, the concept is right.
Userlevel 1
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.
Userlevel 3
Matthew Helm wrote:

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.

Thank you for advice! I'm going to create similar proccess and give you feedback as soon as possible.
Userlevel 3
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
import exos.api
File "/exos/tools/lib/python2.7/site-packages/exos/api/__init__.py", line 21, in
File "/exos/tools/lib/python2.7/site-packages/exos/api/ems.py", line 10, in
ImportError: No module named _exos_ext_ems[/code]
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
from xos import api
ImportError: No module named xos[/code]
It's not clear for me... What am I doing wrong?
Userlevel 7
You need to run EXOS 15.7 minimum in order to create Python App (process). Are you running 15.6 ?
Userlevel 3
Grosjean, Stephane wrote:

You need to run EXOS 15.7 minimum in order to create Python App (process). Are you running 15.6 ?

I'm running 16.1.2.14.
Userlevel 7
Grosjean, Stephane wrote:

You need to run EXOS 15.7 minimum in order to create Python App (process). Are you running 15.6 ?

oh, I misread, sorry.

Try that :

from exos import api

instead of

from xos import api
Userlevel 3
Grosjean, Stephane wrote:

You need to run EXOS 15.7 minimum in order to create Python App (process). Are you running 15.6 ?

Same result...

Traceback (most recent call last):
File "/config/NSCB.py", line 2, in
from exos import api
File "/exos/tools/lib/python2.7/site-packages/exos/api/__init__.py", line 21, in
File "/exos/tools/lib/python2.7/site-packages/exos/api/ems.py", line 10, in
ImportError: No module named _exos_ext_ems
[/code]
What XOS version do you run?
Userlevel 3
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 [i] Starting NSCBackup!
11/17/2015 11:26:53.44 Launched profile NSCB for the event user-request[/code]
I can't even execute single-line script "from exos import api". You can see traceback in previous post.
Userlevel 3
Yeah! I've fixed it! Thanks everybody!
Userlevel 7
eyeV wrote:

Yeah! I've fixed it! Thanks everybody!

What was the problem? 🙂
Userlevel 3
eyeV wrote:

Yeah! I've fixed it! Thanks everybody!

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 )
Userlevel 7
eyeV wrote:

Yeah! I've fixed it! Thanks everybody!

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.
Userlevel 3
eyeV wrote:

Yeah! I've fixed it! Thanks everybody!

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.

Reply