My problem is that I need to set a few variables, and output a few lines every time I login to the ssh shell, and at the same time I have to be able to use sftp to tarnsfer files via Filezilla.
Now, as per the openssh FAQ at http://www.openssh.org/faq.html, if your startup scripts echo any kind of output, it messes up with sftp. So it either delays indefinitely, or errors out with a "Connection closed by server with exit code 128".
I have tried solutions like moving .bashrc to .bash_profile, or using the following code in .bashrc:
if [ "$TERM" != "dumb" ]
then
source .bashc_real
fi
And:
if [ "$TERM" = "xterm" ]
then
source .bashc_real
fi
However, nothing works. My shell terminal is bash, and I connect to sftp with filezilla.
Try doing this instead
Mike's answer will probably work. But it's worth pointing out that you can accomplish this carefully selecting which startup files to put the verbose stuff in. From the bash man page:
The sftp/scp tools start an interactive non-login shell, so .bashrc will be sourced. Many distributions source .bashrc from .bash_profile or vice versa, so it can get confusing. A good trick for testing the cleanliness of your login environment is to ssh in with a command, which simulates the same way scp/sftp connect. For example:
ssh myhost /bin/true
will show you exactly what scp/sftp sees when they connect.A simple demo:
The first test will cause scp/sftp/rsync etc. to break. The second version will work just fine.
If you're using csh:
And if it's bash:
or alternatively using bash regular expressions:
These lines should precede lines where you ouput/echo something back.
Mike's solution worked for me as well. But since my default shell is TCSH, I had to slightly edit the fix as follows (in .tcshrc):
Just thought I would share for everyone's benefit.
Here are the first lines of my (default)
.bashrc
file:The check for an interactive session avoids messing up with SCP, SFTP or
ssh remote-host command
mode.Without this, if your file
.bashrc
usesecho
or some other stuff printing on stdout, you may get this kind of errors:Received message too long 168435779
protocol error: unexpected <newline>
I like some of the other solutions mentioned here better, but I thought I throw out the solution that I currently use on my bash and csh VMs to prevent SFTP disconnects due to echo commands in my startup scripts, just in case anyone finds the information helpful.
In BASH:
In csh:
It's a bit brute force, but it does work.