Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以从Bash exec通过SSH连接到所有网络客户端?_Bash_Ssh_Ip_Arp - Fatal编程技术网

是否可以从Bash exec通过SSH连接到所有网络客户端?

是否可以从Bash exec通过SSH连接到所有网络客户端?,bash,ssh,ip,arp,Bash,Ssh,Ip,Arp,我正在编写一个shell脚本,我希望能够运行一些命令,比如arp-a,获取网络上每个人的IP,然后依次尝试对每个人进行SSHing。问题是,我不知道如何将IP从arp-a发送到SSH命令,而不手动输入IP(这不起作用,因为我正在编写一个可执行文件)。这可能吗?简短回答:您可以编写一个小脚本,从arp中提取IP,并处理每个IP。您可以使用bashloop或其他工具(xargs)来处理多个IP Bash解决方案 #! /bin/bash # Read arp line like: '_gatew

我正在编写一个shell脚本,我希望能够运行一些命令,比如
arp-a
,获取网络上每个人的IP,然后依次尝试对每个人进行SSHing。问题是,我不知道如何将IP从
arp-a
发送到SSH命令,而不手动输入IP(这不起作用,因为我正在编写一个可执行文件)。这可能吗?

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

Bash解决方案

#! /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在到达
ip=${ip:1:-1}
时抛出一个错误,特别是关于
-1
。有什么想法吗?你能把错误贴出来吗?我使用的是bash4.0,没有问题。你能发布你使用的bash版本吗(
bash--version
)我使用的版本是3.2.57(MacOS)
${var:start:-len}
是与4.2版一起添加的。
 ip=${ip#(} ; ip=${ip%)}