之所以写这个脚本,一是为了熟悉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
如果问题,敬请指出,与君共勉。谢谢。