python socket接收的内容排版问题

admin_account 发布于 2017/05/24 17:49
阅读 122
收藏 0
def sshclient(user, passwd, ip, port, excmds):
	global version, upgrade_his
	global resultlist_tmp

	client = paramiko.SSHClient()
	client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

	client.connect(ip, port, user, passwd)
	shell_client = client.invoke_shell()
	time.sleep(5)
	print(shell_client.recv(10240).decode('utf-8'))

	for cmd in excmds:
		result_all = b''
		tmp = b''
		shell_client.sendall(cmd+'\n')
		time.sleep(3)
		while 'ac>'.encode('utf-8') not in tmp:
			tmp = shell_client.recv(10240)
			result_all += tmp
			if 'ac>'.encode('utf-8') in tmp:
				resultlist_tmp.append(result_all.decode('utf-8'))
				print('----------')
				print(result_all.decode('ascii'))
			while '--- more ---'.encode('utf-8') in tmp:
				shell_client.sendall('\n   \n')
				time.sleep(3)
				tmp = shell_client.recv(10240)
				result_all += tmp
				if 'ac>'.encode('utf-8') in tmp:
					resultlist_tmp.append(result_all.decode('utf-8'))
					print('----------')
					print(result_all.decode('utf-8'))
					break

实现的是SSH去连接设备,然后执行命令获取返回结果。

因为要多次执行命令,图方便就用invoke_shell()直接处理socket,while循环里获取的print结果如下图:

但实际上我用终端ssh连接然后执行命令返回的结果没有这种换行情况:

加载中
返回顶部
顶部