Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: A few lines in .zshenv (re ssh-agent) disable scp and rsync?



On Mon, 27 Jun 2011, rj wrote:

How might I resolve the fact that the presence of these lines at the bottom of my .zshenv on a (NetBSD) ISP account

#--------------------------------------------------------------------
# Make sure ssh-agent dies on logout:
trap '
 test -n "SSH_AGENT_PID"  && eval `ssh-agent -k`
' 0

Why `test -n VAR` here, but `if [ "$VAR" = "" ]` below?


# If no agent is running and we have a terminal, run ssh-agent and ssh-add:
if [ "$SSH_AUTH_SOCK" = "" ]
then
 eval `ssh-agent`
 /usr/bin/tty > /dev/null && ssh-add

Better:

tty -s && ssh-add

The -s flag means to print nothing


Better still:

[[ -t 0 ]] && ssh-add

Also avoids the use of an external program.


fi
#------------------------------------------------------------------


are causing me to be unable to transfer files between that account and my home Ubuntu box using (either from my home box or from the other side) rsync or scp?

I need to be able to create and kill ssh-agent as per the .zshenv entry above. (This was some recommended stuff from, I think, the O'Reilly book on SSH.)

But I also need to use scp and rsync to move files and do backups and so on.

How can I make the lines above work for me without crippling my scp and rsync
functions?

You shouldn't put anything that echoes output to the terminal in a login profile. That should be saved for interactive startup scripts.

In Zsh's case, that means you can solve this by simply moving those lines from .zshenv (which gets run for all shells, including those which don't accept input interactively) to .zshrc (which is only run for interactive shells).

For a good explanation, see:
http://blogs.oracle.com/janp/entry/how_the_scp_protocol_works#talkative_profiles

The offending commands are:
eval `ssh-agent`     -- which outputs the PID of the newly-started agent
eval `ssh-agent -k`  -- which outputs the PID of the agent it kills

So, you could also solve this by conditioning all of the ssh-agent actions on whether a terminal is connected:

# if stdin is a terminal (unnecessary in .zshrc)
if [[ -t 0 ]] ; then
	# kill the ssh-agent on exit
	trap '[[ -n "$SSH_AGENT_PID" ]] && eval `ssh-agent -k`' 0
	# start the ssh-agent if not already started
	[[ -z "$SSH_AGENT_PID" ]] && eval `ssh-agent`
	# add identities to the agent
	ssh-add
fi

--
Best,
Ben



Messages sorted by: Reverse Date, Date, Thread, Author