How to execute command remotely as sudo and save the output locally?

Hello ,

I am trying to run a NetBackup command in remote server. Also this command can only be run by root so I am using sudo . Also I want the output of the command locally in a file.

The below command asked for password , ran successfully and showed Output on my local server screen

ssh username@hostname -t 'sudo /usr/openv/netbackup/bin/admincmd/bppllist -L -U -allpolicies'

Since I want to capture output in a file , I ran below :

 ssh username@hostname -t 'sudo /usr/openv/netbackup/bin/admincmd/bppllist -L -U -allpolicies > policydetails_tmp1.txt'

Error :

username@hostname's password:
The file access permissions do not allow the specified action.
ksh: policydetails_tmp1.txt: 0403-005 Cannot create the specified file.

I also ran below but it is running for long time. The command never takes long time.

ssh username@hostname -t 'sudo /usr/openv/netbackup/bin/admincmd/bppllist -L -U -allpolicies' > policydetails_tmp1.txt

Can someone please help me.

Thanks
Rahul

I think this command is what you wanted:

ssh username@hostname -t 'sudo /usr/openv/netbackup/bin/admincmd/bppllist -L -U -allpolicies' > policydetails_tmp1.txt

I'm not certain why it didn't work. Check in policydetails_tmp1.txt, maye your password prompt ended up inside it?

1 Like

it didn't work, because it should be:

ssh -t username@hostname 'command' >output
2 Likes