Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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-for循环_Bash - Fatal编程技术网

带变量范围的Bash-for循环

带变量范围的Bash-for循环,bash,Bash,正在尝试将ip地址范围打印到文件。它只打印包含变量值的一行,而不是在范围内循环 如果给定192.168.0.1/24查找如何获得输出以在文件中的每一行上打印该网络中的每个主机 192.168.0.0 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4 ... 192.168.0.255 使用seq和xargs看起来既酷又有趣: seq 192 192 | xargs -i seq -f "{}.%.0f" 168 168 | xargs -i se

正在尝试将ip地址范围打印到文件。它只打印包含变量值的一行,而不是在范围内循环

如果给定
192.168.0.1/24
查找如何获得输出以在文件中的每一行上打印该网络中的每个主机

192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
...
192.168.0.255

使用
seq
xargs
看起来既酷又有趣:

seq 192 192 |
xargs -i seq -f "{}.%.0f" 168 168 |
xargs -i seq -f "{}.%.0f" 0 0 |
xargs -i seq -f "{}.%.0f" 0 255
因此,在您的脚本中:

seq $a $e |
xargs -i seq -f "{}.%.0f" $b $f |
xargs -i seq -f "{}.%.0f" $c $g |
xargs -i seq -f "{}.%.0f" $d $h |
while read ip; do
   ...
done
或者您可以
以美元表示ip(seq$a$e |……);这样做
,但那就不那么酷了


好的是,它很容易成为一个函数。

这很有效……它使用
eval
而不是循环将所有ip地址写入一个文件,然后我按空格分割,并用换行符将空格替换为另一个文件

cidr=$1

# range is bounded by network (-n) & broadcast (-b) addresses.
lo=$(ipcalc -n $cidr |cut -f2 -d=)
hi=$(ipcalc -b $cidr |cut -f2 -d=)

read a b c d <<< $(echo $lo |tr . ' ')
echo $a.$b.$c.$d
read e f g h <<< $(echo $hi |tr . ' ')
echo $e.$f.$g.$h

eval "echo {$a..$e}.{$b..$f}.{$c..$g}.{$d..$h}" > ip_range.txt

tr ' ' '\n' < ip_range.txt > results.txt
cidr=$1
#范围由网络(-n)和广播(-b)地址限定。
lo=$(ipcalc-n$cidr | cut-f2-d=)
hi=$(ipcalc-b$cidr | cut-f2-d=)

阅读b c d请将该示例输入的所需输出添加到您的问题中。
{$a..$e}.{$b..$f}.{$c..$g}.{$d..$h}
将不起作用,因为大括号扩展不接受变量作为参数。如果没有
eval
请参阅右侧,但是,如果内容不在您的控制之下,请小心
eval
风险。
tr'\n'
?为什么不第一次打印新行呢<代码>eval“printf“%s\n”{$a…“
seq $a $e |
xargs -i seq -f "{}.%.0f" $b $f |
xargs -i seq -f "{}.%.0f" $c $g |
xargs -i seq -f "{}.%.0f" $d $h |
while read ip; do
   ...
done
cidr=$1

# range is bounded by network (-n) & broadcast (-b) addresses.
lo=$(ipcalc -n $cidr |cut -f2 -d=)
hi=$(ipcalc -b $cidr |cut -f2 -d=)

read a b c d <<< $(echo $lo |tr . ' ')
echo $a.$b.$c.$d
read e f g h <<< $(echo $hi |tr . ' ')
echo $e.$f.$g.$h

eval "echo {$a..$e}.{$b..$f}.{$c..$g}.{$d..$h}" > ip_range.txt

tr ' ' '\n' < ip_range.txt > results.txt
cidr=$1

# range is bounded by network (-n) & broadcast (-b) addresses.
lo=$(ipcalc -n $cidr |cut -f2 -d=)
hi=$(ipcalc -b $cidr |cut -f2 -d=)

read a b c d <<< $(echo $lo |tr . ' ')
echo $a.$b.$c.$d
read e f g h <<< $(echo $hi |tr . ' ')
echo $e.$f.$g.$h

eval "printf '%s\n' {$a..$e}.{$b..$f}.{$c..$g}.{$d..$h}" > ip_range.txt