在PERL中工作,我正在尝试使用从html表单解析的变量标签来运行unix命令行。以下是HTML表单:
<?PHP
if ($_REQUEST['action'] == "submit") {
$var1 = $_REQUEST['var1'];
$var2 = $_REQUEST['var2'];
** working toward executing script "/usr/lib/cgi-bin/test.pl" with the above variables **
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test HTML Submit to CGI</title>
</head>
<body>
<form name-"Submit" id="Submit" method="POST">
<input type="hidden" name="action" value="submit" />
Input Variable 1<br /><br />
<input type="text" name="var1"></input><br /><br />
Input Variable 2<br /><br />
<input type="text" name="var2"></input><br /><br />
<input type="submit" value="Execute script" />
</form>
</body>
</html>
我正在寻找正确的语法来使用这些变量通过CGI脚本提交unix命令行。我有一个test.pl,其中包含以下内容:
use CGI;
$parse = new CGI;
$var1 = $parse->param('var1');
$var2 = $parse->param('var2');
if ($var1) {
if ($var2) {
echo "$var1" | UnixCommand --trigger +$var2
}
}
exit;
我只想在一些验证后写入PHP文件的顶部,才能从提交的表单中执行脚本。我试图在CGI中执行的unix命令要求var1周围的引号和$ var2之前的+是文字的,我认为这需要在CGI中进行某种追加或转义以使文本正确流动。是否需要使用某种转义变量来使字符串在命令行中正确执行?
您可以提供任何帮助,我们将不胜感激,
Silver Tiger
答案 0 :(得分:1)
#!/usr/bin/perl -T
use strictures;
use CGI qw();
use IPC::Run qw(run);
use autodie qw(:all run); # must come after importing 'run'
delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # perldoc perlsec
my $cgi = CGI->new;
# parameter restriction/untainting
my ($var1) = ($cgi->param('var1') =~ /\A ([A-Za-z]+) \z/msx);
my ($var2) = ($cgi->param('var2') =~ /\A ([A-Za-z]+) \z/msx);
if (defined $var1 && defined $var2) {
run # will autodie if return code is wrong
['/usr/bin/UnixCommand', '--trigger', '+'.$var2], # command and arguments as list
\$var1, # in
\*STDOUT; # out
} else {
die 'invalid parameters';
}
答案 1 :(得分:0)
1)为什么不到处使用php?
2)转义引号,然后使用open:
# replace all quotes/backslashes
$var2 =~ s/(['\\])/'\\$1'/g;
# open a command (it's popen(3) really)
open (my $pipe, "|-", "$command '$var2'")
or die "Cannot open $command: $!";
print $pipe, $var1; # not echo
3)旁注:总是放置
use warnings;
use strict;
你在perl脚本开头的。您错过错误的机会较少。
您还可以添加$parse->import_names("q");
,然后以[{1}}等方式访问vars,这比我一直调用$q::var1
更简单。