hig*_*guy 3 linux networking wifi ssh
I have a very typical situation in which I want to connect an (android) phone to a linux notebook in the same (wifi) network using ssh. The IP's are assigned by DHCP so I know only the one of the client in advance. Knowing the IP of the client I could in principle loop a ssh command over all possible IP's in the same net to see if a server is listening there. However this seems highly inefficient. So, how can I figure out the IP of my host (knowing it's MAC address) so I can connect to it using ssh? I know about other software which achieves something like this so it must be possible. Related: can I dynamically assign a hostname to this IP on the client (assuming it is Linux) so that I can use a static entry in the ssh config file?
您可以通过以下两个步骤来完成:
扫描您的子网以填充您的 ARP 缓存。有方法,但我建议fping。通过在终端中运行以下命令在 Ubuntu 上安装它:
apt-get install fping
Run Code Online (Sandbox Code Playgroud)
...然后扫描您的网络(例如,子网 192.168.10.0/24):
fping -g 192.168.10.0/24
Run Code Online (Sandbox Code Playgroud)
现在 ARP 缓存中填满了子网中设备的 MAC 地址。
在 ARP 缓存上应用适当的过滤器以仅查看目标设备。只需使用以下命令(aa:bb:cc:dd:ee:ff设备的 MAC在哪里):
arp -n | grep -i aa:bb:cc:dd:ee:ff | cut -c-15
Run Code Online (Sandbox Code Playgroud)
输出是目标设备的 IP 地址。
示例 MAC 地址已更改为小写,因为 Linux 以这种方式显示它(与 Microsoft Windows 使用大写不同)
按照 bash 脚本向 hosts 文件 ( /etc/hosts) 中添加一个名称为mobile的条目,以便您可以使用名称为mobilephone访问您的设备。要刷新关联的 IP 地址,只需再次运行它。将name,mac和更改subnet为所需的值。
#!/bin/sh
name="cellphone"
mac=aa:bb:cc:dd:ee:ff
subnet=192.168.10.0/24
fping -g ${subnet}
ip="$(arp -n | grep -i ${mac} | cut -c-15)"
sed -i".bak" '/'${name}'/d' /etc/hosts
if [ -z "$ip" ]; then
echo "Device not found!"
else
echo "${ip}\t${name}" >> /etc/hosts
fi
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1907 次 |
| 最近记录: |