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

标签 php networking telnet email-validation shell-exec

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

我们需要使用其他方式使用 php 执行 telnet 吗?

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

这是我的代码

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_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    
}

关于php - 无法使用 PHP shell_exec() 执行 telnet 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33952352/

相关文章:

networking - Winsock bind() 失败,WSAEADDRNOTAVAIL 用于定向广播地址

networking - 如何从外部访问我的 PC 上运行的 Java servlet?

java - 无法从我的桌面连接到 ec2 上的 rabbitmq

php - 如何在不发送电子邮件的情况下检查电子邮件地址是否存在?

oracle - 如何使用 telnet 检查与 Oracle 的连接?

php - 无捆绑 Symfony 4 中的整体结构

php - 尝试分配内存不足-API YouTube php(上传视频)

javascript - FormData 添加文件不起作用

php - 将超链接元素传递到超链接页面并在超链接页面接收该元素

networking - 通过 Telnet 运行应用程序