最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

SHELL脚本之基础网络检查

IT圈 admin 1浏览 0评论

SHELL脚本之基础网络检查

一、需求说明

  作为一名运维工程师,我们经常需要进行系统之间的联调或者系统服务不可用时的故障排查。当出现系统服务无法访问的时候我们需要定位故障原因,可能是网络中断、可能是DNS地址未配置或者配置错误、也可能是服务不可用。网络基础检查shell脚本用于基础网络检查和服务连通性检查。其中基础网络检查主要通过ping验证到网关、公网地址、域名等的通断性;服务连通性检查主要通过telnet服务验证系统服务端口是否存活。

二、脚本内容

#!/bin/bash
#script name: network_check.sh
#author: 524627027@qq.com
#version: v1
#description: 此脚本用于检测Linux系统网络状态#参数定义
localip="127.0.0.1"
gatewayip=""
dnsip=""function myping(){ping $1 -c3if [ $? -eq 0 ];thenreturn 1;elsereturn 0fi
}function mytelnet(){s=`which telnet |grep "no telnet in"`if [ -z "$s" ];then`telnet $1 $2` > tt.txts=`cat tt.txt |grep "Escape character is '^]'"`if [ -n "$s" ];thenecho -e "\e[32m 这个主机到 $1 主机的 $2 服务网络可达\e[0m"elseecho -e "\e[31m 这个主机到 $1 主机的 $2 服务网络不可达\e[0m"fielseecho "需要安装telnet客户端服务"yum install -y telnet
#       mytelnet()fi
}function servicecheck(){ssh $1 -p $2 -v &> tc.txts=`cat tc.txt |grep "Connection refused"`if [ -z "$s" ];thenecho -e "\e[32m 这个主机到 $1 主机的 $2 服务网络可达\e[0m"elseecho -e "\e[31m 这个主机到 $1 主机的 $2 服务网络不可达\e[0m"fi
}#获取基本信息
function baseinf_get(){
#获取本机IP地址
localip=`ifconfig |grep netmask |grep -v 127.0.0.1 |awk '{ print $2 }'`#获取网关IP地址
gatewayip=`route -n |grep "0.0.0.0" |awk '{ print $2 }' |grep -v "0.0.0.0"`#获取DNS地址
dnsip=`cat /etc/resolv.conf |grep nameserver |awk '{ print $2 }'`echo -e "本服务器的网卡IP地址为:\n\e[32m $localip \e[0m"
echo -e "本服务器的网关IP地址为:\n\e[32m $gatewayip \e[0m"
echo -e "本服务器的DNS IP地址为:\n\e[32m $dnsip \e[0m"
}#检测主机到自己网卡的网络连通性
function netcardcheck(){
myping $localip
if [ $? -eq 1 ];
thenecho -e "\e[32m 这个主机到网卡 $localip 网络可达\e[0m";
elseecho -e "\e[31m 这个主机到网卡 $localip 网络不可达\e[0m"
fi
}#检测主机到网关的网络连通性
function gatewaycheck(){
myping $gatewayip
if [ $? -eq 1 ];
thenecho -e "\e[32m 这个主机 $localip 到网关 $gatewayip 网络可达\e[0m";
elseecho -e "\e[31m 这个主机 $localip 到网关 $gatewayip 网络不可达\e[0m"
fi
}#检测主机到互联网公网IP的连通性
function internetipcheck(){
internetip=114.114.114.114
myping $internetip
if [ $? -eq 1 ];
thenecho -e "\e[32m 这个主机 $localip 到公网IP地址 $internetip 网络可达\e[0m";
elseecho -e "\e[31m 这个主机 $localip 到公网IP地址 $internetip 网络不可达\e[0m"
fi
}#检测主机到互联网域名的连通性
function domaincheck(){
domainname=www.baidu.com
myping $domainname
if [ $? -eq 1 ];
thenecho -e "\e[32m 这个主机 $localip 到 $domainname 域名网络可达\e[0m";
elseecho -e "\e[31m 这个主机 $localip 到 $domainname 域名网络不可达\e[0m"
fi
}#检测主机到指定网络的连通性,通过参数输入
function myservicecheck(){
read -p "请输入需要检测的主机地址(格式样例:192.168.0.100):" a
read -p "请输入需要检测的服务端口(格式样例:80),如果无请直接输入回车:" b
if [ -z "$b" ];
thenmyping aif [$? -eq 1 ];thenecho -e "\e[32m 这个主机 $localip 到公网IP地址 $a 网络可达\e[0m";elseecho -e "\e[31m 这个主机 $localip 到公网IP地址 $a 网络不可达\e[0m"fi
elseservicecheck $a $b
fi
}function main(){echo -e "1、基础网络检查\n2、指定服务网络连通性检查\n3、退出"read -p "你的选择为:" optcase $opt in1)baseinf_getnetcardcheckgatewaycheckinternetipcheckdomaincheck;;2)myservicecheck;;3)exit;;*)echo "请根据选择输入1,2,3,其他输入无效!";;esac
}main

三、使用示例

1、检查网络检查

[root@test1 ~]# sh network_check.sh
1、基础网络检查
2、指定服务网络连通性检查
3、退出
你的选择为:1
本服务器的网卡IP地址为:
192.168.0.124
本服务器的网关IP地址为:
192.168.0.1
本服务器的DNS IP地址为:
211.142.211.124
PING 192.168.0.124 (192.168.0.124) 56(84) bytes of data.
64 bytes from 192.168.0.124: icmp_seq=1 ttl=64 time=0.131 ms
64 bytes from 192.168.0.124: icmp_seq=2 ttl=64 time=0.063 ms
64 bytes from 192.168.0.124: icmp_seq=3 ttl=64 time=0.062 ms

— 192.168.0.124 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.062/0.085/0.131/0.033 ms
这个主机到网卡 192.168.0.124 网络可达
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=255 time=1.96 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=255 time=1.87 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=255 time=1.70 ms

— 192.168.0.1 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 1.707/1.846/1.961/0.110 ms
这个主机 192.168.0.124 到网关 192.168.0.1 网络可达
PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
64 bytes from 114.114.114.114: icmp_seq=1 ttl=88 time=30.0 ms
64 bytes from 114.114.114.114: icmp_seq=2 ttl=69 time=29.5 ms
64 bytes from 114.114.114.114: icmp_seq=3 ttl=66 time=29.5 ms

— 114.114.114.114 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 29.510/29.715/30.074/0.254 ms
这个主机 192.168.0.124 到公网IP地址 114.114.114.114 网络可达
PING www.a.shifen.com (183.232.231.174) 56(84) bytes of data.
64 bytes from localhost (183.232.231.174): icmp_seq=1 ttl=55 time=17.2 ms
64 bytes from localhost (183.232.231.174): icmp_seq=2 ttl=55 time=17.0 ms
64 bytes from localhost (183.232.231.174): icmp_seq=3 ttl=55 time=16.9 ms

— www.a.shifen.com ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2007ms
rtt min/avg/max/mdev = 16.975/17.092/17.258/0.193 ms
这个主机 192.168.0.124 到 www.baidu.com 域名网络可达

2、服务检查

[root@test1 ~]# sh network_check.sh
1、基础网络检查
2、指定服务网络连通性检查
3、退出
你的选择为:2
请输入需要检测的主机地址(格式样例:192.168.0.100):192.168.0.132
请输入需要检测的服务端口(格式样例:80),如果无请直接输入回车:8085
这个主机到 192.168.0.132 主机的 8085 服务网络可达
[root@test1 ~]# sh network_check.sh
1、基础网络检查
2、指定服务网络连通性检查
3、退出
你的选择为:2
请输入需要检测的主机地址(格式样例:192.168.0.100):192.168.0.132
请输入需要检测的服务端口(格式样例:80),如果无请直接输入回车:8086
这个主机到 192.168.0.132 主机的 8086 服务网络不可达

SHELL脚本之基础网络检查

一、需求说明

  作为一名运维工程师,我们经常需要进行系统之间的联调或者系统服务不可用时的故障排查。当出现系统服务无法访问的时候我们需要定位故障原因,可能是网络中断、可能是DNS地址未配置或者配置错误、也可能是服务不可用。网络基础检查shell脚本用于基础网络检查和服务连通性检查。其中基础网络检查主要通过ping验证到网关、公网地址、域名等的通断性;服务连通性检查主要通过telnet服务验证系统服务端口是否存活。

二、脚本内容

#!/bin/bash
#script name: network_check.sh
#author: 524627027@qq.com
#version: v1
#description: 此脚本用于检测Linux系统网络状态#参数定义
localip="127.0.0.1"
gatewayip=""
dnsip=""function myping(){ping $1 -c3if [ $? -eq 0 ];thenreturn 1;elsereturn 0fi
}function mytelnet(){s=`which telnet |grep "no telnet in"`if [ -z "$s" ];then`telnet $1 $2` > tt.txts=`cat tt.txt |grep "Escape character is '^]'"`if [ -n "$s" ];thenecho -e "\e[32m 这个主机到 $1 主机的 $2 服务网络可达\e[0m"elseecho -e "\e[31m 这个主机到 $1 主机的 $2 服务网络不可达\e[0m"fielseecho "需要安装telnet客户端服务"yum install -y telnet
#       mytelnet()fi
}function servicecheck(){ssh $1 -p $2 -v &> tc.txts=`cat tc.txt |grep "Connection refused"`if [ -z "$s" ];thenecho -e "\e[32m 这个主机到 $1 主机的 $2 服务网络可达\e[0m"elseecho -e "\e[31m 这个主机到 $1 主机的 $2 服务网络不可达\e[0m"fi
}#获取基本信息
function baseinf_get(){
#获取本机IP地址
localip=`ifconfig |grep netmask |grep -v 127.0.0.1 |awk '{ print $2 }'`#获取网关IP地址
gatewayip=`route -n |grep "0.0.0.0" |awk '{ print $2 }' |grep -v "0.0.0.0"`#获取DNS地址
dnsip=`cat /etc/resolv.conf |grep nameserver |awk '{ print $2 }'`echo -e "本服务器的网卡IP地址为:\n\e[32m $localip \e[0m"
echo -e "本服务器的网关IP地址为:\n\e[32m $gatewayip \e[0m"
echo -e "本服务器的DNS IP地址为:\n\e[32m $dnsip \e[0m"
}#检测主机到自己网卡的网络连通性
function netcardcheck(){
myping $localip
if [ $? -eq 1 ];
thenecho -e "\e[32m 这个主机到网卡 $localip 网络可达\e[0m";
elseecho -e "\e[31m 这个主机到网卡 $localip 网络不可达\e[0m"
fi
}#检测主机到网关的网络连通性
function gatewaycheck(){
myping $gatewayip
if [ $? -eq 1 ];
thenecho -e "\e[32m 这个主机 $localip 到网关 $gatewayip 网络可达\e[0m";
elseecho -e "\e[31m 这个主机 $localip 到网关 $gatewayip 网络不可达\e[0m"
fi
}#检测主机到互联网公网IP的连通性
function internetipcheck(){
internetip=114.114.114.114
myping $internetip
if [ $? -eq 1 ];
thenecho -e "\e[32m 这个主机 $localip 到公网IP地址 $internetip 网络可达\e[0m";
elseecho -e "\e[31m 这个主机 $localip 到公网IP地址 $internetip 网络不可达\e[0m"
fi
}#检测主机到互联网域名的连通性
function domaincheck(){
domainname=www.baidu.com
myping $domainname
if [ $? -eq 1 ];
thenecho -e "\e[32m 这个主机 $localip 到 $domainname 域名网络可达\e[0m";
elseecho -e "\e[31m 这个主机 $localip 到 $domainname 域名网络不可达\e[0m"
fi
}#检测主机到指定网络的连通性,通过参数输入
function myservicecheck(){
read -p "请输入需要检测的主机地址(格式样例:192.168.0.100):" a
read -p "请输入需要检测的服务端口(格式样例:80),如果无请直接输入回车:" b
if [ -z "$b" ];
thenmyping aif [$? -eq 1 ];thenecho -e "\e[32m 这个主机 $localip 到公网IP地址 $a 网络可达\e[0m";elseecho -e "\e[31m 这个主机 $localip 到公网IP地址 $a 网络不可达\e[0m"fi
elseservicecheck $a $b
fi
}function main(){echo -e "1、基础网络检查\n2、指定服务网络连通性检查\n3、退出"read -p "你的选择为:" optcase $opt in1)baseinf_getnetcardcheckgatewaycheckinternetipcheckdomaincheck;;2)myservicecheck;;3)exit;;*)echo "请根据选择输入1,2,3,其他输入无效!";;esac
}main

三、使用示例

1、检查网络检查

[root@test1 ~]# sh network_check.sh
1、基础网络检查
2、指定服务网络连通性检查
3、退出
你的选择为:1
本服务器的网卡IP地址为:
192.168.0.124
本服务器的网关IP地址为:
192.168.0.1
本服务器的DNS IP地址为:
211.142.211.124
PING 192.168.0.124 (192.168.0.124) 56(84) bytes of data.
64 bytes from 192.168.0.124: icmp_seq=1 ttl=64 time=0.131 ms
64 bytes from 192.168.0.124: icmp_seq=2 ttl=64 time=0.063 ms
64 bytes from 192.168.0.124: icmp_seq=3 ttl=64 time=0.062 ms

— 192.168.0.124 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.062/0.085/0.131/0.033 ms
这个主机到网卡 192.168.0.124 网络可达
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=255 time=1.96 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=255 time=1.87 ms
64 bytes from 192.168.0.1: icmp_seq=3 ttl=255 time=1.70 ms

— 192.168.0.1 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 1.707/1.846/1.961/0.110 ms
这个主机 192.168.0.124 到网关 192.168.0.1 网络可达
PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
64 bytes from 114.114.114.114: icmp_seq=1 ttl=88 time=30.0 ms
64 bytes from 114.114.114.114: icmp_seq=2 ttl=69 time=29.5 ms
64 bytes from 114.114.114.114: icmp_seq=3 ttl=66 time=29.5 ms

— 114.114.114.114 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 29.510/29.715/30.074/0.254 ms
这个主机 192.168.0.124 到公网IP地址 114.114.114.114 网络可达
PING www.a.shifen.com (183.232.231.174) 56(84) bytes of data.
64 bytes from localhost (183.232.231.174): icmp_seq=1 ttl=55 time=17.2 ms
64 bytes from localhost (183.232.231.174): icmp_seq=2 ttl=55 time=17.0 ms
64 bytes from localhost (183.232.231.174): icmp_seq=3 ttl=55 time=16.9 ms

— www.a.shifen.com ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2007ms
rtt min/avg/max/mdev = 16.975/17.092/17.258/0.193 ms
这个主机 192.168.0.124 到 www.baidu.com 域名网络可达

2、服务检查

[root@test1 ~]# sh network_check.sh
1、基础网络检查
2、指定服务网络连通性检查
3、退出
你的选择为:2
请输入需要检测的主机地址(格式样例:192.168.0.100):192.168.0.132
请输入需要检测的服务端口(格式样例:80),如果无请直接输入回车:8085
这个主机到 192.168.0.132 主机的 8085 服务网络可达
[root@test1 ~]# sh network_check.sh
1、基础网络检查
2、指定服务网络连通性检查
3、退出
你的选择为:2
请输入需要检测的主机地址(格式样例:192.168.0.100):192.168.0.132
请输入需要检测的服务端口(格式样例:80),如果无请直接输入回车:8086
这个主机到 192.168.0.132 主机的 8086 服务网络不可达

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论