Malfist
Malfist

Reputation: 31795

Using perl to run more than one system command in a row

I'm trying to script some things on my team's server and having some difficulty.

For part of what I'm scripting, I need to have the user switch user via sudo su APPUSER and then run several commands. However, sudo is rather locked down on our server and I cannot run something like: sudo su APPUSER -c COMMAND. I've asked for that to be fixed, by the system admins always say, "no".

I am currently using perl to do the scripting, and I need to drop into the appuser instead of the developer's user to run about three commands. I can do it on the command line by executing each command in succession, but I do not know how to get perl to do that.

How can I make perl execute more than one command on the command line in a way that it does not discard the shell/state after the previous execution? i.e., running multiple system calls doesn't work.

Upvotes: 1

Views: 2361

Answers (2)

Malfist
Malfist

Reputation: 31795

I found the solution.

Open a bash process and read and write to it:

#!/usr/bin/perl
use strict;
use warnings;

open(BASH, "|-", "bash");

print BASH "sudo su APPUSER\n";
print BASH "touch /home/APPUSER/sudobashtest\n";
close(BASH);

Upvotes: 1

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24063

Does this not work?

system("sudo su APPUSER && command1 && command2");

Upvotes: 0

Related Questions