melaniec has asked for the wisdom of the Perl Monks concerning the following question:

I need to run a shell script on a remote machine, but it needs to be run using the 'sudo' command. So my question is, how can i run this remotely AND also feed input to the 'sudo' command remotely?
  • Comment on How to remotely execute a command that requires input?

Replies are listed 'Best First'.
Re: How to remotely execute a command that requires input?
by Tanktalus (Canon) on Sep 14, 2006 at 19:39 UTC

    What have you tried so far?

    You can do this so many different ways that it would help if you narrowed down your plans. You could use Net::Telnet to use the telnet protocol to log in and run the script, and that would allow you to communicate with the remote server. Or you could use Expect to communicate with the telnet program locally which would then connect to the remote server ...

    Or you could connect via rcmd (rsh/rlogin), or ssh. Or you could have a CGI script running on the server that does it for you. Or you could have an entire daemon written in perl for running this - it would run everything local to itself, and receive input from your machine over the network much like a web server.

    A bit more detail of your plans would help us help you formulate the rest of your plans.

      Well i'm running the perl script locally on a server, what it is doing is ssh-ing into a 2nd server and running a shell script that requires input. While i've had success i locally opening pipes and feeding input into them, i've yet to successfully do this on a remote machine. system("ssh xxx.xxx.xxx.xxx -l user '..shell script that needs input..'"); thanks!

        How do you do this with local programs? Why do you think that remote programs will be any different? Just remember: ssh is, itself, a local program. Treat it as such, and you may find things are much simpler.

        If you use open("| localprogram") to write to a local program, just use open("|ssh xxx.xxx.xxx.xxx -l user 'shell script that needs input'") to run the local program of 'ssh'.

        (PS: you should respond sooner ... as you notice, most new topics get responses fairly quickly - 12 days is more than enough time for responses. In fact, if you don't have a response within 24 hours, chances of getting any responses goes way down.)

Re: How to remotely execute a command that requires input?
by sgifford (Prior) on Sep 14, 2006 at 19:47 UTC
    Assuming you're using ssh, your best bet is to take advantage of the facilities it already has for doing this, such as setting up public key authentication.

    Update: To clarify, ssh's authentication facilities will help run the remote script as the right user to begin with, replacing sudo; they won't help you simulate keyboard input to another script. For that, use ssh -t and Expect.

Re: How to remotely execute a command that requires input?
by zentara (Cardinal) on Sep 14, 2006 at 20:56 UTC
    Shouldn't you be running this thru SSH? I would recommend looking at A little demo for Net::SSH2 and learn to setup a channel. Of course, you need a ssh2 server to connect to.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum