修改DNS的Linux脚本,Shell脚本-配置网络

原先学习过shll但是只是了解一下它的语法而已,在平时中并没有使用,在暑假的时候又

想起了shell,所以又回顾了一下,学习后一定要用,这样才能掌握。最近实验室刚装好机子,什么

都要配置一下,包括网络,以前配置网络是通过图形界面,但是ubuntu8.10以后图形配置界面有

不小的变化浪费了我不少时间,所以以后配置网络就不用图形界面了,直接找系统的配置文件,

先修改网卡文件/etc/network/interfaces文件,我用的是静态IP,网卡设备为eth0,所以配置文件为

auto lo

iface lo inet loopback

iface eth0 inet static

address 198.6.10.153

netmask 255.255.255.0

gateway 198.6.10.153

auto eth0

然后修改DNS文件/etc/resolv.conf文件,我的如下:

nameserver 198.6.10.123

但这样以来就是每次要修改两次文件,而且配置完成后还要重启网络:

sudo /etc/init.d/networking restart

虽然很直接也比较快,但每次配置机子也挺麻烦的,这时候shell就能发挥作用了,写了一个

简单的配置网络的shell脚本(文件名config_net.sh):

#!/bin/sh

#根据自己的机子修改以下变量,以下变量为默认配置参数值

IP=192.168.89.15    #这个是IP

NETMASK=255.255.255.0    #这个是子网掩码

GATEWAY=192.168.89.1    #这个是网关

DNS=202.117.128.2    #这个是DNS

DNS_DIR=/etc/resolv.conf    #DNS文件路径

IP_DIR=/etc/network/interfaces    #网卡配置文件路径

LOOP=”iface lo inet loopback”

ETH0=”iface eth0 inet static”

if [ $# -eq 4 ]; then

IP=$1;NETMASK=$2;GATEWAY=$3;DNS=$4;    #如果带4个参数运行

elif [ $# -eq 1 ]; then

IP=$1;    #如果带1个参数运行

elif [ $# -eq 0 ] ;then

echo “You use default configure”    #如果不带参数运行

else    #错误的使用方式,打印使用方法

echo “ERR ARGUMENT,Follow is right:”

echo “./config_net.sh IP NETMASK GATEWAY DNS”

echo “(OR)./config_net.sh IP”

echo “(OR)./config_net.sh”

exit

fi

#将配置写入到网卡配置文件中

echo “auto lo

${LOOP}

${ETH0}

address ${IP}

netmask ${NETMASK}

gateway ${GATEWAY}

auto eth0″>${IP_DIR}

#将配置写入到DNS文件中

echo “nameserver ${DNS}”>${DNS_DIR}

#重启网络

/etc/init.d/networking restart

上面的脚本很简单,实现的功能就是配置网络,可以有以下使用方法:

1.$ ./config_net.sh   (采用默认的配置参数)

2. $ ./config_net.sh IP    (可以指定IP,其它参数默认)

3. $ ./config_net.sh IP NETMASK GATEWAY DNS    (指定IP,子网掩码,网关,DNS)

第一种方法可以直接修改文件中的默认值来配置。

第二种可以用在配置局域网中,可以修改此脚本,设定默认的子网掩码,DNS,网关,然后运行时

只需指定IP即可,可以快速配置多台系统。

第三种直接指定各项配置

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440427.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【ZOJ - 3872】Beauty of Array(思维,计算贡献,枚举)

题干: Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A. Input There…

linux 使用gzip压缩打包的文件,linux常用的解压,压缩,打包

gzip zcat[rootlinux ~]# gzip [-cdt#] 文件名[rootlinux ~]# zcat 文件名.gz参数:-c : 将压缩的数据输出到屏幕上,可通过数据流重导向来处理-d :解压缩参数-t : 可以用来检验一个压缩文件的一致性~看看文件有无错误-#…

【POJ - 1836】Alignment(dp,LIS,最长上升子序列类问题)

题干: In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the …

Windows共享Linux打印机,linux – 如何为cups客户端构建windows共享打印机的url

从this page开始:smbThis backend sends print files to printers shared by a Windows host. Examplesof CUPS device-URIs that may be used includes:smb://workgroup/server/printersharenamesmb://server/printersharenamesmb://username:passwordworkgroup/se…

【HDU - 1080】Human Gene Functions(dp,可编辑距离类问题)

题干: 给你两个DNA序列(长度不一定相同),你可以在其中任意位置上加入空格,使得最终他俩长度相同,最终相同长度的两个DNA序列会有个相似度比较(每个字符相对应的比较),问…

linux 对硬盘重新分区,硬盘重新分区后,linux的硬盘表的重新设置

硬盘重新分区后,linux的硬盘表的重新设置发布时间:2007-12-29 16:04:19来源:红联作者:Alwaysfirm会硬盘分区后uuid会变动,导致linux挂载硬盘出错。-----什么是uuid?UUID,全称Universally Unique Identifier它是一个128位,16字节的…

【POJ - 1837】Balance(dp及其优化)

题干: Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders two arms of negligible weight and each arms length is 15. Some hooks are attached to these arm…

linux awk 时间范围,linux下使用awk命令按时间段筛选日志

需求很多时候我们需要按照时间段来进行日志的分析,比如说查看上午的,或者某月某日的的具体日志,就不能简单实用tail -f或者head -n命令了。这个时候我们需要借用awk。命令zcat com.log20160529.gz | grep dianping_reply.log| awk {split($4,…

【CodeForces - 1153D】Serval and Rooted Tree(树形dp)

题干: Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree. …

【OpenJ_Bailian - 4117】简单的整数划分问题(dp)

题干: 将正整数n 表示成一系列正整数之和,nn1n2…nk, 其中n1>n2>…>nk>1 ,k>1 。 正整数n 的这种表示称为正整数n 的划分。正整数n 的不同的划分个数称为正整数n 的划分数。 Input 标准的输入包含若干组测试数据。每组测试…

【ZOJ - 3778】Talented Chef(贪心)

题干: As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. Tonight he is going to have a hearty dinner with his girlfriend at his home. Of course, Coach Gao is going to cook all dishes himself, in …

实验楼Linux基础挑战2答案,实验楼-Linux基础-实验二 Linux的基本概念及操作

一、实验介绍1.1 实验内容实验楼环境介绍常用 Shell 命令及快捷键Linux 使用小技巧1.2 实验知识点Linux 基本命令通配符的使用查看帮助文档二、桌面环境1.Linux 桌面环境介绍相对于现在的 Windows 系统,UNIX/Linux 本身是没有图形界面的,我们通常在 UNIX…

【ZOJ - 3780】Paint the Grid Again(拓扑排序,图论,证明性质)

题干: Leo has a grid with N N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the…

linux bin su,linux – su:/ bin / bash:资源暂时不可用

无法将用户切换为postgres.postgres用户的ulimit设置设置了合理的限制.我们没有达到最高限度./ var / log / messages中没有错误.BETA -bash-4.2# sudo su - postgressu: /bin/bash: Resource temporarily unavailable设置:BETA -bash-4.2# ps -auxww | grep -i pos…

*【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)

题干: Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both…

c语言程序图片马赛克,关于c语言的图像均值滤波 请问大神为什么我的结果都是马赛克...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include #include #include #include #include #include #include "stdlib.h"#include "string.h"#define width 256#define higth 256//原图象的宽度和高度int lvbo(unsigned char D[]){int a;a(D[0]D[1]D[2…

【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)

题干: On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step h…

c语言程序设计现代方法快速排序,C语言实现快速排序改进版

利用三者取中法改进快速排序,具体内容如下实现取数组中第一个,中间和最后一个元素的中间元素作为划分元素(否则将这些元素排除在划分过程之外).大小为11或更小的数组在划分过程中被忽略,然后使用插入排序来完成排序.#include #include #include #include #include #…

c语言一个数组后添加元素append,jQuery 追加元素、拼接元素的方法总结(append、html、insertBefore、before等)...

1. append & appendTo 的功能均为:在被选元素结尾(仍在元素内部)插入指定内容,但是内容和选择器的位置不同(1) append()方法:$("#test").append("测试"); //在id为test元素内部末尾插入测试(2) appendTo()方法&…

【ZOJ - 4024】Peak(模拟,水题)

题干: A sequence of integers is called a peak, if and only if there exists exactly one integer such that , and for all , and for all . Given an integer sequence, please tell us if its a peak or not. Input There are multiple test cases. …