【问题标题】:PHP Script to Traceroute?跟踪路由的 PHP 脚本?
【发布时间】:2012-09-17 16:51:05
【问题描述】:

我有一个在 GoDaddy 共享 linux 服务器上运行 PHP 的网站。我需要确定用户是否连接到公司 VPN。如果我只是简单地做 $_SERVER['REMOTE_ADDR'] 它会给我客户端的 IP 地址。但是,如果我可以使用跟踪器进行更深入的挖掘,第二跳将显示为公司 IP 地址。

是否可以使用 PHP 从网页进行跟踪路由以确定用户是否连接到公司的拆分隧道 VPN?

【问题讨论】:

    标签: php vpn traceroute


    【解决方案1】:

    在 PHP 中创建跟踪路由程序

    http://www.adayinthelifeof.nl/2010/07/30/creating-a-traceroute-program-in-php/

    如果网站出现故障,这是 jist:

    <?php
    
    define ("SOL_IP", 0);
    define ("IP_TTL", 2);    // On OSX, use '4' instead of '2'.
    
    $dest_url = "www.google.com";   // Fill in your own URL here, or use $argv[1] to fetch from commandline.
    $maximum_hops = 30;
    $port = 33434;  // Standard port that traceroute programs use. Could be anything actually.
    
    // Get IP from URL
    $dest_addr = gethostbyname ($dest_url);
    print "Tracerouting to destination: $dest_addr\n";
    
    $ttl = 1;
    while ($ttl < $maximum_hops) {
        // Create ICMP and UDP sockets
        $recv_socket = socket_create (AF_INET, SOCK_RAW, getprotobyname ('icmp'));
        $send_socket = socket_create (AF_INET, SOCK_DGRAM, getprotobyname ('udp'));
    
        // Set TTL to current lifetime
        socket_set_option ($send_socket, SOL_IP, IP_TTL, $ttl);
    
        // Bind receiving ICMP socket to default IP (no port needed since it's ICMP)
        socket_bind ($recv_socket, 0, 0);
    
        // Save the current time for roundtrip calculation
        $t1 = microtime (true);
    
        // Send a zero sized UDP packet towards the destination
        socket_sendto ($send_socket, "", 0, 0, $dest_addr, $port);
    
        // Wait for an event to occur on the socket or timeout after 5 seconds. This will take care of the
        // hanging when no data is received (packet is dropped silently for example)
        $r = array ($recv_socket);
        $w = $e = array ();
        socket_select ($r, $w, $e, 5, 0);
    
        // Nothing to read, which means a timeout has occurred.
        if (count ($r)) {
            // Receive data from socket (and fetch destination address from where this data was found)
            socket_recvfrom ($recv_socket, $buf, 512, 0, $recv_addr, $recv_port);
    
            // Calculate the roundtrip time
            $roundtrip_time = (microtime(true) - $t1) * 1000;
    
            // No decent address found, display a * instead
            if (empty ($recv_addr)) {
                $recv_addr = "*";
                $recv_name = "*";
            } else {
                // Otherwise, fetch the hostname for the address found
                $recv_name = gethostbyaddr ($recv_addr);
            }
    
            // Print statistics
            printf ("%3d   %-15s  %.3f ms  %s\n", $ttl, $recv_addr,  $roundtrip_time, $recv_name);
        } else {
            // A timeout has occurred, display a timeout
            printf ("%3d   (timeout)\n", $ttl);
        }
    
        // Close sockets
        socket_close ($recv_socket);
        socket_close ($send_socket);
    
        // Increase TTL so we can fetch the next hop
        $ttl++;
    
        // When we have hit our destination, stop the traceroute
        if ($recv_addr == $dest_addr) break;
    }
    
    ?>
    

    你需要是根用户。这意味着从网络服务器运行它时它可能不起作用,你必须从命令行运行它:

    jthijssen@tarabas:~/traceroute$ sudo php traceroute.php
    Tracerouting to destination: 199.6.1.164
      1   192.168.1.1      0.004 ms  192.168.1.1
      2   *                0.005 ms  static.kpn.net
      3   (timeout)
      4   139.156.113.141  0.005 ms  nl-asd-dc2-ias-csg01-ge-3-2-0-kpn.net
      5   195.190.227.221  0.005 ms  asd2-rou-1022.nl.euroringen.net
      6   134.222.229.105  0.005 ms  asd2-rou-1001.NL.eurorings.net
      7   134.222.97.186   0.007 ms  kpn-1402.xe-0-0-0.jun1.galilei.network.bit.nl
      8   213.154.236.75   0.012 ms  213.154.236.75
      9   199.6.1.164      0.012 ms  pub3.kernel.org
    

    这是到 www.kernel.org 的跟踪路由。我删除了第二个跃点(因为那是我所在的 IP)。第三跳返回超时。可能那里的站点没有返回 ICMP 数据包以供使用。

    以上代码可以在github上找到:https://github.com/jaytaph/traceroute

    【讨论】:

    • 你好@user1061001 你的问题解决了吗?你能接受正确的答案吗?
    【解决方案2】:

    您可以使用 PHP 在服务器上运行 traceroute 命令。

    <?php
    $output = shell_exec("/usr/sbin/traceroute target");
    echo "<pre>$output</pre>";
    ?>
    

    然后解析$output中的输出。

    来源:http://php.net/manual/en/function.shell-exec.php

    【讨论】:

    • 感谢您的回复。我可以运行 $output = shell_exec('ls -lart'); (如手册所示)。但是,当我尝试使用 shell_exec 跟踪路由或 ping 时,没有返回任何内容。似乎是权限问题 - forums.cpanel.net/f5/… 有没有办法克服它?
    • 通过 shell_exec 运行命令 whoami 以了解有关您登录的用户的更多信息。通过 shell_exec 使用 which traceroute 和 which ping 以确保您指向正确的位置。然后使用ls -l /usr/sbin/traceroute 检查这些文件的权限。您可能希望在调用 traceroute 之前使用 sudo。我们需要知道您的 linux 版本。所以尝试使用命令uname -a。尝试使用终端登录服务器,看看是否可以运行这些命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 2021-11-22
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多