Trying to ssh as another user and cd to directory

I can ssh to another directory by doing the following:

 
for server in server1; do ssh -t $server1 "cd /tmp; 'ls'";done

However, if I try to do it as another user it fails:
I have tried:

 
for server in server1
do
dir=$(su - nbadaccount -c "ssh $server `cd '/tmp/TSTCON'` " )
pwd
done
 
for server in server1; do su nbadaccount -c "ssh -t $server "cd /tmp 'ls'"";done
 
for server in server1; do su nbadaccount -c ssh -t $server "cd /tmp; 'ls'";done

and a variety of other methods. The most I can get it to do is to ssh to the server but it never changes directories. Does anybody have any idea how to do this? Thanks in advance for any help.

---------- Post updated Dec 10th, 2012 at 10:44 AM ---------- Previous update was Dec 9th, 2012 at 10:00 PM ----------

Actually I decided to use the find command to find what I was looking for but I cannot get it to work with regex, though without ssh, it DOES work with regex:

 
varx=$(su nbadmin -c "ssh server1 'find /tmp/TSTCON01 -type d -name '*[0-9][0-9]*''");echo $varx

This does not work but if I run this command it does work:

 
server1>find /tmp/TSTCON01 -name "*[0-9][0-9]*" -type d"

it should print 1353000000 and it does as long as I am not running ssh. Anyone know why the regex is being lost?

Define 'does not work'

What error do you get?

Do you have ssh keys set up on the remote servers?

varx=$(su nbadmin -c "ssh server1 'find /tmp/TSTCON01 -type d -name \"*[0-9][0-9]*\"'")
1 Like

This account does have permission to ssh to the servers and does not give errors with other commands. It has keys set up. I get the message

find: bad option 1143000000
find: [-H | -L] path-list predicate-list

---------- Post updated at 11:37 AM ---------- Previous update was at 11:05 AM ----------

This worked! Thanks, Elixer!:smiley: