SHELL-常用的功能脚本

最近,大量测试虚拟机,结果安装复制很麻烦的,特别有时候需要重新配置常用项的内容。还是写一个脚本自动完成吧。

错误提示:

如果执行的时候出现“-bash: ./ecs.sh: /bin/bash^M: bad interpreter: No such file or directory”错误则将文件的格式保存为unix格式试试看。

目前有的功能:

  1. 网卡配置生成
  2. YUM 阿里云镜像
  3. 虚拟机 共享软件夹

源码如下

#!/bin/bash

if [ $(id -u) != "0" ]; then
    echo "You must be root to run this script"
    exit 1
fi

# 网卡配置生成
eth_generate()
{
	while true
	do
		read -p "please input network card's name:" eth_input_name
		read -p "please input network card's IP ADDR:" eth_input_ipaddr
		read -p "please input network card's Gate WAY:" eth_input_gateway
		if [[ ${#eth_input_name} -gt 0 && ${#eth_input_ipaddr} -gt 0 && ${#eth_input_gateway} -gt 0 ]];
		then
			break
		else
			echo '******'
		fi
	done
	ethconfig="/etc/sysconfig/network-scripts/ifcfg-$eth_input_name"
	if [ -e /etc/sysconfig/network-scripts/ifcfg-$eth_input_name ];
	then
		mv $ethconfig $ethconfig.bak.$(date +"%Y%m%d")
	fi
	echo "TYPE=Ethernet
BOOTPROTO=static
NAME=$eth_input_name
DEVICE=$eth_input_name
ONBOOT=yes
IPADDR=$eth_input_ipaddr
GATEWAY=$eth_input_gateway">>$ethconfig
	if [ -e $ethconfig ];
	then
		echo '##### config generate OK~! #####'
	fi
	service network restart
	mv /etc/resolv.conf /etc/resolv.conf.bak.$(date +"%Y%m%d")
	echo "nameserver 223.5.5.5
nameserver 223.6.6.6">>/etc/resolv.conf
	exit 1
}

# YUM 阿里云镜像
yum_source_aliyun()
{
	if grep -Eqi "release 6." /etc/redhat-release; then
		centosver='6'
	elif grep -Eqi "release 7." /etc/redhat-release; then
		centosver='7'
	fi
	if [[ centosver -eq 6 || centosver -eq 7 ]];
	then
		mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak.$(date +"%Y%m%d")
		curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-$centosver.repo
		echo '##### CentOS-Base.repo is download OK~! #####'
		yum makecache
		echo '##### CentOS-Base.repo CACHE is OK~! #####'
	fi
	exit 1
}

# 虚拟机 共享软件夹
# 1.执行之前请点击 设备 安装增强工具
# 2.设置 共享文件夹 设定 共享文件夹名称,脚本将用到
vbox_sharefloder()
{
	# 0
	echo '1.执行之前请点击 设备 安装增强工具'
	echo '2.设置 共享文件夹 设定 共享文件夹名称,脚本将用到'
	read -p "你是否已经准备好了?准备好请输入yes回车。" isok
	if [ "$isok" != "yes" ];
	then
		echo '请前往设置后再来尝试。'
		exit 1
	fi
	# 1
	read -p "请输入共享文件夹名称: " sharefoldername
	if [[ ${#sharefoldername} -le 0 ]];
	then
		echo "共享文件夹名称 必填"
		exit 1
	fi
	read -p "请输入您希望挂载到的目录,默认/data: " mountto
	if [ ${#mountto} -le 0 ];
	then
		mountto='/data'
	fi
	# 2
	yum install kernel-headers kernel-devel gcc* make -y
	if [ ! -e /mnt/cdrom ];
	then
		mkdir -p /mnt/cdrom
	fi
	# 3
	mount /dev/cdrom /mnt/cdrom
	cd /mnt/cdrom
	./VBoxLinuxAdditions.run
	# umount /mnt/cdrom
	# 4
	if [ ! -e $mountto ];
	then
		mkdir -p $mountto
	fi
	mount -t vboxsf $sharefoldername $mountto
	# 5
	cp /etc/fstab /etc/fstab.bak.$(date +"%Y%m%d")
	echo $sharefoldername $mountto vboxsf rw,gid=100,uid=1000,auto 0 0 >> /etc/fstab
}


#
SelectTo="$1"

#
echo "1: Auto generate network card config(as ifcfg-eth0)"
echo "2: YUM source mirror from aliyun"
echo "3: Visual Box's Tool set"
echo "0: DO NOTHING"

#
if [ ${#SelectTo} -le 0 ];
then
	read -p "Choice One To Setup (1,2,3 or 0): " SelectTo
fi

#
echo '-----';

case "${SelectTo}" in
1)
	eth_generate
	;;
2)
	yum_source_aliyun
	;;
3)
	vbox_sharefloder
	;;
0)
	exit 1
	;;
*)
	echo "you can run :$0 [1|2|3...]"
	exit 1
esac

发表评论