简体   繁体   English

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

[英]Cannot execute telnet commands using PHP shell_exec()

I am trying to do a mail verification using telnet and php. 我正在尝试使用telnet和php进行邮件验证。 The whole process words fine in the terminal, but when I use php shell_exec() , it only runs upto the Telnet connection command. 整个过程在终端中说得很好,但是当我使用php shell_exec() ,它只运行到Telnet连接命令。 Once telnet is connected, the remaining telnet specific commands don't work anymore. 连接telnet后,剩余的特定于telnet的命令将不再起作用。

Is there some other way that we need to execute telnet using php? 有没有其他方法,我们需要使用PHP执行telnet?

UPDATE : I am trying to replicate this tutorial. 更新 :我正在尝试复制教程。

Here's my Code 这是我的代码

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);
    }

And here's the response 这是回应

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_exec is not suitable for that (see Ulrich's explanation). shell_exec不适合(参见Ulrich的解释)。

fsockopen should do the trick. 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    
}

After taking some reference from @Lavi's answer, this is how I managed to solve the situation. 在从@Lavi的回答中得到一些参考后,这就是我设法解决问题的方法。 fputs did the trick, instead of fwrite fputs成功了,而不是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);
}

shell_exec() starts a new process. shell_exec()启动一个新进程。 It first starts a shell which then executes the given string as commands. 它首先启动一个shell,然后作为命令执行给定的字符串。 You can not use multiple shell_exec() commands to interact with the same shell, like remote-controlling a terminal. 您不能使用多个shell_exec()命令与同一个shell进行交互,例如远程控制终端。

What I would do in your case is to try to build on existing code. 在你的情况下我会做的是尝试建立现有的代码。 For example, there are a bunch of existing PHP SMTP libraries . 例如,有一堆现有的PHP SMTP库 I'm pretty sure that at least one of them will be able to do what you want or at least show you a way in which you could implement the missing features. 我很确定他们中至少有一个能够做你想做的事情,或者至少向你展示一种可以实现缺失功能的方法。

If you really insist on rolling your own stuff, check out the existing answer for eg interacting with command line program or PHP's expect module . 如果你真的坚持要自己编写东西,请查看现有的答案,例如与命令行程序或PHP的expect模块进行交互 Unless absolutely needed, I'd avoid this though, because it is inefficient. 除非绝对需要,否则我会避免这种情况,因为效率低下。 Also, webservers are often configured to disallow starting new processes for security reasons, so if your code works when you run it from the commandline but not inside the webserver, keep this difference in mind. 此外,Web服务器通常被配置为出于安全原因而禁止启动新进程,因此如果您的代码在从命令行运行而不是在Web服务器内部运行时,请记住这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM