当前位置:网站首页>Local virtual machine initialization script
Local virtual machine initialization script
2022-07-27 11:33:00 【Wu Xuan (operation and maintenance is also about fun!)】
The latest period of time , Virtual machine initialization is required , So install some services as needed , Organize a script , Share with you ,
Some of the environment configurations may not be suitable , Manual modification is required
#!/bin/bash
# Suitable for local virtual machine environment
# Process detection . If the host has this service, the installation function will not be called
function detection ( ) {
systemctl status httpd >> $log
if [[ $? = 4 ]]
then
httpd_install
else
echo "http The service is already installed , skip "
fi
systemctl status salt-minion >> $log
if [[ $? = 4 ]]
then
salt-minion_install
else
echo "salt-minion The service is already installed , skip "
fi
python3 -V >> $log
if [[ $? != 0 ]]
then
python_install
else
echo "python3 Is already installed , skip "
fi
coscmd -v >> $log
if [[ $? != 255 ]]
then
coscmd_install
else
echo "coscmd Is already installed , skip "
fi
systemctl status mysqld >> $log
if [[ $? = 4 ]]
then
mysql_install
else
ehco "mysql Is already installed , skip "
fi
num=`ps -ef | grep zabbix | awk '{print $8}'| head -n 1 `
test="/etc/init.d/zabbix_agentd"
if [[ "$num"x != "$test"x ]]
then
zabbix_agent_install
else
echo "zabbix-agent Is already installed , skip "
fi
}
function base_install ( ) {
echo “ Configure firewall rules ”
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
systemctl start firewalld
systemctl enable firewalld
service="http tomcat https ssh zabbix saltstack"
port_list="80 8080 443 22 10051 10050 52526 52525"
echo "-----------------------------"
echo " Initialize the ports added by the firewall :"
for i in $port_list
do
echo " Add port :${i}"
firewall-cmd --zone=public --add-port=${i}/tcp --permanent
done
echo "-----------------------------"
systemctl restart firewalld
echo “ List of configured firewalls ”
firewall-cmd --list-all
useradd mrdTomcat
echo "root:[email protected]" >> ./passwd.txt
echo "mrdTomcat:1234567" >> ./passwd.txt
chpasswd < ./passwd.txt
rm -f ./passwd.txt
echo " Normal user created successfully "
echo "****************"
echo " Start synchronization time "
yum install -y ntpdate
ntpdate ntp.tencent.com
echo " Synchronize Alibaba cloud time , The current time is :"
date
yum -y install ntp
systemctl start ntpd
port=`ss -anltup |grep 123| wc -l`
if [[ $port > 0 ]]
then
echo "ntp Service installed successfully "
else
echo "ntp Service installation failed "
exit 1
fi
echo "*****************"
echo " Begin to build swap Partition "
dd if=/dev/zero of=/mnt/swapfile bs=1MB count=1024
mkswap /mnt/swapfile
swapon /mnt/swapfile
echo " Check the current memory status "
free -m
echo " Start auto mount "
echo "/mnt/swapfile swap swap defaults 0 0 " >> /etc/fstab
chown root:root /mnt/swapfile
chmod 0600 /mnt/swapfile
echo "*********************"
# echo " Start mounting disk , By default, the disk is mounted to home Sub district "
deivce="/dev/"
sdb_name_num=`lsblk -r --output NAME,MOUNTPOINT | awk -F \/ '/sd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }' |wc -l`
if [[ $sdb_name_num = 0 ]]
then
sdb_name="vdb"
DISK=$deivce$sdb_name
bli=`blkid |grep vdb1| wc -l `
if [[ $bli -gt 0 ]]
then
# Has been formatted
df_num=`df -h | grep "home" | wc -l `
if [[ $df_num = 0 ]]
then
echo " ${DISK}1 /home ext4 defaults 0 0" >> /etc/fstab
else
echo "/home Already occupied "
fi
else
mkfs.ext4 ${DISK}1
df_num=`df -h | grep "home" | wc -l `
if [[ $df_num = 0 ]]
then
echo " ${DISK}1 /home ext4 defaults 0 0" >> /etc/fstab
else
echo "/home Already occupied "
fi
fi
else # Find the disk
sdb_name=`lsblk -r --output NAME,MOUNTPOINT | awk -F \/ '/sd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }'`
bli=`blkid |grep $sdb_name_num| wc -l `
if [[ $bli -gt 0 ]]
then
# Has been formatted
df_num=`df -h | grep "home" | wc -l `
if [[ $df_num = 0 ]]
then
echo " ${DISK}1 /home ext4 defaults 0 0" >> /etc/fstab
else
echo "/home Already occupied "
fi
else
/usr/bin/expect -c"
set timeout -1
spawn /sbin/fdisk $DISK
expect \"*m for help*:\"
send -- \"n\r\"
expect \"*p*\n\"
send -- \"p\r\"
expect \"Partition number (1-4,*\"
send -- \"\r\"
expect \"*default*:\"
send -- \"\r\"
expect \"*default*:\"
send -- \"\r\"
expect \"*m for help*:\"
send -- \"w\r\"
expect eof
"
mkfs.ext4 ${DISK}1
df_num=`df -h | grep "home" | wc -l `
if [[ $df_num = 0 ]]
then
echo " ${DISK}1 /home ext4 defaults 0 0" >> /etc/fstab
else
echo "/home Already occupied "
fi
fi
fi
mount -a
echo "*********************"
# echo " Set the secret free mutual trust between hosts "
/usr/bin/expect -c"
set timeout -1
spawn /usr/bin/ssh-keygen
expect \"*/root/.ssh/id_rsa*\"
send -- \"\r\"
expect \"*no passphrase*\"
send -- \"\r\"
expect \"* same passphrase*\"
send -- \"\r\"
## To change
spawn ssh-copy-id 192.168.136.3
expect \"*yes/no*\"
send -- \"yes\r\"
expect \"* password*\"
send -- \"[email protected]\r\"
expect eof
"
echo "*******************"
ip_list='10.157.0.17 10.157.0.16 10.1.38.2'
for i in $ip_list
do
ip=`cat /etc/hosts.allow | grep $i | wc -l`
if [[ $ip = 0 ]]
then
echo "sshd:${i}" >> /etc/hosts.allow
fi
done
echo "sshd:ALL" >> /etc/hosts.deny # Prohibit all logins
systemctl restart sshd
}
# install httpd service
function httpd_install ( ) {
echo " Start installation http"
yum install httpd -y
yum -y install php php-gd gcc php-mysql php-xml php-devel
echo "DirectoryIndex index.html index.php" >> /etc/httpd/conf/httpd.conf
echo "AddType application/x-httpd-php .php .php3 .php4 .php5" >> /etc/httpd/conf/httpd.conf
echo "date.timezone=Asia/Shanghai" >> /etc/php.ini
systemctl start httpd >> $log
systemctl enable httpd
ac=`ss -anltp | grep httpd | wc -l`
if [[ $ac > 0 ]]
then
echo "http Service started successfully !"
else
echo "http Boot failure ! Please check in time "
exit
fi
}
# install satl-minion
function salt-minion_install ( ) {
cd /etc/yum.repos.d
cat >salt.repo <<EOF
[salt-latest-repo]
name=Salt repo for RHEL/CentOS 7 PY2
baseurl=https://archive.repo.saltproject.io/yum/redhat/7/x86_64/archive/3000.5
skip_if_unavailable=True
failovermethod=priority
enabled=1
enabled_metadata=1
gpgcheck=0
gpgkey=https://archive.repo.saltproject.io/yum/redhat/7/x86_64/archive/30005/SALTSTACK-GPG-KEY.pub, https://archive.repo.saltproject.io/yum/redhat/7/x86_64/archive/3000.5/base/RPM-GPG-KEY-CentOS-7
EOF
yum clean all
echo " Start installation salt-minion"
yum install -y salt-minion
echo " modify minion To configure :"
### To change
# ip=49.233.21.192
while true
do
ip="192.168.136.3"
test=`telnet $ip 52526 | grep 'Escape character is'`
if [[ $? = 0 ]]
then
echo " Host can be connected , Start configuration "
break
else
echo " The host is not reachable , Please re-enter or troubleshoot the problem !"
exit 1
fi
done
path="/etc/salt/minion"
echo "master: $ip" >> $path
echo "master_port: 52526" >> $path
echo "user: root" >> $path
ip_add=`ip a | grep brd | tail -n 1| awk '{print $2}'| awk -F '/' '{print $1}'`
echo "id: $ip_add" >> $path
echo "log_file: /var/log/salt/minion" >> $path
systemctl start salt-minion
systemctl status salt-minion
if [[ $? = 0 ]]
then
echo "salt-minion Successful launch , Start testing connectivity "
# salt-call test.ping
# # if [[ $? = 0 ]]
# # then
# # echo " The connection test passed "
# # else
# # echo " If the test connection fails, please check it in time !"
# # exit 1
# # fi
else
echo "salt-minion Boot failure , Please check in time "
exit 1
fi
}
# install python and coscmd
function python_install( ) {
echo " Start installation python: by python3.6"
yum install -y python3
python3 -V
if [[ $? -eq 0 ]]
then
echo "python Installation successful ! Start installation coscmd"
else
echo "python Installation failed , Please check the problem in time !"
exit 1
fi
}
# install coscmd
function coscmd_install ( ) {
pip3 install coscmd
pip3 install coscmd -U
coscmd=`coscmd -v | wc -l`
if [[ $coscmd > 0 ]]
then
echo "coscmd Installation successful "
else
echo "coscmd Installation failed "
exit 1
fi
}
function mysql_install ( ) {
dir="/usr/local/mysql5"
mkdir -p $dir
echo " Start installation muysql5"
cd $dir
pwd
# Pull data response yum Source
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-server -y
systemctl start mysqld.service
systemctl enable mysqld.service
mysql=`ss -anltp | grep mysqld`
if [[ $mysql > 0 ]]
then
echo "mysql Successful launch !"
pass=` grep "password" /var/log/mysqld.log | head -n 1| awk '{print $NF}'`
u_pass="[email protected]"
echo " The password you set :$u_pass" >> ./install.log
mysqladmin -uroot -p$pass password $u_pass
mysql -uroot -p$u_pass -e "grant all privileges on *.* to 'root'@'%' identified by '$u_pass' with grant option;"
mysql -uroot -p$u_pass -e "flush privileges;"
else
echo "mysql Start exception ! Please check "
exit 1
fi
}
function zabbix_agent_install () {
# This script is only used to automate the installation zabbix-agent, And it is installed through the source package .
## This is the test package
pag="http://101.200.170.223/linux/zabbix_agent5/zabbix-5.0.15_x64_kernel2.6.tgz"
path="/usr/local/zabbix/conf/zabbix_agentd.conf"
# Standby automation installation and confirmation proxy_ip
echo " Start installing the toolkit "
yum install -y nc
echo " Start installation zabbix-agent"
mkdir /usr/local/zabbix
cd /usr/local/zabbix
wget $pag
tar xzvf zabbix-5.0.15*
ln -s ./bin/zabbix_sender ./bin/zabbix_get /usr/bin
ln -s ./sbin/zabbix_agent /usr/sbin/
echo " Start modifying the configuration file "
proxy_ip="192.168.136.3"
sed -i "s/Server=Proxy/Server=$proxy_ip/g" $path
sed -i "s/ServerActive=Proxy/ServerActive=$proxy_ip/g" $path
# Get to host ip
ip_add=`ip a | grep brd | tail -n 1| awk '{print $2}'| awk -F '/' '{print $1}'`
sed -i "s/Hostname=IP/Hostname=$ip_add/g" $path
cp $path /usr/local/etc
echo "zabbix_agent 10050/tcp" >> /etc/services
echo "zabbix_agent 10050/udp" >> /etc/services
cp /usr/local/zabbix/sbin/zabbix_agentd /etc/init.d
echo " Check the corresponding port "
systemctl status zabbix-agent
if [[ $? = 0 ]]
then
echo " stop it yum Installed zabbix-agent"
systemctl stop zabbix-agent
echo " Stop success "
else
num=`netstat -anltp | grep zabbix_agent|wc -l `
if [[ $num = 0 ]]
then
echo " The port is not occupied "
else
echo " Port occupied , Dealing with "
while true
do
pid=`netstat -anltp | grep zabbix |awk '{print $NF}'| awk -F '/' '{print $1}'`
kill -9 $pid
pid1=`netstat -anltp | grep zabbix |awk '{print $NF}'| awk -F '/' '{print $1}' | wc -l`
if [[ $pid1 = 0 ]]
then
echo " Stop success "
else
echo " Stopping "
fi
done
echo " Handle a successful !"
fi
fi
echo " start-up zabbix-agent"
/etc/init.d/zabbix_agentd
num2=`netstat -anltp | grep zabbix_agent|wc -l `
if [[ $num2 > 0 ]]
then
echo "zabbix-agent Successful launch !"
else
echo "zabbix-agent Boot failure , Please check in time !"
exit 1
fi
}
echo "*******************"
echo " Check network connectivity "
ping -c 2 www.baidu.com
if [[ $? -eq 0 ]]
then
log="chushihua.log" # Log collection file
num=`netstat -anltp | grep -v "ssh"| wc -l`
echo " Network connection successful ! Start installing basic tools "
if [ ! -f $log ]
then
date=`date`
echo " $date Initialize the host log document " >> $log
yum install -y vim
yum install -y wget
yum install -y telnet
yum install -y nc
yum install expect -y
base_install
# install httpd
detection
fi
else
echo " Network connection failed , Please check !"
exit 1
fi边栏推荐
- 最长上升子序列模型 AcWing 272. 最长公共上升子序列
- Inclusion exclusion principle acwing 890. divisible numbers
- 最长上升子序列模型 AcWing 1010. 拦截导弹
- 01 BTC cryptology principle
- 背包模型 AcWing 423. 采药
- 背包模型 AcWing 1024. 装箱问题
- The C programming language (2nd) -- Notes -- 1.8
- 最长上升子序列模型 AcWing 1016. 最大上升子序列和
- 深析C语言的灵魂 -- 指针
- Digital triangle model acwing 275. pass note
猜你喜欢

Digital triangle model acwing 1015. Picking flowers

properties文件

【Unity入门计划】CreatorKitFPS:第一人称射击3D小游戏

Solutions to errors in tensorflow operation

博弈论 AcWing 894. 拆分-Nim游戏

C# 自定义集合

Digital triangle model acwing 1018. Minimum toll

深析C语言的灵魂 -- 指针

Analysis of distributed database and cache double write consistency scheme (Reprint)

State compression DP acwing 91. shortest Hamilton path
随机推荐
Quantitative industry knowledge summary
01 BTC cryptology principle
A deep analysis of the soul of C language -- pointer
洛谷P1441 砝码称重
第8章 多线程
博弈论 AcWing 892. 台阶-Nim游戏
数字三角形模型 AcWing 1018. 最低通行费
Chinese remainder theorem acwing 204. strange way of expressing integers
Moveit2 - 5. Scenario Planning
properties文件
What is private traffic?
Today's code farmer girl summarized her notes about NPM package management and URL module
(9) Shell I / O redirection
Modelarts image classification and object detection
Moveit2 - 4. robot model and robot state
第10章 枚举类与注解
C programming language (2nd Edition) -- Reading Notes -- 1.5.4
Maker harmony OS application development training notes 01
The C programming language (2nd) -- Notes -- 1.9
Description and feelings