Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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
Linux 替换bash脚本中变量的可选参数_Linux_Bash_Variables_Host - Fatal编程技术网

Linux 替换bash脚本中变量的可选参数

Linux 替换bash脚本中变量的可选参数,linux,bash,variables,host,Linux,Bash,Variables,Host,如何将可选参数传递给将替换脚本中现有变量的bash脚本?例如: #!/bin/bash #hostfinder.sh #Find hosts in current /24 network subnet=$(hostname -i | cut -d. -f1,2,3) for j in \ $(for i in $subnet.{1..255}; do host $i; done | grep -v not | cut -d" " -f5) do ping -c1 -w 1 $j; d

如何将可选参数传递给将替换脚本中现有变量的bash脚本?例如:

#!/bin/bash
#hostfinder.sh
#Find hosts in current /24 network

subnet=$(hostname -i | cut -d. -f1,2,3)

for j in \
    $(for i in $subnet.{1..255}; do host $i; done | grep -v not | cut -d" " -f5)
do ping -c1 -w 1 $j; done | grep -i from | cut -d" " -f3,4,5 | tr ':' ' ' | \
sed -e 's/from/Alive:/'
./hostfinder.sh 10.20.0
这将获取当前主机的IP,对可能的邻居运行反向查找,ping测试找到的任何主机名,并显示类似以下内容的输出:

Alive: host1.domain (10.64.17.23)
Alive: host2.domain (10.64.17.24)
...
说我疯了,但它比nmap快得多,并且给出了一个很好的列表

无论如何,在执行脚本时,我希望有选择地将任何C类网络地址的前三个八位字节作为$1参数传递给$subnet变量。例如:

#!/bin/bash
#hostfinder.sh
#Find hosts in current /24 network

subnet=$(hostname -i | cut -d. -f1,2,3)

for j in \
    $(for i in $subnet.{1..255}; do host $i; done | grep -v not | cut -d" " -f5)
do ping -c1 -w 1 $j; done | grep -i from | cut -d" " -f3,4,5 | tr ':' ' ' | \
sed -e 's/from/Alive:/'
./hostfinder.sh 10.20.0

我的第一个想法是尝试像$subnet=$1这样的东西,但我认为这不会起作用。我对重新编写脚本以使其更加优雅或其他什么都不感兴趣,我只是对我在主题行中添加的内容感到好奇。

尝试使用getopt读取命令行中的选项,然后设置变量。

尝试使用getopt读取命令行中的选项,然后设置变量。

替换如何:

subnet=$(hostname -i | cut -d. -f1,2,3)
与:

  • $#
    是命令行参数的数量
  • 这不如
    getopt
    优雅,但易于理解和扩展
更换怎么样:

subnet=$(hostname -i | cut -d. -f1,2,3)
与:

  • $#
    是命令行参数的数量
  • 这不如
    getopt
    优雅,但易于理解和扩展

像LtWorf建议的那样,尝试使用
getopt
。它从命令行读取选项及其参数

您可以在这里找到
getopt
用法的一个很好的例子:

像LtWorf建议的那样,尝试使用
getopt
。它从命令行读取选项及其参数

您可以在这里找到
getopt
用法的一个很好的例子:

nmap意味着做一件完全不同的事情。不过,nmap可以做很多事情。:)另外,我认为使用nmap进行ping扫描的方式是这样的:nmap-sP 192.168.1.1-254Did
subnet=$1
不适合您?nmap意味着做一件完全不同的事情。尽管nmap可以做很多事情。:)另外,我认为使用nmap进行ping扫描的方式如下:nmap-sP 192.168.1.1-254Did
subnet=$1
不适合您?谢谢您的建议。请你举个例子好吗?谢谢你的建议。请你举个例子好吗?谢谢。我会玩这个,然后回应。这就像冠军一样有效。谢谢!我现在可以为A类或B类扩展更多选项。这将成为一个很好的便携式清扫器,我没有安装nmap,或者nmap-sP在某些子网上似乎无法工作,我需要清点主机。谢谢。我会玩这个,然后回应。这就像冠军一样有效。谢谢!我现在可以为A类或B类扩展更多选项。这将成为一个很好的便携式清扫器,在这里我没有安装nmap,或者当nmap-sP似乎无法在某些子网上运行时,我需要清点主机。