Python scp/paramiko获取文件时出现错误时间格式异常



Python 3.9scp 0.14.4运行于Mac OSX Ventura 13.0

当尝试运行以下命令时:

def createSSHClient(server, port, user, password):
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(server, port, user, password)
return client

def get_file():
ssh = createSSHClient('myhost.com', 2022, 'username', 'mypassword')
banner = ssh.exec_command('n')
scp = SCPClient(ssh.get_transport())
scp.get(remote_path='/mnt/users/username/file.txt', local_path='./file.txt', preserve_times=False)
if __name__ == '__main__':
get_file()

抛出以下异常:

Traceback (most recent call last):
File "/git/projects/diet/scp-example/.venv/lib/python3.9/site-packages/scp.py", line 437, in _set_time
mtime = int(times[0])
ValueError: invalid literal for int() with base 10: b'his'

这似乎是试图解析横幅或什么?尽管我表示不保留时间,但我不确定它为什么要这么做。

这是列出主机后返回的横幅:

Unauthorized use of this system is prohibited.  Anyone using this
system expressly consents to the monitoring of his or her system use
and understands that evidence of criminal activity discovered by
system personnel may be reported to law enforcement officials.

因此,我认为"他"是被从横幅中挑出来的。

尝试在客户端scp get之前插入命令,但可能没有真正做太多,因为我怀疑scp仍然获得横幅。

当我通过cli手动执行此操作时,所有工作正常

有什么想法吗?

我想你使用了两个不同的库来访问文件

对于paramiko,您可以使用以下命令获取文件

sftp = ssh.open_sftp()
sftp.get(remotepath='/mnt/users/username/file.txt', localpath='file.txt')

而不是

scp = SCPClient(ssh.get_transport())
scp.get(remote_path='/mnt/users/username/file.txt', local_path='./file.txt', preserve_times=False)

最新更新