Search code examples
pythonparamikoscp

Python scp/paramiko Bad Time Format Exception when attempting to get a file


Python 3.9 scp 0.14.4 running on Mac OSX Ventura 13.0

When trying to run the following:

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()

The following exception is being thrown:

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'

This seems to be trying to parse the banner or something? Even though I indicate not to preserve times so not sure why its attempting it.

This is the banner returned once the host is listed:

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.

Thus I was thinking the 'his' was being picked up out of the banner.

Tried slipping in a command prior to the scp get on the client but probably didn't really do much as I suspect the scp still gets the banner.

when I manually do this via the cli all works fine

Any thoughts on this would be great


Solution

  • I think you are using two different libraries to access the file

    For paramiko you can use the following to get the file

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

    rather than

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