我需要这个项目的帮助和问题。
我可以毫无问题地对这个juniper路由器进行更改。当我需要更改用户的密码时,会出现问题。
根据屏幕或下面的输出...我试过:用户发送命令来更改密码。然后,我们假设从CLI中获取新密码,我试图输入密码,并在下面发送passwd1和2字符串。我甚至试图用getpass()隐藏输出但没有...它因为无法输入密码而空闲然后它被跳过并转到期望:
还有另一种实现方式:
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()
对此的解决方法是设置交互式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)