Linux 如何ping一个文件中的多个IP,并使用shell脚本仅打印另一个文件中的联机IP?

Linux 如何ping一个文件中的多个IP,并使用shell脚本仅打印另一个文件中的联机IP?,linux,bash,shell,ping,Linux,Bash,Shell,Ping,我有一个包含很多IP的文件,我想循环它们,ping它们,然后打印到另一个在线文件。我知道循环文件,ping它们,但我不知道如何读取输出以了解ip是否在线。只需检查ping命令的退出状态。 如果ping。。。;然后在线回音;fi 请注意,您必须使用-c和-w标志限制ping时间。只需检查ping命令的退出状态即可。 如果ping。。。;然后在线回音;fi 请注意,您必须使用-c和-w标志限制ping时间。您可以使用nmap: cat queryips.txt 192.168.1.1 192.168

我有一个包含很多IP的文件,我想循环它们,ping它们,然后打印到另一个在线文件。我知道循环文件,ping它们,但我不知道如何读取输出以了解ip是否在线。

只需检查ping命令的退出状态。
如果ping。。。;然后在线回音;fi


请注意,您必须使用
-c
-w
标志限制ping时间。

只需检查ping命令的退出状态即可。
如果ping。。。;然后在线回音;fi


请注意,您必须使用
-c
-w
标志限制ping时间。

您可以使用
nmap

cat queryips.txt

192.168.1.1
192.168.20.7
10.2.4.6
google.com
# Nmap 6.47 scan initiated Thu Dec 31 12:12:27 2015 as: nmap -iL queryips.txt -sn -n -oG upips.txt
Host: 192.168.1.1 ()    Status: Up
Host: 192.168.20.7 ()   Status: Up
Host: 173.194.116.72 () Status: Up
# Nmap done at Thu Dec 31 12:12:28 2015 -- 4 IP addresses (3 hosts up) scanned in 1.26 seconds
nmap-iL queryips.txt-sn-n-oG upips.txt

192.168.1.1
192.168.20.7
10.2.4.6
google.com
# Nmap 6.47 scan initiated Thu Dec 31 12:12:27 2015 as: nmap -iL queryips.txt -sn -n -oG upips.txt
Host: 192.168.1.1 ()    Status: Up
Host: 192.168.20.7 ()   Status: Up
Host: 173.194.116.72 () Status: Up
# Nmap done at Thu Dec 31 12:12:28 2015 -- 4 IP addresses (3 hosts up) scanned in 1.26 seconds
  • -iL queryips.txt
    queryips.txt
    文件加载主机列表
  • -sn
    执行ping扫描
  • -n
    不执行反向DNS查找
  • -oG upips.txt
    将可归零输出生成
    upips.txt
    文件
cat upips.txt

192.168.1.1
192.168.20.7
10.2.4.6
google.com
# Nmap 6.47 scan initiated Thu Dec 31 12:12:27 2015 as: nmap -iL queryips.txt -sn -n -oG upips.txt
Host: 192.168.1.1 ()    Status: Up
Host: 192.168.20.7 ()   Status: Up
Host: 173.194.116.72 () Status: Up
# Nmap done at Thu Dec 31 12:12:28 2015 -- 4 IP addresses (3 hosts up) scanned in 1.26 seconds
或打印符合标准的主机:

nmap -iL queryips.txt -sn -n -oG - | awk -F" " '!/#/ {print $2}'

您可以使用
nmap

cat queryips.txt

192.168.1.1
192.168.20.7
10.2.4.6
google.com
# Nmap 6.47 scan initiated Thu Dec 31 12:12:27 2015 as: nmap -iL queryips.txt -sn -n -oG upips.txt
Host: 192.168.1.1 ()    Status: Up
Host: 192.168.20.7 ()   Status: Up
Host: 173.194.116.72 () Status: Up
# Nmap done at Thu Dec 31 12:12:28 2015 -- 4 IP addresses (3 hosts up) scanned in 1.26 seconds
nmap-iL queryips.txt-sn-n-oG upips.txt

192.168.1.1
192.168.20.7
10.2.4.6
google.com
# Nmap 6.47 scan initiated Thu Dec 31 12:12:27 2015 as: nmap -iL queryips.txt -sn -n -oG upips.txt
Host: 192.168.1.1 ()    Status: Up
Host: 192.168.20.7 ()   Status: Up
Host: 173.194.116.72 () Status: Up
# Nmap done at Thu Dec 31 12:12:28 2015 -- 4 IP addresses (3 hosts up) scanned in 1.26 seconds
  • -iL queryips.txt
    queryips.txt
    文件加载主机列表
  • -sn
    执行ping扫描
  • -n
    不执行反向DNS查找
  • -oG upips.txt
    将可归零输出生成
    upips.txt
    文件
cat upips.txt

192.168.1.1
192.168.20.7
10.2.4.6
google.com
# Nmap 6.47 scan initiated Thu Dec 31 12:12:27 2015 as: nmap -iL queryips.txt -sn -n -oG upips.txt
Host: 192.168.1.1 ()    Status: Up
Host: 192.168.20.7 ()   Status: Up
Host: 173.194.116.72 () Status: Up
# Nmap done at Thu Dec 31 12:12:28 2015 -- 4 IP addresses (3 hosts up) scanned in 1.26 seconds
或打印符合标准的主机:

nmap -iL queryips.txt -sn -n -oG - | awk -F" " '!/#/ {print $2}'
使用此脚本

1. File full of IP's
[root@localhost scripts]# cat iplist.txt
172.31.57.63
localhost
127.0.0.1
172.31.57.62

2. Create the script

 [root@localhost scripts]# cat pingips.sh
 #!/bin/bash

 up_ipfile='online_server.txt'

 while IFS= read -r ips; do
        ping -c 1 $ips > /dev/null 2>&1
        if [ $? -eq 0 ]; then
              echo $ips >> $up_ipfile
        fi
 done < iplist.txt

 3. Run the script after making it executable 
 [root@localhost scripts]# ./pingips.sh


 4. It will create a file with IP's which are alive
 [root@localhost scripts]# cat online_server.txt
 172.31.57.63
 localhost
 127.0.0.1
1。完整的IP文件
[root@localhost脚本]#cat iplist.txt
172.31.57.63
本地服务器
127.0.0.1
172.31.57.62
2.创建脚本
[root@localhost脚本]#cat pingips.sh
#!/bin/bash
up\u ipfile='online\u server.txt'
而IFS=read-r ips;做
ping-c 1$ips>/dev/null 2>&1
如果[$?-等式0];然后
回显$ips>>$up\IPU文件
fi
完成
使用此脚本

1. File full of IP's
[root@localhost scripts]# cat iplist.txt
172.31.57.63
localhost
127.0.0.1
172.31.57.62

2. Create the script

 [root@localhost scripts]# cat pingips.sh
 #!/bin/bash

 up_ipfile='online_server.txt'

 while IFS= read -r ips; do
        ping -c 1 $ips > /dev/null 2>&1
        if [ $? -eq 0 ]; then
              echo $ips >> $up_ipfile
        fi
 done < iplist.txt

 3. Run the script after making it executable 
 [root@localhost scripts]# ./pingips.sh


 4. It will create a file with IP's which are alive
 [root@localhost scripts]# cat online_server.txt
 172.31.57.63
 localhost
 127.0.0.1
1。完整的IP文件
[root@localhost脚本]#cat iplist.txt
172.31.57.63
本地服务器
127.0.0.1
172.31.57.62
2.创建脚本
[root@localhost脚本]#cat pingips.sh
#!/bin/bash
up\u ipfile='online\u server.txt'
而IFS=read-r ips;做
ping-c 1$ips>/dev/null 2>&1
如果[$?-等式0];然后
回显$ips>>$up\IPU文件
fi
完成
我执行这个脚本,但它不会在终端或在线服务器文件中给我任何输出。有错误吗?没有,没有错误。它不会给出任何输出,因为我正在/dev/null中发送ping的输出和错误。但是如果你的IP是活动的(可ping),那么它肯定会在online_server.txt中。你能确保你正在测试的IP是活动的吗?哦,我不知道输出被发送到了/dev/null,我是这个sintax的新手。在文件iplist.txt中,还有许多IP处于脱机状态@manishrOk,你的回答真的帮助了我,谢谢:我执行这个脚本,但它不会在终端或在线服务器文件中给我任何输出。有错误吗?没有,没有错误。它不会给出任何输出,因为我正在/dev/null中发送ping的输出和错误。但是如果你的IP是活动的(可ping),那么它肯定会在online_server.txt中。你能确保你正在测试的IP是活动的吗?哦,我不知道输出被发送到了/dev/null,我是这个sintax的新手。在文件iplist.txt中,还有许多IP处于脱机状态@manishrOk,你的回答真的帮助了我,谢谢:D