无法使用PHP shell_exec()执行telnet命令

无法使用PHP shell_exec()执行telnet命令,php,networking,telnet,email-validation,shell-exec,Php,Networking,Telnet,Email Validation,Shell Exec,我正在尝试使用telnet和php进行邮件验证。整个过程在终端中很好,但是当我使用phpshell\u exec()时,它只运行到Telnet connection命令。一旦连接了telnet,其余的telnet特定命令将不再工作 我们是否需要使用php以其他方式执行telnet 更新:我正在尝试复制教程 这是我的密码 public function mailtest(Request $request){ //Taking input email $email =

我正在尝试使用telnet和php进行邮件验证。整个过程在终端中很好,但是当我使用php
shell\u exec()
时,它只运行到Telnet connection命令。一旦连接了telnet,其余的telnet特定命令将不再工作

我们是否需要使用php以其他方式执行telnet

更新:我正在尝试复制教程

这是我的密码

public function mailtest(Request $request){
        //Taking input email
        $email = $request->input("email");
        $divide = explode("@", $email);
        //Find out the Domain
        $server = $divide[1];
        $response = shell_exec("nslookup -q=mx $server");
        //Response of the nslookup
        print_r($response);
        $mailServerList = explode(PHP_EOL, $response);
        $line = $mailServerList[4];

        $serverArr = preg_split('/\s+/', $line);
        $n = sizeof($serverArr);
        $mailServer = $serverArr[$n-1];
        //Printing out the mail server out of the nslookup response
        print_r($mailServer);
        //Executing Telnet command
        $telnet = shell_exec("telnet $mailServer 25");
        print_r("telnet response ".$telnet);

        //Telnet Helo
        $helo = shell_exec("Helo testing.com");
        print_r("Helo response ".$helo);
        //Telnet mail from 
        $from = shell_exec('mail from: testing@gmail.com');
        print_r("MAil from response ".$from);
        //Telnet RCPT to
        $finalResponse = shell_exec("rcpt to: $email");
        print_r("Mail to response ".$finalResponse);
    }
下面是答案

Server:     10.0.80.11
Address:    10.0.80.11#53

Non-authoritative answer:
gmail.com   mail exchanger = 5 gmail-smtp-in.l.google.com.
gmail.com   mail exchanger = 10 alt1.gmail-smtp-in.l.google.com.
gmail.com   mail exchanger = 40 alt4.gmail-smtp-in.l.google.com.
gmail.com   mail exchanger = 30 alt3.gmail-smtp-in.l.google.com.
gmail.com   mail exchanger = 20 alt2.gmail-smtp-in.l.google.com.

Authoritative answers can be found from:
gmail-smtp-in.l.google.com  internet address = 173.194.64.27
gmail-smtp-in.l.google.com  has AAAA address 2607:f8b0:4003:c02::1a
alt1.gmail-smtp-in.l.google.com internet address = 173.194.219.27
alt1.gmail-smtp-in.l.google.com has AAAA address 2607:f8b0:4002:c03::1b
alt4.gmail-smtp-in.l.google.com internet address = 64.233.190.26
alt4.gmail-smtp-in.l.google.com has AAAA address 2800:3f0:4003:c01::1b
alt3.gmail-smtp-in.l.google.com internet address = 74.125.141.26
alt3.gmail-smtp-in.l.google.com has AAAA address 2607:f8b0:400c:c06::1a
alt2.gmail-smtp-in.l.google.com internet address = 173.194.205.27
alt2.gmail-smtp-in.l.google.com has AAAA address 2607:f8b0:400d:c02::1b

gmail-smtp-in.l.google.com.telnet response Trying 2607:f8b0:4003:c02::1a...
Connected to gmail-smtp-in.l.google.com.
Escape character is '^]'.
Helo response 
MAil from response No message, no subject; hope that's ok
Mail to response 
shell\u exec()
启动一个新进程。它首先启动一个shell,然后将给定的字符串作为命令执行。不能使用多个
shell\u exec()
命令与同一个shell交互,就像远程控制终端一样

在您的情况下,我将尝试在现有代码的基础上进行构建。例如,有一堆现有的。我非常肯定,其中至少有一个能够实现您想要的功能,或者至少向您展示一种可以实现缺少的功能的方法


如果你真的坚持使用自己的东西,请查看现有的答案,例如或PHP的。除非绝对需要,否则我会避免这样做,因为这样做效率很低。此外,出于安全原因,Web服务器通常被配置为不允许启动新进程,因此,如果您的代码在从命令行而不是在Web服务器内部运行时工作,请记住这一区别。

shell\u exec不适合这样做(请参阅Ulrich的解释)

fsockopen
应该可以做到这一点

$fp = @fsockopen('yourMailServer.com', 9001);      

if ($fp) { 
    fwrite($fp, "username\n");
    fwrite($fp, "password\n");

    while ($line = fread($fp, 2048)) {    
       // do things with $line    
    }    
} else {    
    //return error    
}

在参考了@Lavi的答案之后,这就是我如何解决问题的方法<代码>FPUT完成了这一技巧,而不是
fwrite

$connect = @fsockopen($mailServer, 25);
if($connect){
    fputs ($connect , "HELO $mailServer\r\n");
    $out = fgets ($connect, 1024);
    $details .= $out."\n";

    fputs ($connect , "MAIL FROM: <$fromemail>\r\n");
    //$from = fgets ($connect, 1024);
    fputs ($connect , "RCPT TO: <$toemail>\r\n");
    //$to = fgets ($connect, 1024);
    fputs ($connect , "QUIT");
    fclose($connect);
}
$connect=@fsockopen($mailServer,25);
如果($connect){
fputs($connect,“HELO$mailServer\r\n”);
$out=fgets($connect,1024);
$details.=$out.“\n”;
fputs($connect,“邮件发件人:\r\n”);
//$from=fgets($connect,1024);
fputs($connect,“RCPT TO:\r\n”);
//$to=fgets($connect,1024);
fputs($connect,“QUIT”);
fclose($connect);
}

也可以尝试php的
imap
扩展和
imap.*
functions@AlexAndrei对不起,它到底是做什么的?该扩展提供的功能允许您通过
IMAP
协议进行操作,请参见这里@AlexAndrei对不起,我对此不太清楚,但是,你能告诉我它是否有助于电子邮件验证吗?或者执行Telnet命令?基本上我想做一个
Telnet myMailServer.com 25
来启动连接,然后从
Helo hi
开始,依此类推,如图所示。我尝试使用
fsockopen
,但似乎不起作用。你能检查一下教程,看看我应该如何使用
fsockopen
或者你认为可以达到目的的任何方式吗?对于教程中的每一条绿线,你应该调用一个fwrite命令。对于每一条蓝线,您应该阅读(并根据)while循环中的$line参数成功了,我从telnet yourMailServer.com 25开始?或者,
fsockopen
命令是否具有相同的用途?fsockopen取代了telnet。它打开了一个套接字。这非常有效,但我必须更改FGET的位置,因为其中4个FGET需要获取所有输出的信息。这也是从有效电子邮件到无效电子邮件的同步::(