Exec From Perl
2002-07-15 11:09:05
Category: perl:files
Description: Run external programs from your own program, store the output in a string and alter it. Use system() to fork a program and exec() to exit your program and never return.
Author: detour
Viewed: 7281
Rating: (15 votes)


#!/usr/bin/perl
#
# run.pl by detour@metalshell.com
#
# Execute another program using three different methods.
#
# http://www.metalshell.com/
 
use strict;
 
my $buff;
 
# commands inside `s are run externally
$buff = `ps -ax`;
# change any occurence of run.pl to hacker.pl to show we
# have full control over what gets printed.
$buff =~ s/run.pl/hacker.pl/g;
print $buff;
 
print "\nsystem() and exec() are the same except system\n";
print "will fork the program and return.\n\n";
 
print "The output from ls will be displayed but you can't\n";
print "alter it before it gets printed\n";
system("ls");
 
 
print "\nExec will not return to your program\n";
print "You are now in another shell, type 'exit'\n";
exec "/bin/sh", "-s";
print "This will not be seen.\n";