Python - change password for user in JunOS

1k Views Asked by At

I need assistance with this project and issues im having.

I am able to make changes to this juniper router with no issues.. The issue comes in when I need to change the password for a user.

Per the screen or the output below... I have attempted: user sends the command to change the password. Then it's suppose to get prompted with the New Password from the CLI which I'm attempting to enter it with the passwd1 and 2 string to be sent over per below. I even attempted to hid the output with getpass() but nothing.. it idles then since is unable to enter the password then it gets skipped and goes to the expect:

screenshot

2

There are 2 best solutions below

0
Vijay Shetty On BEST ANSWER

There is another way of implimenting it:

from passlib.hash import md5_crypt
from getpass import getpass

user = getpass()
p1 = getpass()

hashpwd = md5_crypt.encrypt(p1)

commands = 'set system login user '+user+' class read-only authentication encrypted-password '+hashpwd

print (commands)

Output:

Password: 
Password: 
set system login user Vijay class read-only authentication encrypted-password $1$siC6W8.B$8FeEjf/Nt7shR1e8Axl.v1

For handling Junos devices using python, I would recommend you to use PyEZ - https://github.com/Juniper/py-junos-eznc

Example:

from jnpr.junos import Device
from lxml import etree

dev = Device('hostname', user='username', password='Password123')
dev.open()

cnf = dev.rpc.get_config()    # similar to 'show configuration | no-more' on cli
print (etree.tounicode(cnf))

dev.close()
0
D' go On

The resolution to this is to set the prompt of the interactive cli. For example if you know you are expecting an unsupported prompt "interactive prompt" for example "=" - then you need to tell python that you are expecting that... submit your command and reset the prompt.

Example:

def JunOS(self,host_ips,config,commit):                                                                                                                               

            try:                                                                                                                                                          
                    conn = SSH2(verify_fingerprint = False)                                                                                                               
                    conn.connect(host_ips)                                                                                                                                
                    print "Connecting to : ", host_ips                                                                                                                    
                    conn.login(account)                                                                                                                                   
                    print "**********************"                                                                                                                        
                    conn.execute(config)                                                                                                                                  
                    print conn.response                                                                                                                                   
                    conn.set_prompt(r'.$')                                                                                                                                
                    conn.execute('set system login user admin authen plain')                                                                                              
                    conn.execute(psswd)                                                                                                                                   
                    conn.set_prompt()                                                                                                                                     
                    conn.execute(psswd)                                                                                                                                   
                    conn.execute(commit)                                                                                                                                  
                    print conn.response                                                                                                                                   
                    time.sleep(3)                                                                                                                                         
                    print "********************************"                                                                                                              
                    print "Password Updated !"                                                                                                                            
                    print "********************************"                                                                                                              
            except:                                                                                                                                                       
                    print "IP for this device : ", host_ips                                                                                                               
                    print "Unable to connect or Username/password are incorrect"                                                                                          
                    print "**********************"
                    time.sleep(2)