Junos PyEZ Entering Passwords from Python

1.2k Views Asked by At

Hi I am currently learning PyEZ to configure JunOS devices from Python. But I am stuck at a certain problem. I want to be able to create new users through Python but I can't figure out how to enter passwords with python. I have tried many different things but can't seem to make it work. Any advice would be appriciated

from jnpr.junos import Device
from jnpr.junos.utils.config import Config  

dev = Device(host='192.168.56.2', user='root', password='Juniper1')
dev.open()
cu=Config(dev)

new_User='set system login user Read class read-only authentication plain-text-password'
pass_New='Read1234'
pass_Repeat='Read1234'

cu.load(new_User, format='set')
cu.load(pass_New,format='set')
cu.load(pass_Repeat,format='set')

And Here is the Error

Traceback (most recent call last):
  File "/home/oscar/PycharmProjects/Junos/HelloWorld.py", line 18, in <module>
    cu.load(pass_New,format='set')
  File "/usr/local/lib/python2.7/dist-packages/jnpr/junos/utils/config.py", line 377, in load
    return try_load(rpc_contents, rpc_xattrs)
  File "/usr/local/lib/python2.7/dist-packages/jnpr/junos/utils/config.py", line 343, in try_load
    raise ConfigLoadError(cmd=err.cmd, rsp=err.rsp, errs=err.errs)
jnpr.junos.exception.ConfigLoadError: ConfigLoadError(severity: error, bad_element: Read1234, message: unknown command)
2

There are 2 best solutions below

2
On

When you're using PyEZ to apply configuration, the module is expecting atomic configuration blobs; it is not just a replacement for the interactive CLI shell.

The error you are seeing is because you're sending pass_New 'Read1234' when Junos is expecting a specific set command.

To achieve your goal, you'll have to provide the hashed version of the password in your code, and send that as part of the new_User command.

To do this you'll need a hashing module - I use passlib, because crypt() function in OSX spits out hashes that are not compatible with Junos even though they are both BSD variants - go figure.

#!/usr/bin/python
from passlib.hash import md5_crypt
from jnpr.junos import Device
from jnpr.junos.utils.config import Config

username = 'Read'
plaintext = 'toomanysecrets'

dev = Device(host='192.168.56.2', user='root',passwd='Juniper1')
dev.open()
cu=Config(dev)
hashedpassword = md5_crypt.encrypt(plaintext)
set_command = 'set system login user '+username+' class read-only authentication encrypted-password '+hashedpassword
cu.load(set_command, format='set')
dev.commit()
dev.close()
0
On

Also to add why we can't do

new_User='set system login user Read class read-only authentication plain-text-password'
pass_New='Read1234'
pass_Repeat='Read1234'

cu.load(new_User, format='set')
cu.load(pass_New,format='set')
cu.load(pass_Repeat,format='set')

I can notice you are trying to type/retupe password using load which is not how load function works. PyEZ in background work on netconf, it's not a screen scrapping. Hence we should not try simulating that. When we call load it tries to load the config via load-configuration rpc.