Как исправить ожидаемый юникод или байты, получивший класс pyats.utils.secret_strings.SecretString в Python?Python

Программы на Python
Anonymous
Как исправить ожидаемый юникод или байты, получивший класс pyats.utils.secret_strings.SecretString в Python?

Сообщение Anonymous »

Я пытаюсь подключиться по SSH к устройству Cisco с помощью Python и выполнить некоторые команды конфигурации.
Однако я получаю сообщение об ошибке из-за неправильного формата секретной строки для моего пароля.
Я получаю пароль из файла yml и сохраняю его в словаре.
Но когда я передаю его в код входа по ssh, он выдает ошибку типа

Код: Выделить всё

Unknown exception: Expected unicode or bytes, got 

Вот код, который я пытаюсь использовать

Код: Выделить всё

    def ssh_cmd_execution(self, cmd, testscript):
device_ip = testscript.parameters['dut_ip']
logger.info('device_ip: ' + device_ip)
user_name = testscript.parameters['dut_username']
logger.info('username: ' + user_name)
server_password = testscript.parameters['dut_password']
#logger.info('password: ' + server_password)
port = ''
enable_password = ''
logger.info('enable_password' + enable_password)
# cmd_list = ['show version', 'sh run', 'sh boot']
cmd_list = cmd
ssh_ob = SSHUtilities()
output = ssh_ob.connect_execute_cmd_ssh(device_ip, port, user_name, server_password, enable_password, cmd_list)
logger.info("SSH Output" + str(output))
logger.info("SSH Output:" + str(output.keys()))
return True
и connect_execute_cmd_ssh здесь

Код: Выделить всё

def connect_execute_cmd_ssh(self, device_ip, port, user_name, password, enable_password, cmd_list):

ssh_d = dict()
self.ssh_IP = device_ip
if(port):
self.ssh_port = port
else:
self.ssh_port = '22'
self.ssh_UserName = user_name
self.ssh_Password = password
if (enable_password):
self.ssh_enable_Password = enable_password
else:
self.ssh_enable_Password = ""
self.ssh_cmd_list = cmd_list
logger.info("Command list to be executed on the device connected with ssh:" + str(self.ssh_cmd_list))
try:
self.output.clear()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=self.ssh_IP,
username=self.ssh_UserName,
password=self.ssh_Password,
port = self.ssh_port,
look_for_keys=False,
timeout=None
)
channel = ssh.invoke_shell()
time.sleep(1)
channel.send('\n')
if (self.ssh_enable_Password):
channel.send(b'enable \n')
channel.send(self.ssh_enable_Password.encode('ascii'))
channel.send(b'\n')
channel.send(b'terminal length 0 \n')
channel.send(b'\n')
logger.info("Established SSH connection with device")
for cmd in self.ssh_cmd_list:
channel.send(cmd + '\n')
time.sleep(3)
# while not channel.recv_ready(): '''changing recv_ready to send_ready as 'send_ready' is having trouble
# in reading and storing output if cmd takes time after execution. While send_ready working fine even
# though time taken by cmd is more'''
while not channel.send_ready():
time.sleep(3)
output2 = channel.recv(99999).decode(encoding='ascii')
#ssh_d[cmd] = output2.split(cmd)[1]
ssh_d[cmd] = output2
ssh.close()

except paramiko.ssh_exception.ChannelException as e:
raise Exception("Failed to open connection", e)
except paramiko.ssh_exception.AuthenticationException as e:
raise Exception("verify Entered user name and password", e)
except paramiko.ssh_exception.SSHException as e:
raise Exception("Failed to SSH to host ", e)

return ssh_d
Но я получаю сообщение об ошибке расшифровки пароля.

Подробнее здесь: https://stackoverflow.com/questions/784 ... rings-secr

Вернуться в «Python»