【问题标题】:Python - change password for user in JunOSPython - 在 JunOS 中更改用户密码
【发布时间】:2016-04-26 16:11:24
【问题描述】:

我在这个项目和我遇到的问题上需要帮助。

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

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

screenshot

【问题讨论】:

    标签: python-2.7 juniper-network-connect pyez


    【解决方案1】:

    还有另一种实现方式:

    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 $1$siC6W8.B$8FeEjf/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()
    

    【讨论】:

      【解决方案2】:

      解决办法是设置交互式cli的提示。例如 如果您知道您期待一个不受支持的提示“交互式提示”,例如“=” - 那么您需要告诉 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-16
        • 2013-11-17
        • 2016-10-17
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 1970-01-01
        • 2021-07-26
        相关资源
        最近更新 更多