我有一台远程服务器,安装的是 CentOS,我现在想通过 Python 获取该服务器上的日志(messages)文件,并将内容写入到一个 txt 文件中
import paramiko
ip = "192.168.4.5"
username = "root"
password = "P@ssw0rd"
log = open("log.txt", "w")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, port=22, username=username, password=password, timeout=20)
ssh_stdin, ssh_stdout, ssh_stderr = client.exec_command("cat /var/log/messages | grep -i error")
blog.write(str(ssh_stdout.read()))
log.close()
client.close()
通过该方法我可以将日志内容写入到 log.txt 文件中,但是所有脚本内容却写在了同一行。
b'Aug 5 15:45:18 localhost kernel: RAS: Correctable Errors collector initialized.\nAug 5 15:45:19 localhost kernel: [drm] It appears like vesafb is loaded. Ignore above error if any.\nAug 5 15:45:29 localhost mcelog[929]: mcelog: Cannot write MSR_ERROR_CONTROL to /dev/cpu/0/msr\nAug 5 15:45:29 localhost mcelog[929]: mcelog: Cannot write MSR_ERROR_CONTROL to /dev/cpu/1/msr\nAug 5 15:45:42 localhost journal[1474]: Error looking up permission: GDBus.Error:org.freedesktop.portal.Error.NotFound: No entry for geolocation\nAug 5 15:45:42 localhost org.gnome.Shell.desktop[1474]: Errors from xkbcomp are not fatal to the X server\nAug 5 15:45:43 localhost kernel: [drm:vmw_stdu_crtc_page_flip [vmwgfx]] *ERROR* Page flip error -16.\n'
请问这个日志的开头 b 表示什么,有办法去掉吗?
我看了每行日志后面都有换行符,请问我怎么才能在写入 txt 文件的时候,是逐行写入的,而不是在后面加换行符?