是否可以从Bash执行程序SSH到所有网络客户端?

问题描述 投票:0回答:1

我正在编写一个shell脚本,我希望它能够运行某些命令,例如arp -a,以获取网络上每个人的IP,然后依次尝试对其进行SSH交换。问题是,我不知道如何在不手动输入IP的情况下将IP从arp -a发送到SSH命令(由于我正在编写可执行文件,因此无法正常工作。)甚至可以吗?

bash ssh ip arp bash3
1个回答
0
投票

简短回答:您可以编写一个小脚本来从arp中提取IP并处理每个IP。您可以使用bash循环或其他工具(xargs)处理多个IP。

重击解决方案

#! /bin/bash
  # Read arp line like: '_gateway (192.168.215.3) at 00:52:58:e7:cf:5f [ether] on ens33`
while read tag ip x ; do
  # Strip leading and trailing characters from the IP
  ip=${ip:1:-1}
  # Execute ssh
  ssh ... "$ip"
done <<< "$(arp -a)"

更新bash 3.X:

看起来像bash 3.X,不支持'-1'作为子字符串的索引。使用以下内容代替ip=${ip:1:-1}来删除前导'('和尾随')'

 ip=${ip#(} ; ip=${ip%)}
© www.soinside.com 2019 - 2024. All rights reserved.