Python - 在 JunOS 中更改用户密码

Python - change password for user in JunOS

我需要有关此项目和我遇到的问题的帮助。

我可以毫无问题地更改此瞻博网络路由器。当我需要更改用户密码时,问题就出现了。

根据屏幕或下面的输出... 我尝试过: 用户发送命令更改密码。然后假设从 CLI 收到新密码的提示,我正在尝试使用 passwd1 和 2 字符串输入它,以便按照下面的方式发送。我什至试图用 getpass() 隐藏输出,但什么也没有..然后它闲置了,因为无法输入密码然后它被跳过并转到 expect:

screenshot

还有另一种实现方式:

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)

输出:

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

为了使用 python 处理 Junos 设备,我建议您使用 PyEZ - https://github.com/Juniper/py-junos-eznc

示例:

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()

解决方法是设置交互式cli的提示。例如 如果您知道您期待一个不受支持的提示 "interactive prompt" 例如“=” - 那么您需要告诉 python 您期待...提交您的命令并重置提示。

示例:

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)