Paramiko: SFTPError('Garbage packet received',)

Created on 15 Sep 2014  路  6Comments  路  Source: paramiko/paramiko

In a WSGI-app trying to read a file on a remote server I sometimes get the error in the title. What does it mean and what can cause it?

The last layers of the traceback are

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/files.py in save
        self.name = self.storage.save(name, content)
/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py in save
        name = self._save(name, content)
/usr/local/lib/python2.7/dist-packages/storages/backends/sftpstorage.py in _save
        if not self.exists(dirname):
/usr/local/lib/python2.7/dist-packages/storages/backends/sftpstorage.py in exists
            self.sftp.stat(remote_path)
/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py in stat
        t, msg = self._request(CMD_STAT, path)
/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py in _request
        return self._read_response(num)
/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py in _read_response
                t, data = self._read_packet()
/usr/local/lib/python2.7/dist-packages/paramiko/sftp.py in _read_packet
            raise SFTPError('Garbage packet received')
Bug Needs changelodocs SFTP

Most helpful comment

You can cause this exception reliably if you write to stdout from any of the shell scripts that ssh automatically sources on the remote host: ~/.bashrc, ~/.ssh/rc, and /etc/ssh/sshrc are all examples. It seems that paramiko assumes that stdout will only contain data from the remote sftp process, and chokes on the unexpected input.

Note that the scp client handles this case without failure, simply copying the script output to its stdout.

All 6 comments

Sounds like the comment above that line is the only real explanation: https://github.com/paramiko/paramiko/blob/f74c7d76ec2ea9d39a4af6643761b648f81bd266/paramiko/sftp.py#L174-L177

most sftp servers won't accept packets larger than about 32k, so anything with the high byte set (> 16MB) is just garbage.

I can't say I've seen that exception raised before in my travels but I've only maintained the lib for a couple of years & that line dates from 2006 or so.

Guessing the usual troubleshooting steps for any software apply here - see if you can figure out a pattern around the occasional occurrences of the problem; see whether it's always the same target server that causes it or if it applies to all targets; try running it in a loop outside of the WSGI application to see if it happens there too; etc.

Good luck & sorry I've no clear answers. Leaving this open in case you or others find something more useful; I'd love to add something to the exception message, or the documentation, explaining what this means in more detail - presuming of course that this isn't a bug of some kind and it's not "really" a bogus packet.

You can cause this exception reliably if you write to stdout from any of the shell scripts that ssh automatically sources on the remote host: ~/.bashrc, ~/.ssh/rc, and /etc/ssh/sshrc are all examples. It seems that paramiko assumes that stdout will only contain data from the remote sftp process, and chokes on the unexpected input.

Note that the scp client handles this case without failure, simply copying the script output to its stdout.

Gunicorn timeout causes the issue for me. Just increase the timeout and works fine.

--timeout

I am using the paramiko library in order to transfer the file from Device to my server. But having the trouble when there is a security tools like tacacs and radius is configured. it fails to open the sftp connection. Although in case of Device which does not have the tacacs and Radius configured it works file . any thing we have to provide extra parameter.

The code i am using given below

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("Ip ADDRESS", username='user', password='pass')
sftp = client.open_sftp()    ## Says garbage packet received from the server.

below is the exception

File "/vagrant/xyx/bxyx/wsutil.py", line 14, in download
sftp = client.open_sftp()
File "/vagrant/xyx/env/lib/python2.7/site-packages/paramiko/client.py", line 477, in open_sftp
return self._transport.open_sftp_client()
File "/vagrant/xyx/env/lib/python2.7/site-packages/paramiko/transport.py", line 919, in open_sftp_client
return SFTPClient.from_transport(self)
File "/vagrant/xyx/env/lib/python2.7/site-packages/paramiko/sftp_client.py", line 132, in from_transport
return cls(chan)
File "/vagrant/xyx/env/lib/python2.7/site-packages/paramiko/sftp_client.py", line 99, in __init__
server_version = self._send_version()
File "/vagrant/xyx/env/lib/python2.7/site-packages/paramiko/sftp.py", line 105, in _send_version
t, data = self._read_packet()
File "/vagrant/xyx/env/lib/python2.7/site-packages/paramiko/sftp.py", line 179, in _read_packet
raise SFTPError('Garbage packet received')
SFTPError: Garbage packet received

We would be grateful if any one can help in that .

if i check the packet in debug i receive the \r\n in 4 byte packet that raises the exception.

Normal SFTP works fine when we use the terminal in order to transfer the file.

Has anyone found a solution for this? We are experiencing an issue, for a single SFTP server, during file download/get, this same error, it's not any particular file, we can download using filezilla fine, and we've tried setting the timeout in the connection method to 6000 seconds, and we have ever tried setting the following values for the transport class...

default_window_size = 500000
default_max_packet_size = 1024
set_keepalive(2)

Hello,

For information, I encountered a similar issue with the following traceback:

Traceback (most recent call last):
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/fabric/main.py", line 757, in main
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/fabric/tasks.py", line 386, in execute
    multiprocessing
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/fabric/tasks.py", line 276, in _execute
    return task.run(*args, **kwargs)
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/fabric/tasks.py", line 173, in run
    return self.wrapped(*args, **kwargs)
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/fabric/decorators.py", line 56, in inner_decorator
    return func(*args, **kwargs)
  File "/home/joa/scal/ironman/Rescue/fab/fabfile.py", line 95, in copy_installer
    send(path, version)
  File "/home/joa/scal/ironman/Rescue/fab/fabfile.py", line 87, in send
    put(path, offline_path(version))
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/fabric/network.py", line 687, in host_prompting_wrapper
    return func(*args, **kwargs)
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/fabric/operations.py", line 346, in put
    ftp = SFTP(env.host_string)
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/fabric/sftp.py", line 33, in __init__
    self.ftp = connections[host_string].open_sftp()
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/paramiko/client.py", line 490, in open_sftp
    return self._transport.open_sftp_client()
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/paramiko/transport.py", line 945, in open_sftp_client
    return SFTPClient.from_transport(self)
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 139, in from_transport
    return cls(chan)
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 103, in __init__
    server_version = self._send_version()
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/paramiko/sftp.py", line 107, in _send_version
    t, data = self._read_packet()
  File "/home/joa/.virtualenvs/rescue-py2/local/lib/python2.7/site-packages/paramiko/sftp.py", line 178, in _read_packet
    raise SFTPError('Garbage packet received')
paramiko.sftp.SFTPError: Garbage packet received
Disconnecting from root@HOST... done.

It was actually simply due to the fact the the command used the wrong user. Changing the user solved my issue.

The weird bit is that using a wrong user yields a "Garbage packet received" rather than an authentication-related message.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thomasd3 picture thomasd3  路  3Comments

hoerny picture hoerny  路  5Comments

jacky15 picture jacky15  路  5Comments

Kkevsterrr picture Kkevsterrr  路  5Comments

kalidasya picture kalidasya  路  5Comments