如何找到IP地址在另一个文件列表 [英] How to find a list of ip addresses in another file
问题描述
我得到任务,看看我们是否是广告的IP地址(3000)的列表。这不是一个好主意,做手工,所以我复制了所有我们在一个文件中的广告的IP地址。现在我只需要创建一个bash脚本和饲料IP地址列表中插入脚本,以便它可以找到IP地址列表中的文件中的IP地址被广告。如果在不同的文件中找到它保存在一个文件中如果没有。这是我到目前为止所。这个脚本的问题是,我必须手动键入每个IP地址。我怎么能养活将该文件与IP地址的列表要查找在文件和IP地址的列表中,我们都是广告。非常感谢你在先进。
#!/斌/庆典而真实的;做
回声-eIP地址:\\ C
读取IP 如果grep的--color$ IP,IPS然后
回声$ IP被发现
回声$ IP>>发现
其他
回声$ IP未找到
回声$ IP>>未找到 科幻DONE
一个更好的剧本:
同时读取IP
做
grep的$ IP,$ IPS>的/ dev / null的2 - ;&放大器; 1安培;&安培;回声$ IP>> ip.found ||回声$ IP>> ip.notfound
DONE
命名剧本searchip.sh结果
假设你的输入文件是IPLIST,建立变量,并调用是这样的:
IPS = IPS
猫IPLIST | SH searchip.sh
或
SH searchip.sh< IPLIST
然后你得到两个文件,一个是IP发现,另一种是IP找不到。结果
你需要的是外壳I / O重定向。
I was given the task to see if we are advertising a list of ip addresses(3000). Not a good idea to do it manually, so I copied all the ip addresses that we are advertising in a file. Now I just have to create a bash script and feed the list of ip address into the script so it can find the ip addresses in the file with the list of ip addresses being advertised. If found save it in one file if not in a different file. This is what I have so far. The problem with this script is that I have to type in manually every ip address. HOW CAN I FEED THE FILE WITH THE LIST OF IP ADDRESSES TO BE SEARCHED IN THE FILE WITH THE LIST OF IP ADDRESSES WE ARE ADVERTISING. Thank you very much in advanced.
#!/bin/bash
while true; do
echo -e "IP address: \c"
read ip
if grep --color "$ip" "ips"; then
echo $ip "was found"
echo $ip >> found
else
echo $ip "was NOT found"
echo $ip >> notFound
fi
done
A better script :
while read ip
do
grep "$ip" "$ips" > /dev/null 2>&1 && echo "$ip" >> ip.found || echo "$ip" >> ip.notfound
done
Name the script "searchip.sh"
Assume your input file is "iplist" ,set up variable and call like this:
ips=ips
cat iplist | sh searchip.sh
or
sh searchip.sh < iplist
Then you get two files , one is ip found, other one is ip not found.
What you need is shell I/O redirection.
这篇关于如何找到IP地址在另一个文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!