I am a Linux beginner, in order to better learn Linux, I have tried to write some scripts, only for learning, and hobbies.
English is not good, in order to run on the terminal, so reluctantly wrote a few words.
This script is the first script to implement automatic installation of squid,iptables and implement Internet behavior management. Follow up and write out the rules of iptables.
If there are errors, or better implementation methods, please discuss together, study.
Script
#!/bin/bash
#This Script Auto Configure IP address, hostanem, local yum,
#and change firewall from FIREWALLD to Iptables,
#and Install squid Proxy.
# This script by Charhai
# Mail:[email Protected]
# 2016-12-01
Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Shlog=/tmp/auto_install.log
Echo_line () {
Echo-e "\033[35m--------------------------------\033[0m"
}
#configure IP addresses.
Ifcfgdir=/etc/sysconfig/network-scripts
#nethw = ' IP link Show | awk '/broadcast/{print ' | Cut-d ":"-F1 | awk ' {print nr,$0} '
NMCLI Dev Status | awk '/ethernet/{print $ ' | awk ' {print nr,$1} ' >/tmp/nmclifile
Net_config () {
Read-p "Input LAN ' s IP address. exap:192.169.1.1! : "Lan_ip
Read-p "Input netmask. Exap:24: "Lan_mask
Read-p "Input LAN ' s gateway IP addrss. : "LAN_GW
Read-p "Input LAN dns1 IP address. : "Lan_dns1
Read-p "Input LAN dns2 IP address. : "Lan_dns2
NMCLI con Add type Ethernet con-name LAN ifname ${lan} IP4 ${lan_ip}/${lan_mask}
# nmcli con Add type Ethernet con-name LAN ifname ${lan} IP4 ${lan_ip}/${lan_mask} gw4 ${LAN_GW}
NMCLI con mod lan Ipv4.dns "${lan_dns1} ${lan_dns2}"
RM-RF ${ifcfgdir}/ifcfg-${lan}
Nmcli con up LAN
ECHO-E "input subnets. EXAP: \033[35m192.168.0.0/22,192.168.9.0/24\033[0m "
Read-p ' >>> ' GWS
Langws= ' echo ${gws} | awk ' begin{rs= ', '} {print '} '
For i in ${LANGWS}; Do
echo "${i} via ${LAN_GW} dev ${lan}" >> ${ifcfgdir}/route-lan
Done
if [!-Z $wan]; Then
Read-p "Input WAN s IP address. EXAP:182.10.10.10/24: "Wan_ip
Read-p "Input netmask. Exap:24! : "Wan_mask
Read-p "Input WAN Gateway ' IP address.:" WAN_GW
Read-p "Input WAN dns1 IP address.:" Wan_dns1
Read-p "Input WAN dns2 IP address.:" Wan_dns2
RM-RF ${ifcfgdir}/ifcfg-${wan}
NMCLI con Add type Ethernet con-name WAN ifname ${wan}} ip4 ${wan_ip}/${wan_mask} gw4 ${WAN_GW}
Nmcli Con MoD wan ipv4.dns "${wan_dns1} ${wan_dns2}"
Nmcli con up Wan
Fi
}
Echo_line
Cat/tmp/nmclifile
echo_line
read-p "Choice LAN network card ' s name,press any key scripts is Exit. : "Net_choice
Case ${net_choice} in
1)
lan= ' Cat/tmp/nmclifile | grep 1 | awk ' {print $} '
wan= ' cat/tmp/nmclifile | grep 2 | awk ' {print $} '
net_config
;;
2)
lan= ' echo ${nethw} | grep 2 | awk ' {print $} '
wan= ' echo ${nethw} | grep 1 | awk ' {print $ 2} '
net_config
;;
*)
echo "Scripts is exit!"
exit 1
;;
Esac
Rm-rf/tmp/nmclifile
#configure hostname.
Echo_line
Read-p "Change hostname?,y or N" choice_name
Case $choice _name in
Y
Read-p "input hostname, exap:squid.xinyiglass.dy!:" Host_name
Hostnamectl Set-hostname ${host_name}
Only_name= ' echo ${host_name} | Cut-d "."-f1 '
CP/ETC/HOSTS/ETC/HOSTS.BK && sed-i ' 3, $d '/etc/hosts
echo "${lan_ip} ${only_name} ${host_name}" >>/etc/hosts
;;
N
echo "Use default hostname, ' hostname '!"
;;
Esac
#restart Network.service.
Echo_line
Systemctl Restart Network.service
#configure Yum repos.
Lcyum () {
Yum_dir=/etc/yum.repos.d
mkdir ${YUM_DIR}.BK
Mkdir/media/cdrom
Mount/dev/cdrom/media/cdrom
Find ${yum_dir}-name *.repo-exec mv {} ${YUM_DIR}.BK \;
}
#create yum repos files.
Yum_config () {
Yum_dir=/etc/yum.repos.d
Cat > ${yum_dir}/centos-media.repo << EOF
[C7-media]
name=centos-$releasever-media
baseurl=file:///media/cdrom/
Gpgcheck=1
Enabled=1
Gpgkey=file:///media/cdrom/rpm-gpg-key-centos-7
Eof
Sleep 1
If [-f/media/cdrom/rpm-gpg-key-centos-7]; then
echo "cdrom is mounted."
echo "Use local yum repos."
yum Clean all &>/dev/null
alias yum= ' yum--disablerepo=\*--enablerepo= C7-media '
yum makecache &>/dev/null
else
echo "CDROM is not mounted, Use the default Yum repos.
rm-rf ${yum_dir}/*
cp-a ${yum_dir}.bk/* $yum _dir}
yum Clean all & >/dev/null
yum makcache &>/dev/null
Fi
}
Echo_line
echo "Input y use local yum Repos. "
echo "Input n use Internet yum repos."
echo "Press any key exit scripts."
Echo_line
read-p "Choice y or N." Cdr
Case ${CDR} in
Y
Lcyum
Yum_config
;;
Y
Lcyum
Yum_config
;;
N
echo "Use Internet yum repos."
Yum Clean all &>/dev/null
;;
*)
echo "Scripts is exited"
Exit 1
;;
Esac
Echo_line
echo ""
#change Firewall from Firewalld to iptables.
Echo_line
echo "Change firewall from FIREWALLD to Iptables"
Echo_line
Systemctl Stop Firewalld.service
Systemctl Disable Firewalld.service &>/dev/null
Yum Install Iptables-services-y
Systemctl Enable Iptables &>/dev/null
Systemctl Restarte iptables
Iptables-a input-d ${lan_ip}-p TCP--dport=22-j ACCEPT
#install squid proxy software.
Echo_line
echo "Install squid"
Yum Install Squid-y
This article is from the "Persistent" blog, please be sure to keep this source http://charhai.blog.51cto.com/440887/1878945
Automatic installation of Squid+iptables Internet agent and Internet Behavior Management script (i).