Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

pbcopy isn't working in ssh session

If I ssh to my mac, pbcopy isn't working in an ssh session.
No diagnostics or errors. Nothing in errors console. Just not working.
Same with pbpaste.
Tried to replicate shell environment - no effect.
Any ideas?

macmini, Mac OS X (10.5.5), 2Gb RAM 320GB HDD

Posted on Oct 15, 2009 7:10 AM

Reply
11 replies

Oct 15, 2009 10:17 AM in response to Xecutor

Sure. One is running under the Aqua GUI windowing environment, and one is running outside of that environment.

So how would you like it if, you allowed other family members to have accounts on your Mac, and one of them ssh'ed into the Mac and did pbcopy and when you did a paste, you got their stuff?

Same deal. You started a totally different session from a different environment which has not inherited any of the GUI context from your Terminal session, which inherited context from the Aqua window manager.

Oct 15, 2009 10:25 AM in response to BobHarris

I think Xecutor has a valid complaint. I wouldn't want my clipboard showing up in the local user's environment, but I would expect it to work in my own environment. pushd and popd work perfectly fine in a ssh session, why not pbcopy and pbpaste? If they can detect that the user doesn't have a local environment, they should behave accordingly instead of just silently failing.

I suggest reporting this as a bug.

Oct 15, 2009 1:12 PM in response to etresoft

pushd/popd are handled by your shell. If you start a new shell under the current shell, your popd does not know anything about your previous pushd's. If you start a new ssh session, it is not going to know about your other sessions's pushd's.

But a bare bones Unix process does not have a window manager with a clipboard infrastructure.

Maybe it should give an error. It does return with a non-zero exit code

# an ssh session
echo hi | pbcopy
echo $?
1

and now from

# Terminal under the Aqua window manager
echo hi | pbcopy
echo $?
0

I understand why it does what it does, and that Apple really is not focused on Unix users.

However, I would encourage you to file a bug with Apple at either:
<http://www.apple.com/feedback/macosx.html>
or
<http://bugreporter.apple.com>
Free ADC (Apple Developer Connection) account needed for BugReporter

Oct 15, 2009 1:44 PM in response to BobHarris

BobHarris wrote:
Maybe it should give an error. It does return with a non-zero exit code

# an ssh session
echo hi | pbcopy
echo $?
1

and now from

# Terminal under the Aqua window manager
echo hi | pbcopy
echo $?
0


That's good to know. Perhaps the original poster could write a couple of scripts called pbcopy and pbpaste and put them in a user path. If a call to /usr/bin/pbcopy fails, then echo the data to a read-protected file in the user's home directory. Do the reverse for pbpaste.

Oct 15, 2009 9:50 PM in response to BobHarris

I don't know what kind of IPC pbcopy and pbpaste are using to
communicate with windows manager, but I thought
it's something mentioned in a shell environment.
Yes, in an ssh session shell is executed by sshd, not
Terminal, but still it's local process executed with my
permissions. And I completely replicated shell environment
in ssh session to match local session.
I found possible solution in osx hints with LaunchAgents.
Hint was for osx 10.4, and it's not working in 10.5.
It seems that pbcopy isn't working in ssh session
by design, probably for security reasons only.
Probably it's checking processes tree or something like this...
I guess I can emulate remote pbcopy with simple perl script
running in background and temporary file than...

Oct 16, 2009 2:52 AM in response to Xecutor

If anyone here is interested in my solution to the problem:
Little C++ program called "remoteclipmon.cpp":
#include <stdio.h>
#include <sys/event.h>
#include <unistd.h>
#include <stdlib.h>
#include <string>
#include <signal.h>
#include <errno.h>


static bool stop=false;

extern "C" void sigDisp(int sig)
{
stop=true;
}

int main()
{
sigset(SIGINT,sigDisp);
int kq=kqueue();
if(kq==-1)
{
fprintf(stderr,"failed to create kqueue :%s\n",strerror(errno));
return 1;
}
std::string filePath=getenv("HOME");
if(filePath.length() && *filePath.rbegin()!='/')
{
filePath+='/';
}
filePath+="Documents/remoteclipboard.txt";
std::string cmd="cat " filePath" |/usr/bin/pbcopy";
FILE *f=fopen(filePath.c_str(),"wb+");
if(!f)
{
fprintf(stderr,"failed to open '%s':%s\n",filePath.c_str(),strerror(errno));
return 1;
}
int fd=fileno(f);
struct kevent e={0,};
e.ident=fd;
e.flags=EV_ADD;
e.filter=EVFILT_READ;
while(!stop)
{
kevent(kq,&e,1,&e,1,0);
usleep(10000);
system(cmd.c_str());
ftruncate(fd,0);
}
close(kq);
fclose(f);
return 0;
}

Can be compiled with:
g++ -o remoteclipmon remoteclipmon.cpp
And executed in background:
./remoteclip&
Little local shell script rcopy placed in a bin/ subdirectory in my home directory:
#!/bin/bash
if [ ! -z "$*" ]; then
echo -n $* >$HOME/Documents/remoteclipboard.txt
else
cat >$HOME/Documents/remoteclipboard-temp.txt
cat $HOME/Documents/remoteclipboard-temp.txt >$HOME/Documents/remoteclipboard.txt
rm $HOME/Documents/remoteclipboard-temp.txt
fi

And even smaller remote shell script rcopy:
#!/bin/bash
ssh macmini bin/rcopy $*

Here macmini is name of my host.
Now on remote system I can type:
rcopy hello world
and get 'hello world' in clipboard, or
./some_program |rcopy
and get output of some_program in my local clipboard.

pbcopy isn't working in ssh session

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.