Python - 在JunOS中更改用户的密码

时间:2016-04-26 16:11:24

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

我需要这个项目的帮助,并且我有问题。

我能够毫无问题地对这个juniper路由器进行更改。当我需要更改用户的密码时,会出现问题。

根据屏幕或下面的输出... 我试过了: 用户发送命令更改密码。然后,我们假设从CLI中获取新密码,我试图输入密码,并在下面发送passwd1和2字符串。我甚至试图用getpass()隐藏输出但没有...它因为无法输入密码而空闲然后它被跳过并转到期望:

screenshot

2 个答案:

答案 0 :(得分:0)

对此的解决方法是设置交互式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)

答案 1 :(得分:-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()