帶內串列埠 在串列埠中輸入命令

Tanwheey發表於2024-10-30
def hioadm_shell(self, start_cmd, cmd, wait_str='Press CTRL+C', wait_time=2, record_size=10240):
        """進入盤內命令 hioadmshell + 控制盤

        Args:
            start_cmd: hioadmshell + device
            cmd:  next cmd
        Returns:
        """
        std = self.con.exec_command_with_confirm(start_cmd+'\n', next_cmd=cmd, confirm=False, wait_str=wait_str,
                                                 wait_time=wait_time, record_size=record_size)
        self.con.invoke.send('\x03\n')
        return std

def exec_command_with_confirm(self, command,next_cmd="", end_words='\n', confirm=True, wait_str='', wait_time=10, record_size=10240):
"""
SSH執行命令並確認
:param wait_str:
:param confirm:
:param wait_time: 執行命令後,等待時間
:param command: 執行命令
:return: 命令執行結果
"""

self.alive()
log.debug(f'{self.ip} 執行了命令 {command}{end_words}')
self.invoke.send(f'{command}{end_words}')
result = ''
out = ''
try:
if confirm and wait_str:
for i in range(0, wait_time):
result = self.invoke.recv(1024).decode('utf-8',errors='ignore')
print(result)
if 'y/n' in result or 'Y/N' in result:
result = self.exec_command_with_confirm('Y', confirm=False, wait_str=wait_str)
break
else:
sleep(1)
else:
for i in range(0, wait_time):
result += self.invoke.recv(1024).decode('utf-8', errors='ignore')
result = result.replace('\r\r\n', '').replace('\r\n','')
if wait_str in result:
log.info(result)
self.invoke.send('\n')
if next_cmd:
sleep(1)
self.invoke.send(f'{next_cmd}\n')
while True:
sleep(2)
if self.invoke.recv_ready():
out= self.invoke.recv(record_size).decode('utf-8', errors='ignore')
out = out.replace('\r\r\n','').replace('\r\n','')
log.info(out)
self.invoke.send('\n')
if 'CORE0->' in out:
break
return out.replace('CORE0->','')
# log.info('執行結果如下:\n%s\n\n' % result)
break
else:
log.debug('資料未收全,等待1s繼續查收')
sleep(1)
# log.info(result)
except Exception as e:
raise Exception(f'命令執行錯誤{e}')
self.invoke.close()
return result

  

相關文章