Skip to content

Commit

Permalink
用nc测试筛选ip端口
Browse files Browse the repository at this point in the history
  • Loading branch information
redrainl authored May 14, 2024
1 parent 2c0278a commit 60cb72a
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions speedtest/multi_test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
cd /root/iptv
#read -p "确定要运行脚本吗?(y/n): " choice

time=$(date +%m%d%H%M)
i=0
Expand All @@ -21,7 +23,7 @@ if [ $# -eq 0 ]; then
echo "14. 甘肃电信(Gansu_105)"
echo "15. 河北联通(Hebei_313)"
echo "0. 全部"
read -t 15 -p "输入选择或在10秒内无输入将默认选择全部: " city_choice
read -t 10 -p "输入选择或在10秒内无输入将默认选择全部: " city_choice

if [ -z "$city_choice" ]; then
echo "未检测到输入,自动选择全部选项..."
Expand All @@ -37,7 +39,7 @@ case $city_choice in
1)
city="Shanghai_103"
stream="udp/239.45.1.4:5140"
channel_key="上海电信"
channel_key="上海"
;;
2)
city="Beijing_liantong_145"
Expand Down Expand Up @@ -124,28 +126,46 @@ case $city_choice in
esac

# 使用城市名作为默认文件名,格式为 CityName.ip
filename="ip/${city}.ip"
ipfile="ip/${city}.ip"
onlyip="ip/${city}.onlyip"
onlyport="ip/${city}.port"

# 搜索最新ip

echo "===============从tonkiang检索最新ip================="
python3 hoteliptv.py $channel_key >test.html
grep -o "href='hotellist.html?s=[^']*'" test.html>temp.txt
# sed -n "s/.*href='hotellist.html?s=\([^']*\)'.*/\1/p" temp.txt > $filename
# sed -n "s/^.*href='hotellist.html?s=\([^:]*:[0-9]*\).*/\1/p" temp.txt | sort | uniq > $filename
rm -f test.html
/usr/bin/python3 hoteliptv.py $channel_key >test.html
grep -o "href='hotellist.html?s=[^']*'" test.html > tempip.txt

sed -n "s/^.*href='hotellist.html?s=\([^:]*\):[0-9].*/\1/p" tempip.txt > tmp_onlyip
sort tmp_onlyip | uniq | sed '/^\s*$/d' > $onlyip
rm -f test.html tempip.txt tmp_onlyip $ipfile

# 遍历ip和端口组合
while IFS= read -r ip; do
while IFS= read -r port; do
# 尝试连接 IP 地址和端口号
echo "nc -w 1 -z "$ip" "$port" 2>&1"
output=$(nc -w 1 -z "$ip" "$port" 2>&1)
# 如果连接成功,且输出包含 "succeeded",则将结果保存到输出文件中
if [[ $output == *"succeeded"* ]]; then
# 使用 awk 提取 IP 地址和端口号对应的字符串,并保存到输出文件中
echo "$output" | grep "succeeded" | awk -v ip="$ip" -v port="$port" '{print ip ":" port}' >> "$ipfile"
fi
done < "$onlyport"
done < "$onlyip"


rm -f $onlyip
echo "===============检索完成================="

# 检查文件是否存在
if [ ! -f "$filename" ]; then
echo "错误:文件 $filename 不存在。"
if [ ! -f "$ipfile" ]; then
echo "错误:文件 $ipfile 不存在。"
exit 1
fi

lines=$(cat "$filename" | wc -l)
echo "$filename文件】内ip共计$lines"
lines=$(cat "$ipfile" | wc -l)
echo "$ipfile文件】内ip共计$lines"

while read line; do
i=$(($i + 1))
Expand All @@ -154,21 +174,19 @@ while read line; do
if [ "$city" == "Jieyang_129" ]; then
echo $url
# 使用yt-dlp下载并解析下载速度
output=$(yt-dlp --ignore-config --no-cache-dir --output "output.ts" --download-archive new-archive.txt --external-downloader ffmpeg --external-downloader-args "-t 5" "$url" 2>&1)
output=$(/usr/local/bin/yt-dlp --ignore-config --no-cache-dir --output "output.ts" --download-archive new-archive.txt --external-downloader ffmpeg --external-downloader-args "-t 5" "$url" 2>&1)
a=$(echo "$output" | grep -oP 'at \K[0-9.]+M')
rm -f new-archive.txt output.ts

else
echo $url
curl $url --connect-timeout 3 --max-time 10 -o /dev/null >zubo.tmp 2>&1
a=$(head -n 3 zubo.tmp | awk '{print $NF}' | tail -n 1)
fi




echo "$i/$lines个:$ip $a"
echo "$ip $a" >> "speedtest_${city}_$time.log"
done < "$filename"
done < "$ipfile"

rm -f zubo.tmp
cat "speedtest_${city}_$time.log" | grep -E 'M|k' | awk '{print $2" "$1}' | sort -n -r >"result/result_${city}.txt"
Expand All @@ -177,16 +195,31 @@ ip1=$(head -n 1 result/result_${city}.txt | awk '{print $2}')
ip2=$(head -n 2 result/result_${city}.txt | tail -n 1 | awk '{print $2}')
ip3=$(head -n 3 result/result_${city}.txt | tail -n 1 | awk '{print $2}')
rm -f speedtest_${city}_$time.log
sed "s/ipipip/$ip1/g" template/template_${city}.txt >tmp1.txt
sed "s/ipipip/$ip2/g" template/template_${city}.txt >tmp2.txt
sed "s/ipipip/$ip3/g" template/template_${city}.txt >tmp3.txt

#----------------------用3个最快ip生成对应城市的txt文件---------------------------

if [ $city = "Shanghai_103" ]; then
program="template/template_${city}.txt"
else
program="template_min/template_${city}.txt"

fi

sed "s/ipipip/$ip1/g" $program >tmp1.txt
echo "=======================sed "s/ipipip/$ip1/g" $program >tmp1.txt"
sed "s/ipipip/$ip2/g" $program >tmp2.txt
sed "s/ipipip/$ip3/g" $program >tmp3.txt
cat tmp1.txt tmp2.txt tmp3.txt >txt/${city}.txt

rm -rf tmp1.txt tmp2.txt tmp3.txt


#--------------------合并所有城市的txt文件为: zubo.txt-----------------------------------------

echo "上海电信,#genre#" >zubo.txt
cat txt/Shanghai_103.txt >>zubo.txt
echo "揭西酒店凤凰,#genre#" >>zubo.txt
cat txt/Jieyang_129.txt >>zubo.txt
echo "北京电信,#genre#" >>zubo.txt
cat txt/Beijing_dianxin_186.txt >>zubo.txt
echo "北京联通,#genre#" >>zubo.txt
Expand All @@ -213,14 +246,11 @@ echo "甘肃电信,#genre#" >>zubo.txt
cat txt/Gansu_105.txt >>zubo.txt
echo "河北联通,#genre#" >>zubo.txt
cat txt/Hebei_313.txt >>zubo.txt
echo "揭西凤凰,#genre#" >>zubo.txt
cat txt/Jieyang_129.txt >>zubo.txt

scp root@你的服务器:/iptv/mylist.txt .
scp root@你的服务器:/speedtest/mylist.txt .
# sed -i '/^上海电信/,$d' mylist.txt
sed -i '/^上海电信/,/^上海IPV6/{/^上海IPV6/!d;}' mylist.txt
cat zubo.txt mylist.txt >temp.txt && mv -f temp.txt mylist.txt
scp mylist.txt root@你的服务器:/iptv/mylist.txt
scp mylist.txt root@你的服务器:/speedtest/mylist.txt

for a in result/*.txt; do echo "";echo "========================= $(basename "$a") ==================================="; cat $a; done

0 comments on commit 60cb72a

Please sign in to comment.