在 Paramiko 中仅记录 SSH 身份验证横幅

问题描述 投票:0回答:1

我想限制 Python Paramiko 库的日志详细信息。

我在记录器中使用以下配置:

logging.basicConfig(format='%(asctime)s - %(levelname)s :::: %(message)s',
                    datefmt='%m/%d/%Y %I:%M:%S',
                    filename='sample.log', level=logging.DEBUG)

上述配置的控制台日志:

02/23/2021 05:19:47 - DEBUG :::: Kex agreed: [email protected]
02/23/2021 05:19:47 - DEBUG :::: HostKey agreed: ssh-ed25519
02/23/2021 05:19:47 - DEBUG :::: Cipher agreed: aes128-ctr
02/23/2021 05:19:47 - DEBUG :::: MAC agreed: hmac-sha2-256
02/23/2021 05:19:47 - DEBUG :::: Compression agreed: none
02/23/2021 05:19:47 - DEBUG :::: kex engine KexCurve25519 specified hash_algo <built-in function 
openssl_sha256>
02/23/2021 05:19:47 - DEBUG :::: Switch to new keys ...
02/23/2021 05:19:47 - DEBUG :::: Adding ssh-ed25519 host key for 10.10.4.196: 
b'f14196dd49bc7fced6f6658236686957'
02/23/2021 05:19:47 - DEBUG :::: Trying discovered key b'64d107318fdaea41ba957bdccab827d1' in 
/home/ubuntu/.ssh/id_rsa
02/23/2021 05:19:47 - DEBUG :::: userauth is OK
02/23/2021 05:19:47 - INFO :::: Authentication (publickey) failed.
02/23/2021 05:19:47 - DEBUG :::: userauth is OK
02/23/2021 05:19:47 - INFO :::: Authentication (password) successful!
**02/23/2021 05:19:47 - INFO :::: Welcome user**

我不想打印日志中所有与 SSH 相关的详细信息,而只想打印最后一行,即 '欢迎用户'。 请建议是否有任何这样的方法可能。

感谢您的宝贵时间:)

python logging ssh python-3.6 paramiko
1个回答
1
投票

您感兴趣的行似乎是 SSH 身份验证横幅。

因此,如果您只想这样做,请禁用所有 Paramiko 日志记录并自行记录横幅。要检索横幅,请使用

Transport.get_banner()
。如果您正在使用
SSHClient
API(您应该),请使用
Transport
 获取 
SSHClient.get_transport()
实例。

© www.soinside.com 2019 - 2024. All rights reserved.