Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:19:23+00:00 2026-05-24T05:19:23+00:00

I’m trying to run various commands with net:ssh2 however the commands need to be

  • 0

I’m trying to run various commands with net:ssh2 however the commands need to be run as root. I have root logins disabled over SSH and I have keys setup, so I can ssh and use ‘sudo su – root’ to get root access without a password. The problem is that I need to run multiple commands so I was wondering if it’s possible to connect, then sudo to root, then run all the commands I need without having to pipe an echo to sudo or use expect.

The relevant section of my code I’m working with is the following, I’d preferably like to
sudo before the exec($commands[0]) if possible.

EDIT: I can’t login directly to root with or without a password as it’s against policy unfortunately. I have to connect with my user first.

    my $ssh2 = Net::SSH2->new();
    $ssh2->connect($server) or die "$!\n";
    if ($ssh2->auth_publickey($user,"/home/$user/.ssh/id_rsa.pub", "/home/$user/.ssh/id_rsa"))
    {
            my $chan = $ssh2->channel();
            $chan->blocking(1);
            $chan->exec($commands[0]);
            my @users;
            while(<$chan>) {push(@users,$_);}
            my $stdout=join("",@users);
            print $stdout;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:19:24+00:00Added an answer on May 24, 2026 at 5:19 am

    I didn’t try this, but su has the -c argument, which passes a single command to the shell to execute, and then finishes the shell. This most likely corresponds to the -c argument of your shell, which in fact happily accepts compound commands here, e.g. a list of commands separated by ;.

    Thus executing

    sudo su -c 'command1 ; command2 ; command3 ' - root
    

    could work here. (Or even only one of those commands, and then repeatedly call these.)

    (But I think there might be something wrong with the settings if they allow a passwordless sudo su - root, but not normal sudo command executing.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.