Shell脚本实现Centos相关配置0.1版本

       之所以写这个脚本,一是为了熟悉Shell编程,而是减少重复的配置,只需要修改脚本中的命令即可实现相关的配置;关于本脚本的功能会持续补充,先从基本的需求做起。

已上传到本人的git地址:https://github.com/LandStart/LinuxShell.git

实现的功能有:1)配置静态ip,2)配置主机名 3)配置ssh免密登录 4)修改Centos镜像源

注意运行本脚本,首先需要有网络才可以下载相关的程序。

#!/bin/bash

echo "Before operating....................................."

mustCommandList=("ifconfig" "wget" "git" "figlet")
for i in ${mustCommandList[@]}
do
	if ! [ -x "$(command -v $i)" ]; then
    	echo 'checking the : '$i' is not installed...,prepared to install...' >&2
    	`sudo yum install $i`
	else
		echo ${i} " has  installed."
	fi
done

echo "===================================================="
echo "= your os is :" `uname`"                               ="
echo "= current time is : " `date`                    "  ="
echo "===================================================="
figlet Lad_DDD
echo "please select your number : 0) exit programer "
echo "please select your number : 1) config static ipAddress "
echo "please select your number : 2) config hostname"
echo "please select your number : 3) config ssh without password "
echo "please select your number : 4) config mirro origin"

ipconfig='/etc/sysconfig/network-scripts/ifcfg-eth0'

while true 
do
	read -p "input the number " SerialNumber	
	case $SerialNumber in
	1) echo "start config static ipAddress"
	   sed -i 's/dhcp/static/g' ifcfg-eth0
	   sed -i 's/ONBOOT=no/ONBOOT=yes/g' ifcfg-eth0
  	   grepip=`grep 'IPADDR' ifcfg-eth0`
	   if [ $? -ne 0 ]
 	    then
    	   	sed -i '$a\IPADDR=192.168.241.100' ifcfg-eth0
	   fi
	   grepnet=`grep 'NETMASK' ifcfg-eth0`
	   if [ $? -ne 0 ]
 	    then
    		sed -i '$a\NETMASK=255.255.255.0' ifcfg-eth0
	   fi
	   grepgate=`grep 'GATEWAY' ifcfg-eth0`
	   if [ $? -ne 0 ]
 	    then
    	        sed -i '$a\GATEWAY=192.168.241.2' ifcfg-eth0
	   fi
           restartnetwork=`sudo ervice network restart`
	;;
	2)echo "start config hostname"
	  read -p "config the hostname: " -t 100 hostname
	  read -p "config the hostnameipaddress: " -t 100 ip
	  echo -e "$hostname"
	  echo $ip
	  grepip=`grep $ip /home/land/hosts`
	  if [ $? -ne 0 ]
 	   then
    	  	sed -i '$a\'$ip /home/land/hosts
    	  grephost=`grep "$hostname" /home/land/hosts`
    	  if [ $? -ne 0 ]
     	   then 
        	sed -i 's/'$ip'/&'" $hostname "'/g' /home/land/hosts
    	  fi
	  else
   		sed -i 's/'$ip'/&'" $hostname "'/g' /home/land/hosts
	  fi
	;;
	3)echo "start ssh without password"
	  finstallssh=`ssh`
	  if [ $? -ne 0 ]
 	   then
   	  	`ssh-keygen`
	  else
       		`sudo yum install ssh`
        	`ssh-keygen`
	  fi
	  read -p "please input use need to access with no use the password: " user
	  read -p "please input ipAddress need to access witiiih no use the password: " server
		`ssh-copy-id -i ~/.ssh/id_rsa.pub $user@$server`
	;;
	0)
	 break
	;;
	4)
          `sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak`
	  `sudo /etc/yum.repos.d/`
	  `sudo wget http://mirrors.163.com/.help/CentOS7-Base-163.repo`	
	;;
	*)echo 'plase input the correct select'
	;;
	esac
 done





 

如果问题,敬请指出,与君共勉。谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值