下面的正则表达式确认传入的输入是否包含字符串'ping‘
elsif($RunCommand =~ m/^\s*ping\s+(.+)/)现在我想确认传递的输入是否包含管道命令|
以下内容似乎不能正常工作:
elsif($RunCommand =~ m/^\s*|\s+(.+)/)对于上下文,我有以下if,elseif if子例程。我只是在它的顶部添加了检查&或;或<或>或|的5个语句。但它不能正常工作...现在它总是转到&PrintPageHeaderBC("c");,并且包含ping、nslookup等(可接受的命令)的尝试现在不会执行它们应该执行的操作。我相信问题一定是我添加的5个正则表达式不能正确匹配输入的包含&或;或<或>或|的文本。有什么帮助吗?我确信我所做的5个单独的语句(可能是错误的)也可以组合成一个语句。
# First discard command attempts that contain & ; < > |, then acceptable commands are executed, then final else to non-functional command
if($RunCommand =~ m/^\s*&\s+(.+)/)
{
# Print PageHeaderBC that informs of non-functional command
&PrintPageHeaderBC("c");
}
elsif($RunCommand =~ m/^\s*;\s+(.+)/)
{
# Print PageHeaderBC that informs of non-functional command
&PrintPageHeaderBC("c");
}
elsif($RunCommand =~ m/^\s*<\s+(.+)/)
{
# Print PageHeaderBC that informs of non-functional command
&PrintPageHeaderBC("c");
}
elsif($RunCommand =~ m/^\s*>\s+(.+)/)
{
# Print PageHeaderBC that informs of non-functional command
&PrintPageHeaderBC("c");
}
elsif($RunCommand =~ m/^\s*|\s+(.+)/)
{
# Print PageHeaderBC that informs of non-functional command
&PrintPageHeaderBC("c");
}
# Now start acceptable commands
# PING
elsif($RunCommand =~ m/^\s*ping\s+(.+)/)
{
&PrintPageHeader("c");
#$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
$Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
print "<code>$Prompt $RunCommand</code><xmp>";
$Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
if(!$WinNT)
{
$SIG{'ALRM'} = \&CommandTimeout;
alarm($CommandTimeoutDuration);
}
if($ShowDynamicOutput) # show output as it is generated
{
$|=1;
$Command .= " |";
open(CommandOutput, $Command);
while(<CommandOutput>)
{
$_ =~ s/(\n|\r\n)$//;
print "$_\n";
}
$|=0;
}
else # show output after command completes
{
print `$Command`;
}
if(!$WinNT)
{
alarm(0);
}
print "</xmp>";
}
# TELNET
elsif($RunCommand =~ m/^\s*telnet\s+(.+)/)
{
&PrintPageHeader("c");
#$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
$Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
print "<code>$Prompt $RunCommand</code><xmp>";
$Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
if(!$WinNT)
{
$SIG{'ALRM'} = \&CommandTimeout;
alarm($CommandTimeoutDuration);
}
if($ShowDynamicOutput) # show output as it is generated
{
$|=1;
$Command .= " |";
open(CommandOutput, $Command);
while(<CommandOutput>)
{
$_ =~ s/(\n|\r\n)$//;
print "$_\n";
}
$|=0;
}
else # show output after command completes
{
print `$Command`;
}
if(!$WinNT)
{
alarm(0);
}
print "</xmp>";
}
#DIG
elsif($RunCommand =~ m/^\s*dig\s+(.+)/)
{
&PrintPageHeader("c");
#$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
$Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
print "<code>$Prompt $RunCommand</code><xmp>";
$Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
if(!$WinNT)
{
$SIG{'ALRM'} = \&CommandTimeout;
alarm($CommandTimeoutDuration);
}
if($ShowDynamicOutput) # show output as it is generated
{
$|=1;
$Command .= " |";
open(CommandOutput, $Command);
while(<CommandOutput>)
{
$_ =~ s/(\n|\r\n)$//;
print "$_\n";
}
$|=0;
}
else # show output after command completes
{
print `$Command`;
}
if(!$WinNT)
{
alarm(0);
}
print "</xmp>";
}
#NSLOOKUP
elsif($RunCommand =~ m/^\s*nslookup\s+(.+)/)
{
&PrintPageHeader("c");
#$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
$Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
print "<code>$Prompt $RunCommand</code><xmp>";
$Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
if(!$WinNT)
{
$SIG{'ALRM'} = \&CommandTimeout;
alarm($CommandTimeoutDuration);
}
if($ShowDynamicOutput) # show output as it is generated
{
$|=1;
$Command .= " |";
open(CommandOutput, $Command);
while(<CommandOutput>)
{
$_ =~ s/(\n|\r\n)$//;
print "$_\n";
}
$|=0;
}
else # show output after command completes
{
print `$Command`;
}
if(!$WinNT)
{
alarm(0);
}
print "</xmp>";
}
#HOST
elsif($RunCommand =~ m/^\s*host\s+(.+)/)
{
&PrintPageHeader("c");
#$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
$Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
print "<code>$Prompt $RunCommand</code><xmp>";
$Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
if(!$WinNT)
{
$SIG{'ALRM'} = \&CommandTimeout;
alarm($CommandTimeoutDuration);
}
if($ShowDynamicOutput) # show output as it is generated
{
$|=1;
$Command .= " |";
open(CommandOutput, $Command);
while(<CommandOutput>)
{
$_ =~ s/(\n|\r\n)$//;
print "$_\n";
}
$|=0;
}
else # show output after command completes
{
print `$Command`;
}
if(!$WinNT)
{
alarm(0);
}
print "</xmp>";
}
#NMAP
elsif($RunCommand =~ m/^\s*nmap\s+(.+)/)
{
&PrintPageHeader("c");
#$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
$Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
print "<code>$Prompt $RunCommand</code><xmp>";
$Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
if(!$WinNT)
{
$SIG{'ALRM'} = \&CommandTimeout;
alarm($CommandTimeoutDuration);
}
if($ShowDynamicOutput) # show output as it is generated
{
$|=1;
$Command .= " |";
open(CommandOutput, $Command);
while(<CommandOutput>)
{
$_ =~ s/(\n|\r\n)$//;
print "$_\n";
}
$|=0;
}
else # show output after command completes
{
print `$Command`;
}
if(!$WinNT)
{
alarm(0);
}
print "</xmp>";
}
#TRACEROUTE
elsif($RunCommand =~ m/^\s*traceroute\s+(.+)/)
{
&PrintPageHeader("c");
#$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
$Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
print "<code>$Prompt $RunCommand</code><xmp>";
$Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
if(!$WinNT)
{
$SIG{'ALRM'} = \&CommandTimeout;
alarm($CommandTimeoutDuration);
}
if($ShowDynamicOutput) # show output as it is generated
{
$|=1;
$Command .= " |";
open(CommandOutput, $Command);
while(<CommandOutput>)
{
$_ =~ s/(\n|\r\n)$//;
print "$_\n";
}
$|=0;
}
else # show output after command completes
{
print `$Command`;
}
if(!$WinNT)
{
alarm(0);
}
print "</xmp>";
}
#WHOIS
elsif($RunCommand =~ m/^\s*whois\s+(.+)/)
{
&PrintPageHeader("c");
#$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
$Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
print "<code>$Prompt $RunCommand</code><xmp>";
$Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
if(!$WinNT)
{
$SIG{'ALRM'} = \&CommandTimeout;
alarm($CommandTimeoutDuration);
}
if($ShowDynamicOutput) # show output as it is generated
{
$|=1;
$Command .= " |";
open(CommandOutput, $Command);
while(<CommandOutput>)
{
$_ =~ s/(\n|\r\n)$//;
print "$_\n";
}
$|=0;
}
else # show output after command completes
{
print `$Command`;
}
if(!$WinNT)
{
alarm(0);
}
print "</xmp>";
}
else
{
# Print PageHeaderBC that informs of non-functional command
&PrintPageHeaderBC("c");
}
&PrintCommandLineInputForm;
&PrintPageFooter;}
发布于 2011-08-29 00:41:05
在正则表达式中,|符号用于alternation。如果想要匹配文字|字符,则需要对其进行转义:
m/^\s*\|\s+(.+)/此外,如果我要冒险猜测,我会说它总是落入“失败”子例程的原因是因为那个正则表达式。通过交替(如所写的),它可以匹配任何以\s*或\s+(.+)开头的字符串,这实际上是由于\s*中的星号运算符而导致的任何字符串。
编辑:关于您的评论,由于锚定(^),这些示例将不会与您的任何正则表达式匹配。如果我们以字符串ping 4.2.2.2; pwd为例,发生的情况是正则表达式引擎从字符串的开头开始匹配,因为它是锚定的。初始\s*通过重复零次来正确匹配,在这种情况下实际上什么也不做。然后它查找要匹配的;,但是下一个字符是4,因此它失败了。您提供的第二个字符串也是如此。由于您使用了\s+,如果;后面没有空格,它也会中断。
老实说,如果你真的只想阻止任何字符串,甚至包含字符<,>,;,|或&,最简单的事情是将它们都放入一个字符类中,并使用m/[&;|<>]/g进行匹配。因为这个正则表达式是未锚定的,所以它将在字符串中的任何位置匹配。但是,这将完全禁止这些字符,这意味着它们不能出现在字符串或任何类似的内容中。这可能不是您想要的,但它将应用于您当前声明的规则(“五个正则表达式应与包含&或;或<或>或|的任何文本匹配”)。
发布于 2011-08-29 00:41:59
elsif($RunCommand =~ m/^\s*|\s+(.+)/)需要满足以下条件:
elsif($RunCommand =~ m/^\s*\|\s+(.+)/)|是正则表达式中的逻辑'or‘运算符,所以如果您试图匹配它,则需要对其进行转义
发布于 2011-08-29 00:40:26
您需要使用\对特殊字符进行转义。例如,要搜索|,可以像这样对其进行转义,\|。因此,您的正则表达式匹配为:
m/^\s*\|\s+(.+)/以下字符在regex中都有特殊含义,如果您希望按字面解释它们,则需要对它们进行转义:
* ? + [ ] ( ) { } ^ $ | \https://stackoverflow.com/questions/7222263
复制相似问题