How to ignore STDERR when nesting commands?

I have a pel script running as root that needs to read the contents of a file on a remote system, I have an ssh trust relationship as a particular user but not as root.

I then need to write back out to that file again to change it's content a bit.

On the surface this seemed really easy but I'm getting tangled up in all the layers required to actually do this.

I'm tryint to do the following:

su - <username of this side trust relationship> -c \"ssh <username of far-side trust>@<target server> cat <filename>\"

This seems to work pretty well but it's including the login banner at the top of the "file" returned and I can't seem to get it to ignore it.

I've tried including '2> /dev/null' at various points in the line to no avail.

In order to get to the bottom of this, I've abstracted that part out into a shell script called 'remote-cat.sh:

#!/bin/sh
su - $1 -c "ssh -q -n $2 cat $3"

Which returns the login header followed by the file contents:

bash-3.00# ./remote-cat.sh username otheruser@remotehost ace.cfg.text
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
Started interactive bash shell
<AceConfig>
   <LU62>
      <Locale>ENG</Locale>
   ...

If I change my script to be:

#!/bin/sh
su - $1 -c "ssh -q -n $2 cat $3 >/dev/null"

I only see the header:

bash-3.00# ./remote-cat.sh username otheruser@remotehost ace.cfg.text
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
Started interactive bash shell
bash-3.00# 

But if I change the script to use 2> /dev/null:

#!/bin/sh
su - $1 -c "ssh -q -n $2 cat $3 2>/dev/null"

I just get the header and text combined again like before.

So, where am I going wrong here? Or is there better way to do this in perl without having to do all the su, ssh, cat rubbish?

try adding the ssh -o BatchMode=yes option.

your initialization files think your coming in with an interactive session.

a better solution would be to use scp to copy the file locally and then operate on it.

No joy there, just got the same behaviour as without it.

I was hoping not to have to in order to have fewer temp files kicking round, but I think that might be the way to do it and it actually makes my life easier when trying to write the change back afterwards.

Cheers!

another thing you can try on the remote side is to modify the .profile or whatever .file your shell uses and wrap the commands in this

if tty -s
then
   echo starting shell blah blah
fi

another thought is that the first two lines are actually being printed from the local machine when you run the su command but before ssh starts to execute. try the tty -s solution there first.