Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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/sql-server-2005/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
列出linux中网络中的所有活动IP地址_Linux_Grep_Ping - Fatal编程技术网

列出linux中网络中的所有活动IP地址

列出linux中网络中的所有活动IP地址,linux,grep,ping,Linux,Grep,Ping,我的任务是在linux机器上使用ping和grep工具的组合来生成网络中活动的ip地址列表,我只想显示那些活动的ip地址。这是我目前的代码: #!/bin/sh COUNTER=1 while [ $COUNTER -lt 254 ] do ping 10.1.0.$COUNTER -c 1 COUNTER=$(( $COUNTER + 1 )) done 我建议您使用nmap: nmap -sP 10.1.0.* 为您提供网络中的所有IP 更新 如果必须使用grep和table

我的任务是在linux机器上使用ping和grep工具的组合来生成网络中活动的ip地址列表,我只想显示那些活动的ip地址。这是我目前的代码:

#!/bin/sh

COUNTER=1

while [ $COUNTER -lt 254 ]
do
  ping 10.1.0.$COUNTER -c 1
  COUNTER=$(( $COUNTER + 1 ))
done

我建议您使用nmap:

nmap -sP 10.1.0.*
为您提供网络中的所有IP

更新 如果必须使用grep和table:

nano liveAddress.sh
用代码实现:

#!/bin/sh

COUNTER=1

while [ $COUNTER -lt 254 ]
do
  ping -c 1 10.1.0.$COUNTER | grep PING | awk '{print $2}'
  COUNTER=$(( $COUNTER + 1 ))
done
保存(ctrl+o)并退出(ctrl+x)


这将为您提供网络上机器的唯一IP。

首先,为ping添加1秒的限制

#!/bin/sh

COUNTER=1

while [ $COUNTER -lt 254 ]
do
  ping -w1 10.1.0.$COUNTER -c 1
  COUNTER=$(( $COUNTER + 1 ))
done
然后将脚本命名为s.sh

. s.sh  | grep '1 received' -B1 | awk {'print $2'}

最后,删除此脚本并使用nmap;)

为什么需要使用
grep
? 只需对代码稍作更改,即可找到所有活动IP:

#!/bin/bash

COUNTER=1

while [ $COUNTER -lt 254 ]
do
  ping -w1 10.1.0.$COUNTER -c 1
  if [ $? -eq 0 ];then
      echo "10.1.0.$COUNTER"
  fi
  COUNTER=$(( $COUNTER + 1 ))
done

对于不想安装nmap的用户,请使用简单的bash脚本。您可以将任何C类网络设置为参数

#!/bin/bash
#set -x
net=$1
typeset -i i

for (( i = 1 ;  i<255 ; i++ )) ;
    do
            ip=$net.$i
            ping -c 1 -W1 $ip >/dev/null
            if [ $? == '0' ]
                    then
                    echo $ip exists
            fi
    done
开始

输出

192.168.200.1 exists
192.168.200.3 exists

仅使用默认工具即可快速响应:

#!/bin/bash 

IP_PREFIX=$1

if [[ -z $IP_PREFIX ]]; then
        echo "Usage: $(basename $0) 192.168.1"
        exit
fi

export COUNTER=1
while [ $COUNTER -lt 255 ]
do
    TARG="${IP_PREFIX}.${COUNTER}"
    #echo "Pinging $TARG"
    ping $TARG -c 1 -w 4 | grep -B 1 "\b0% packet loss" > /dev/null && \
        echo $TARG is alive. &
    COUNTER=$(( $COUNTER + 1 ))
done

wait

但是OP说他必须使用ping和grep。这是毫无疑问的!所以我“建议”使用nmap!(:听起来你想在这里做一个学校项目--不帮助人们做他们的学校作业:(你错过了检查ping是否成功并存储/输出列表测试
ping的退出状态,这样你就可以只显示活动的。)。
./broadcast.sh 192.168.200
192.168.200.1 exists
192.168.200.3 exists
#!/bin/bash 

IP_PREFIX=$1

if [[ -z $IP_PREFIX ]]; then
        echo "Usage: $(basename $0) 192.168.1"
        exit
fi

export COUNTER=1
while [ $COUNTER -lt 255 ]
do
    TARG="${IP_PREFIX}.${COUNTER}"
    #echo "Pinging $TARG"
    ping $TARG -c 1 -w 4 | grep -B 1 "\b0% packet loss" > /dev/null && \
        echo $TARG is alive. &
    COUNTER=$(( $COUNTER + 1 ))
done

wait