Reputation: 11
Is it possible to access wifi router in PHP if yes then how?
Code to get the IP address of host (but I want information about a router):
$myIP = gethostbyname(trim(`hostname`));
echo $myIP;dd();
<?php
Simple Code Example:
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_bind($socket,'0.0.0.0',150);
socket_getsockname($socket, $IP, $PORT);
print $IP.":".$PORT."\n";
?>
Upvotes: 1
Views: 2723
Reputation: 2073
Most routers nowadays have an SSDP service. Basically, you sent a small packet to a broadcast address and a lot of devices will respond to it. Your router will be one of them.
My router exposes itself as an "urn:dslforum-org:device:InternetGatewayDevice:1" and SSDP can filter for you for only that device. This quick and dirty piece of code works for me:
<?php
$socket = socket_create(AF_INET, SOCK_DGRAM, getprotobyname('udp'));
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, true);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=> 1, 'usec'=>'0'));
//socket_set_option($socket, IPPROTO_IP, IP_MULTICAST_IF, "xxxx"); //only if you have multiple network cards
$data = "M-SEARCH * HTTP/1.1\n".
"HOST: 239.255.255.250:1900\n".
"MAN: \"ssdp:discover\"\n".
"MX: 1\n".
"ST: urn:dslforum-org:device:InternetGatewayDevice:1\n\n";
socket_sendto($socket, $data, strlen($data), 0, "239.255.255.250", "1900");
socket_recvfrom($socket, $mess, 1024, 0, $ip, $port);
echo $mess;
echo $ip;
socket_close($socket);
So now you have the ip address of your router. Using UPnP you can now query your router. My router offers a lot of information through UPnP, some of which are:
Upvotes: 2
Reputation: 111349
Your first step would be finding the IP of your router. For this you'd look at the routing table and look for the "default gateway."
One way to get the routing table is running a shell command for example with shell_exec
. The command to run depends on the o.s.
netstat -rn
ip route
route print
Upvotes: 0