2

I am trying to connect to a remote host as a user. Now i want to run a few commands that need sudo access. The commands are:

export http_proxy=http://xxxx.xx.xx.com:8080
export https_proxy=http://xxxx-xxxx.xx.xx.com:8080
sudo /etc/init.d/filebeat restart
sudo tail -f /var/log/filebeat/filebeat

I tried executing this using

my $ssh = Net::OpenSSH->new("$username\@$Hostname", password => $pass);
my @cmdF = "/etc/init.d/filebeat restart";
print "Restarting filebeat for $webHosts[$i]";
my @cmdF = "/etc/init.d/filebeat restart";
my $outputF = $ssh->capture({stdin_data=>"$pass\n"}, 'sudo', '-Sk', '-p', '', '-', @cmdF);
print "\nCurrent Filebeat Status : $outputF\n\n";

I took just one command at the moment but ended up getting this error:

sudo: /etc/init.d/filebeat restart: command not found

Please help me with the same for all commands..

Thank you in advance.

2
  • Try my @cmdF = ("/etc/init.d/filebeat" "restart") instead, i.e. separat command and command arguments in different list entries. Otherwise sudo will look a file named /etc/init.d/filebeat restart to execute - which does not exist Commented Mar 27, 2023 at 10:24
  • What is $ssh? Commented Mar 27, 2023 at 16:10

1 Answer 1

1

sudo /etc/init.d/filebeat restart consists of three arguments (including the command):

  1. sudo
  2. /etc/init.d/filebeat
  3. restart

You execute these instead:

  1. sudo
  2. -Sk
  3. -p
  4. Empty string
  5. -
  6. /etc/init.d/filebeat restart

Most relevant is that you combined two arguments into one, producing garbage.

What you need:

my $commands = <<'.';
export http_proxy=http://xxxx.xx.xx.com:8080
export https_proxy=http://xxxx-xxxx.xx.xx.com:8080
sudo /etc/init.d/filebeat restart
sudo tail -f /var/log/filebeat/filebeat
.

$ssh->capture( 'sh', '-c', $commands );
Sign up to request clarification or add additional context in comments.

4 Comments

How should I pass the password in this case then?
Are you saying you want to execute different commands than the ones you said you wanted to execute? Feel free to fix your question if that's the case.
I wish to run these commands as a root user on a remote host for which I was passing an argument called $pass.
I copied the commands you posted exactly.commands you posted exactly. There's no password argument. If you want to run sh itself as root, feel free to use the approach you were using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.