Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Possible Duplicate:
scp to remote server with sudo

What is the best way of doing SCP from one box to the other as a sudo user. There are two servers

Server A

10.152.2.10

/home/oracle/export/files.txt

User : deploy

Server B

10.152.2.11

/home/oracle/import/

User : deploy
Sudo user : /usr/local/bin/tester

all i want is to copy files from server A to Server B as a sudo user...

In order to do this, first i normally login as deploy user on the target server and then switch as a sudo user without password.

after that SCP to copy file, this is the normal way i perform this activity...

In order to auotmate i have written script

#!/bin/sh
ssh deploy@lnx120 
sudo /usr/local/bin/tester "./tester/deploy.sh"

I have generated the private key for deploy user, so it allows me to login as deploy user without password. afterthar the sudo command is executed it will switch the user to tester...

after that nothing happens.. i mean the script is not getting executed ... is there any way to accomplish this in a different way...

share|improve this question

migrated from stackoverflow.com Dec 25 '09 at 15:04

This question came from our site for professional and enthusiast programmers.

marked as duplicate by quack quixote May 8 '10 at 0:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Please clarify/simplify what you are trying to do. I am finding the use of the term "sudo user" to be ambiguous. It seems that you are trying to automate an scp transfer, but what are the other limiting factors? –  pcapademic Dec 25 '09 at 22:21
add comment

2 Answers

You can make a wrapper around ssh, like this:

#!/usr/bin/perl
use strict;
my @newargs = ( "ssh" );
my $added = 0;
for my $arg (@ARGV) {
    # replace "scp" with "sudo scp"
    if ($arg =~ /^scp / and $added == 0) {
        $arg =~ s/^/sudo /;
        $added = 1;
    }
    push @newargs, $arg;
}
exec @newargs;

Make it executable, and run scp like this:

scp -S ./ssh-wrapper somefile anotherfile hostname.domain.tld:path/

This will only work if sudo on the remote server doesn't require a password (or if it's cached), but better than nothing.

share|improve this answer
add comment

If I understand correctly, you need sudo because the file is somewhere your regular user does not have access to? And you do not have the root password, or do not want to use it for some reason?

One solution would be to copy the file to a location where you regular user has access to (and if necessary change the access rights on the file as well), and then perform the scp.

share|improve this answer
1  
SSH is likely disabled for root. –  Nerdling Dec 25 '09 at 15:50
add comment