Linux shell应用1-自动配置网络
2011-10-18 00:07
316 查看
1. 概述
Linux shell给我们提供了很多有用的命令,利用这些命令,能够自动的完成许多复杂的任务。如察看每个用户所占用的磁盘容量,CPU负载及内存情况,自动管理网络,定时下载等功能。而这篇文章中主要是介绍一下,利用脚本来实现自动配置网络。特别是固定IP的配置,对于初学者来说,配置网络可能比较麻烦,而通过shell脚本,就可以轻松的完成配置网络的功能。
2. shell配置网络
(1)网络知识介绍
在前面的文章中,我们已经介绍了Linux shell基础编程,正则表达式和高级编程。简单回忆一下,其中基础编程,主要包括结构化命令,用户输入,流式编辑器sed和gawk基础知识,而高级编程主要有正则表达式的应用,sed,gawk高级部分等。对于配置静态IP上网,通过需要配置以下内容,IP地址,网关,子网掩码,DNS服务器,网卡MAC地址。Linux系统中有几个重要的配置文件:
/etc/resolv.conf- DNS配置文件
/etc/hosts-主机名到IP地址的映射
/etc/sysconfig/network-所有的网络接口和路由信息
/etc/sysconfig/network-script/ifcfg-<interface-name>-每一个网络接口的配置信息
(2)自动配置网络的shell脚本
#!/bin/bash
read -p "do you want to configure nework[Y/N]?" reply #读入信息,是否配置网络
case $reply in
Y|y)
echo
echo "***************************************"
echo "--------network configuration----------"
echo
ifconfig eth0 down #关闭网卡,在配置之前先关闭网卡,我的机器上是eth0
while true
do
read -p "please input IP:" IP
if echo $IP|grep "/([0-9]/{1,3/}/.[0-9]/{1,3/}/.[0-9]/{1,3/}/.[0-9]/{1,3/}/)
/(/([0-9]/{1,3/}/./)/{3/}[0-9]/{1,3/}/)" #IP地址的正则表达式
then
ifconfig eth0 $IP #配置IP
break
else
echo "please check IP format..."
fi
done
while true
do
read -p "please input netmask:" NETMASK
if ifconfig eth0 netmask $NETMASK #配置子网掩码
then
break
fi
echo "please check netmask format..."
done
while true
do
read -p "please input gateway:" GATEWAY
if route add default gw $GATEWAY #配置网关
then
break
fi
echo "please check gateway format..."
done
while true
do
ifconfig eth0 down
read -p "please input MAC:" MAC
if ifconfig eth0 hw ether $MAC #配置对应网卡的MAC
then
break
fi
echo "please check MAC format..."
done
while true
do
read -p "please input DNS:" DNS
if echo $DNS|grep "/([0-9]/{1,3/}/.[0-9]/{1,3/}/.[0-9]/{1,3/}/.[0-9]/{1,3/}/)
/(/([0-9]/{1,3/}/./)/{3/}[0-9]/{1,3/}/)"
then
DNS=`echo nameserver $DNS` #配置DNS
echo $DNS >>/etc/resolv.conf
break
else
echo "please check DNS format..."
fi
done
ifconfig eth0 up #激活网卡
/etc/rc.d/init.d/network restart #重新启动网络
mentohust=`whereis mentohust`
if [ mentohust="mentohust:" ] #如果是锐捷拨号
then
echo "there is no mentohust...."
else
mentohust & #以后台的方式启动锐捷
fi
TEMP=`mktemp -t temp.XXXXXX`
echo $TEMP
echo "Testing Network......" #测试网络连通性
exec 2>$TEMP
`echo ping -c 5 www.sina.com.cn` >>$TEMP
if grep "unknown host" $TEMP #测试一下ping的数据包是否含有unknown host
then
echo "network timeout,please check network configuration"
else
echo
echo "*******************************************"
echo "OK,good luck!"
fi
rm -rf $TEMP;;
N|n)
exit;;
esac
就此,利用这个shell脚本就可以轻松的配置网络了。
关于Linux shell脚本配置自动配置网络就介绍到这里了。
Linux shell给我们提供了很多有用的命令,利用这些命令,能够自动的完成许多复杂的任务。如察看每个用户所占用的磁盘容量,CPU负载及内存情况,自动管理网络,定时下载等功能。而这篇文章中主要是介绍一下,利用脚本来实现自动配置网络。特别是固定IP的配置,对于初学者来说,配置网络可能比较麻烦,而通过shell脚本,就可以轻松的完成配置网络的功能。
2. shell配置网络
(1)网络知识介绍
在前面的文章中,我们已经介绍了Linux shell基础编程,正则表达式和高级编程。简单回忆一下,其中基础编程,主要包括结构化命令,用户输入,流式编辑器sed和gawk基础知识,而高级编程主要有正则表达式的应用,sed,gawk高级部分等。对于配置静态IP上网,通过需要配置以下内容,IP地址,网关,子网掩码,DNS服务器,网卡MAC地址。Linux系统中有几个重要的配置文件:
/etc/resolv.conf- DNS配置文件
/etc/hosts-主机名到IP地址的映射
/etc/sysconfig/network-所有的网络接口和路由信息
/etc/sysconfig/network-script/ifcfg-<interface-name>-每一个网络接口的配置信息
(2)自动配置网络的shell脚本
#!/bin/bash
read -p "do you want to configure nework[Y/N]?" reply #读入信息,是否配置网络
case $reply in
Y|y)
echo
echo "***************************************"
echo "--------network configuration----------"
echo
ifconfig eth0 down #关闭网卡,在配置之前先关闭网卡,我的机器上是eth0
while true
do
read -p "please input IP:" IP
if echo $IP|grep "/([0-9]/{1,3/}/.[0-9]/{1,3/}/.[0-9]/{1,3/}/.[0-9]/{1,3/}/)
/(/([0-9]/{1,3/}/./)/{3/}[0-9]/{1,3/}/)" #IP地址的正则表达式
then
ifconfig eth0 $IP #配置IP
break
else
echo "please check IP format..."
fi
done
while true
do
read -p "please input netmask:" NETMASK
if ifconfig eth0 netmask $NETMASK #配置子网掩码
then
break
fi
echo "please check netmask format..."
done
while true
do
read -p "please input gateway:" GATEWAY
if route add default gw $GATEWAY #配置网关
then
break
fi
echo "please check gateway format..."
done
while true
do
ifconfig eth0 down
read -p "please input MAC:" MAC
if ifconfig eth0 hw ether $MAC #配置对应网卡的MAC
then
break
fi
echo "please check MAC format..."
done
while true
do
read -p "please input DNS:" DNS
if echo $DNS|grep "/([0-9]/{1,3/}/.[0-9]/{1,3/}/.[0-9]/{1,3/}/.[0-9]/{1,3/}/)
/(/([0-9]/{1,3/}/./)/{3/}[0-9]/{1,3/}/)"
then
DNS=`echo nameserver $DNS` #配置DNS
echo $DNS >>/etc/resolv.conf
break
else
echo "please check DNS format..."
fi
done
ifconfig eth0 up #激活网卡
/etc/rc.d/init.d/network restart #重新启动网络
mentohust=`whereis mentohust`
if [ mentohust="mentohust:" ] #如果是锐捷拨号
then
echo "there is no mentohust...."
else
mentohust & #以后台的方式启动锐捷
fi
TEMP=`mktemp -t temp.XXXXXX`
echo $TEMP
echo "Testing Network......" #测试网络连通性
exec 2>$TEMP
`echo ping -c 5 www.sina.com.cn` >>$TEMP
if grep "unknown host" $TEMP #测试一下ping的数据包是否含有unknown host
then
echo "network timeout,please check network configuration"
else
echo
echo "*******************************************"
echo "OK,good luck!"
fi
rm -rf $TEMP;;
N|n)
exit;;
esac
就此,利用这个shell脚本就可以轻松的配置网络了。
关于Linux shell脚本配置自动配置网络就介绍到这里了。
相关文章推荐
- Linux shell应用1-自动配置网络
- Centos网络配置(手动设置和自动获取)IP的2种方法
- 自动切换机器的网络配置
- CNN:人工智能之神经网络算法进阶优化,六种不同优化算法实现手写数字识别逐步提高,应用案例自动驾驶之捕捉并识别周围车牌号—Jason niu
- sql2005 外围应用配置器:计算机 localhost 在网络上不存在,或无法远程配置该计算机……
- Windows2008应用之配置客户端自动添加打印机
- 在RDO自动安装的OpenStack Liberty单机中搭建能够内外网互访的网络(三)-配置规则和创建密钥对
- linux shell 脚本自动从配置远程ftp服务上下载更新文件
- 一个自动配置网络的批处理文件
- centos网络配置方法(手动设置,自动获取)
- 深入理解Spring系列之七:web应用自动装配Spring配置
- Linux shell应用2-自动下载文件
- 用批处理备份还原网络配置并实现不同IP间的切换(netsh应用)
- TF-IDF与余弦相似性的应用(一):自动提取关键词 - 阮一峰的网络日志
- SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- Linux中的网络配置及其应用
- centos网络配置方法(手动设置,自动获取)
- centos7 配置网络,自动获取ip地址