如果可以,可以用PHP访问wifi路由器吗?我想用连接到本地主机/服务器的PHP获取wifi路由器信息吗? 我想用PHP访问wifi路由器并获取有关路由器的所有信息 我正在使用PHP进行地理围栏项目 我搜索了不同的PHP网络功能,还研究了PHP中的套接字编程,但没有明确的前进方向
获取主机的IP地址,但我需要有关路由器的信息
$myIP = gethostbyname(trim(`hostname`));
echo $myIP;dd();
<?php
简单代码示例:
$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";
?>
答案 0 :(得分:1)
当今大多数路由器都具有SSDP服务。基本上,您向广播地址发送了一个小数据包,许多设备都会对此做出响应。您的路由器就是其中之一。
我的路由器将自己暴露为“ urn:dslforum-org:device:InternetGatewayDevice:1”,SSDP只能为您过滤该设备。这个快速而肮脏的代码对我有用:
<?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);
因此,现在您有了路由器的IP地址。使用UPnP,您现在可以查询路由器。我的路由器通过UPnP提供了很多信息,其中一些是:
答案 1 :(得分:0)
您的第一步将是找到路由器的IP。为此,您需要查看路由表并查找“默认网关”。
获取路由表的一种方法是运行shell命令,例如使用variable "cluster_member_count" {
default = "2"
}
variable "cluster_member_name_prefix" {
default = "member-"
}
variable "aws_keypair_privatekey_filepath" {
default = "../mykey.pem"
}
resource "aws_instance" "example" {
//
}
resource "null_resource" "hosts_file" {
count = "${var.cluster_member_count}"
# Changes to any instance of the cluster requires re-provisioning
triggers {
cluster_instance_ids = "${join(",", aws_instance.example.*.id)}"
}
connection {
type = "ssh"
host = "${element(aws_instance.example.*.public_ip, count.index)}"
user = "ec2-user"
private_key = "${file(var.aws_keypair_privatekey_filepath)}"
}
provisioner "remote-exec" {
inline = [
# Adds all cluster members' IP addresses to /etc/hosts (on each member)
"echo '${join("\n", formatlist("%v", aws_instance.example.*.private_ip))}' | awk 'BEGIN{ print \"\\n\\n# Cluster members:\" }; { print $0 \ ${var.cluster_member_name_prefix}\ NR-1 }' | sudo tee -a /etc/hosts > /dev/null",
]
}
}
。要运行的命令取决于操作系统。
shell_exec
netstat -rn
ip route