su using a shell script

Hello,

I am writing a script on server "A" to go to server "B" and run a command on server B. The output needs to be redirected to a file on server "A"

we do not have direct access from server A to server B.
we need to switch user (to dummy) on Server A and then using a sshX (alias created on server A to login to B but with dummy user profile) we can login to server B.

The below is the script code I am using on server A:

 
#!/usr/bin/ksh
set -x
su - dummy
 
sshX -n -l  dummy "ls -lrt" >> $HOME/tmp

Issue here is, it doesnt come out of second shell and does not run the sshX command as well unless i come out of second shell which is due to use of su.

I want to execute "ls -lrt" on server B and redirect output to tmp file and come out to same Server A.

Could you please guide on the same?

Thanks !!

su - dummy -c 'sshX -n dummy "ls -ltr"' > ${HOME}/tmp

This will save the output of the command as file in the current user's home directory.

Since you can run ssh, can you add the local user's ssh key to the remote user's authorized keys file? If so, then you can:

ssh -n dummy@remotehost ls -ltr > ${HOME}/tmp

no passwdless ssh can not be done.

with above it's not able to find out sshX :wall:

Actaully sshX is an alias created with dummy profile on server A.

With script execution, I am really not able to understand why its not able to find it out when we are saying su as well.

Use the path to sshX in the argument to the -c option. If you need to find it:

su - dummy
which sshX

Actaully sshX is an alias created with dummy profile on server A.

With script execution, I am really not able to understand why its not able to find it out when we are saying su as well.

If you can run su - dummy , run alias to get the list of aliases. If this is some sort of shell function, you might have to look at dummy's profile files.