#!/bin/bash #Name:init.sh #Author:amd5.cn #Version:Deepin 15 Professional #Datetime: 2017-09-13 #Usage: sudo bash init.sh (weixin|qq) #定义颜色 SCREEN=`stty -F /dev/console size 2>/dev/null` COLUMNS=${SCREEN#* } [ -z $COLUMNS ]&& COLUMNS=240 SPA_COL=$[$COLUMNS-230] RED='\033[35m' BLUE='\033[36m' REDS='\033[1;31m' GREEN='\033[37m' BLUES='\033[34m' END='\033[0m' success(){ string=$1 RT_SPA=$[$SPA_COL-${#string}] echo -en $string for i in `seq 1 $RT_SPA`;do echo -n " " done echo -e "[${BLUES}${2}${END}]" } ok(){ string=$1 RT_SPA=$[$SPA_COL-${#string}] echo -en $string for i in `seq 1 $RT_SPA`;do echo -n " " done echo -e "[${GREEN}${2}${END}]" } failde(){ string=$1 RT_SPA=$[$SPA_COL-${#string}] echo -en $string for i in `seq 1 $RT_SPA`;do echo -n " " done echo -e "[${RED}${2}${END}]" } #通用安装deb软件包函数:$1=安装包的名称(必选),$2=deb包所在目录(必选)。 install_deb(){ if [[ $1 == '' ]] || [[ $2 == '' ]];then failde 缺少必要的安装文件 安装失败 return 1 fi if [[ $3 == 'force' ]];then if [[ -d "$2" ]];then echo "软件:"${BLUE}${1}${END}",开始安装......" echo "正在安装................................................" dpkg -i $2/*.deb >/dev/null 2>&1 if [[ $? -eq 0 ]];then version=`dpkg -l | grep $1 | awk -F' ' 'NR==1{print $3}'` success 软件:${BLUE}${1}${END},版本:${RED}${version}${END} 安装成功 else failde 软件:${BLUE}${1}${END} 安装失败 return 2 fi else failde 安装包所在的目录${RED}${2}${END}不存在 安装失败 return 3 fi else dpkg --list | grep $1 >/dev/null 2>&1 if [[ $? -eq 0 ]];then version=`dpkg -l | grep $1 | awk -F' ' 'NR==1{print $3}'` ok 软件:${BLUE}${1}${END},版本:${RED}${version}${END} 已经安装 return 4 else if [[ -d "$2" ]];then echo -e "软件:"${BLUE}${1}${END}",开始安装......" echo "正在安装................................................" dpkg -i $2/*.deb >/dev/null 2>&1 if [[ $? -eq 0 ]];then version=`dpkg -l | grep $1 | awk -F' ' 'NR==1{print $3}'` success 软件:${BLUE}${1}${END},版本:${RED}${version}${END} 安装成功 else failde 软件:${BLUE}${1}${END} 安装失败 return 2 fi else failde 安装包所在的目录${RED}${2}${END}不存在 安装失败 return 3 fi fi fi return 0 } renew_deb(){ if [[ $1 == '' ]] || [[ $2 == '' ]];then failde 缺少必要的安装文件 安装失败 return 1 elif [[ $3 == 'over' ]];then install_deb $1 $2 force else dpkg --list | grep $1 >/dev/null 2>&1 if [[ $? -eq 0 ]];then echo -e "软件:"${BLUE}${1}${END}" 已经安装,即将升级到最新版本。" echo "正在卸载................................................" dpkg -P $1 >/dev/null 2>&1 if [[ $? -eq 0 ]];then success 软件:${BLUE}${1}${END} 卸载成功 else failde 软件:${BLUE}${1}${END} 卸载失败 fi install_deb $1 $2 else install_deb $1 $2 fi fi return 0 } #拷贝配置文件函数:$1=配置文件所在目录(必选),$2=配置文件的目标位置(必选),$5=需要重新加载的服务(可选). config_file(){ if [[ $1 == '' ]] || [[ $2 == '' ]];then failde 缺少必要的安装文件 安装失败 return 1 else if [[ -d "$1" ]] && [[ -d "$2" ]];then cp -rf $1/* $2 >/dev/null 2>&1 if [[ $? -eq 0 ]];then success 拷贝文件${BLUE}${1}${END}到${RED}${2}${END} 复制成功 else failde 拷贝文件${BLUE}${1}${END}到${RED}${2}${END} 复制失败 return 2 fi else failde 源目录${RED}${1}${END}或目标目录${RED}${2}${END}不存在 安装失败 return 3 fi fi if [[ $3 != '' ]];then eval "$3" if [[ $? -eq 0 ]];then success 命令:"${BLUE}${3}${END}" 执行成功 else failde 命令:"${BLUE}${3}${END}" 执行失败 return 4 fi fi return 0 } #挂载共享目录 mount_share() { [[ -d /home/$1 ]] || mkdir /home/$1 cat /etc/fstab | grep "//10.10.255.226/"$1 >/dev/null 2>&1 if [[ $? -ne 0 ]];then echo "//10.10.255.226/"$1" /home/"$1" cifs username=share,password=share,auto,ro 0 0" >> /etc/fstab mount -a > /dev/null 2>&1 && success 共享目录挂载 挂载成功 || failde 共享目录挂载 挂载失败 else success 共享目录挂载 已经挂载 mount -a > /dev/null 2>&1 && success 共享目录挂载测试 测试成功 || failde 共享目录挂载测试 测试失败 fi [[ $1 == "zx_wx" ]] && svn="wx" || svn="wz" if [[ -h "/home/myuser/桌面/share-$svn" ]];then ok 共享目录链接 已经创建 elif [[ -h "/home/myuser/Desktop/share-$svn" ]];then ok 共享目录链接 已经创建 else [[ -h "/home/myuser/桌面/share-$svn" ]] || ln -s /home/$1 /home/myuser/桌面/share-$svn [[ $? -eq 0 ]] && success 共享目录链接 创建成功 || failde 共享目录链接 创建失败 return 0 fi } #判断是否是root,不是则直接退出. [[ "$UID" -ne 0 ]] && echo "ERROR: 需要以root运行,退出!!!" && exit 1; #设置电脑使用utc时区 #sed -i 's/^UTC=no$/UTC=yes/g' /etc/default/rcS #sed -i 's/^UTC=no$/UTC=yes/g' /etc/default/rcSe #sed -n '/^UTC/p' /etc/default/rcS #source /etc/default/rcS #source /etc/default/rcSe echo " 5 9 1 1 " | tzselect >/dev/null 2>&1 [[ $? -eq 0 ]] && success 电脑UTC时区:TZ=Asia/Shanghai 设置成功 || failde 电脑UTC时区:TZ=Asia/Shanghai 设置失败 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime >/dev/null 2>&1 echo "Asia/Shanghai" > /etc/timezone [ -f /etc/profile.d/tz.sh ] || cp -f ./tz.sh /etc/profile.d/ #新建账户并初始化 id -u myuser >/dev/null 2>&1 if [[ $? -ne 0 ]];then failde 员工账号:${BLUE}myuser${END} 不存在 useradd myuser -s /bin/bash -m success 员工账号:${BLUE}myuser${END} 创建成功 else #rm -rf /home/myuser/* >/dev/null ok 员工账号:${BLUE}myuser${END} 已经存在 fi echo "myuser:myuser" | chpasswd >/dev/null 2>&1 if [[ $? -eq 0 ]];then success 员工账号:${BLUE}myuser${END}密码 更新成功 else failde 员工账号:${BLUE}myuser${END}密码 更新失败 exit 1 fi #强制myuser为myuser组用户(删除sudo权限) usermod -g myuser myuser >/dev/null 2>&1 #新建备用账户并初始化 id -u myadmin >/dev/null 2>&1 if [[ $? -ne 0 ]];then failde 管理员账号:${BLUE}myadmin${END} 不存在 useradd myadmin -s /bin/bash -m success 管理员账号:${BLUE}myadmin${END} 创建成功 else ok 管理员账号:${BLUE}myadmin${END} 已经存在 fi echo "myadmin:abcACY!" | chpasswd >/dev/null 2>&1 if [[ $? -eq 0 ]];then success 管理员账号:${BLUE}myadmin${END}密码 更新成功 else failde 管理员账号:${BLUE}myadmin${END}密码 更新失败 exit 1 fi #强制myadmin为sudo组用户(增加sudo权限) usermod -a -G sudo myadmin>/dev/null 2>&1 #账户root配置zsh install_deb zsh zsh install_deb oh-my-zsh oh-my-zsh cp -f ./zsh/.zshrc /root/ cp -f ./zsh/mortalscumbag.zsh-theme /usr/share/oh-my-zsh/themes/ echo "/bin/zsh" | chsh >/dev/null 2>&1 #配置root账号密码 echo "root:abcACY!@#" | chpasswd >/dev/null 2>&1 #锁定除myuser外普通账户 cat /etc/passwd |awk -F : '{if ($3 >= 1000) if ($1 !~ "myuser|nobody|myadmin") print $1 }' | while read us;do passwd -l $us && echo "User: "$us" is locked" || echo "User: "$us"locked error."; done #将修改密码和查看ip脚本复制到桌面 [[ ! -d "/home/myuser/桌面/admin" ]] && mkdir -p /home/myuser/桌面/admin && config_file script /home/myuser/桌面/admin chown -R myuser:myuser /home/myuser chmod a+x /home/myuser/桌面/admin/* #修改主机名称 sed -i '/min-pc/d' /etc/hosts hostname | grep -E 'KS[0-9]{4}' >/dev/null if [[ $? -eq 0 ]];then ok 主机名称:${RED}`hostname`${END} 已经设置 else read -p "请输入主机名称:" name name=`echo $name | tr 'a-z' 'A-Z'` echo "$name" >/etc/hostname && hostname $name && success 主机名称:${RED}`hostname`${END} 设置成功 fi #账户安全策略 install_deb libpam-cracklib libpam pamconfig=pampassword if [[ $? -eq 0 ]];then if [[ -d "$pamconfig" ]];then if [[ -e "$pamconfig/login.defs" ]];then cp -f "$pamconfig/login.defs" /etc/ cat "/etc/login.defs" |sed -n "s/^####\([^\#]*\)$/\1/p" else failde 账户安全策略,缺少配置文件login.defs 设置失败 fi if [[ -e "$pamconfig/common-password" ]];then cp -f "$pamconfig/common-password" /etc/pam.d/ cat "/etc/pam.d/common-password" |sed -n "s/^####\([^\#]*\)$/\1/p" success 账户安全策略 设置成功 else failde 账户安全策略,缺少配置文件common-password 设置失败 fi else failde 账户安全策略,没有找到配置文件夹 设置失败 fi fi #配置iptables config_file iptables /etc "iptables-restore /etc/iptables.rules" #删除个人qq if [[ $1 == "weixin" ]];then dpkg --list |grep apps.com.qq.im >/dev/null 2>&1 if [[ $? -eq 0 ]];then dpkg --purge apps.com.qq.im >/dev/null 2>&1 [[ $? == 0 ]] && success 软件:${BLUE}腾讯个人QQ${END} 删除成功 || failde 软件:${BLUE}腾讯个人QQ${END} 删除失败 else ok 软件:${BLUE}腾讯个人QQ${END} 未安装 fi else echo "不用删除个人qq。" fi #安装个人qq if [[ $1 == "qq" ]];then install_deb deepinwine-qq deepin-qq fi #更新crossover version=`dpkg -l | grep crossover | awk -F' ' 'NR==1{print $3}'` if [[ $version == "16.0.6-1" ]];then ok 软件:${BLUE}crossover${END},版本:${RED}${version}${END} 已经安装 else crossover=`dpkg -l | grep crossover | awk 'NR==1{print $2}' | awk -F':' '{print $1}'` dpkg -P apps.com.qq.b.eim deepinwine-hrtx apps.com.qq.b.crm deepin-crossover-helper $crossover > /dev/null 2>&1 install_deb crossover crossover16 fi #安装企业qq install_deb apps.com.qq.b.eim qyqq #安装chrome version=`dpkg -l | grep google-chrome-stable | awk -F' ' '{print $3}'` if [[ $version == "60.0.3112.78-1" ]];then ok 软件:${BLUE}google-chrome${END},版本:${RED}${version}${END} 已经安装 else renew_deb google-chrome-stable chrome fi #配置ssh install_deb openssh-server ssh #if [[ $? == 0 ]];then if [[ -d "ssh" ]];then cp -f ssh/sshd_config /etc/ssh/sshd_config service ssh restart [[ $? -eq 0 ]] && success SSH远程密钥连接 配置成功 || failde SSH远程密钥连接 配置失败 [[ -d "/root/.ssh" ]] || mkdir /root/.ssh cp -f ssh/authorized_keys /root/.ssh/authorized_keys else echo "确少ssh 配置文件" fi #fi #配置salt install_deb salt-minion salt-minion cp -rf minion /etc/salt/ rm -rf /etc/salt/minion_id rm -rf /etc/salt/pki /etc/init.d/salt-minion restart >/dev/null 2>&1 systemctl enable salt-minion > /dev/null 2>&1 [[ $? -eq 0 ]] && success Salt-Minion客户端服务开机启动 配置成功 || failde Salt-Minion客户端服务开机启动 配置失败 #配置开机启动 cp -f rc.local /etc/rc.local #配置vim cp -pf vimrc.local /etc/vim/ #配置usb禁用 chown root:myadmin /media chmod 750 /media chmod 750 /bin/mount #配置触摸板软件 install_deb gpointing-device touchpad #安装 RapidSVN install_deb rapidsvn svn #安装 gimp install_deb gimp gimp #配置共享 if [[ $1 == "wx" ]];then mount_share zx_wx elif [[ $1 == "wz" ]];then mount_share zx_wz elif [[ $1 == "all" ]];then mount_share zx_wx mount_share zx_wz fi #安装colorprint #colorprint"依赖太多,系统自带的截图工具可以解决大部分图形处理等功能