1.脚本代码
代码如下(示例):dhcp.sh
#!/bin/bash
yum list dhcp
yum -y install dhcp
rpm -qc dhcp
cat>> /etc/dhcp/dhcpd.conf <<EOF
option domain-name "example.org";
option domain-name-servers 192.168.11.2, 223.5.5.5;
default-lease-time 21600;
max-lease-time 43200;
ddns-update-style none;
log-facility local7;
subnet 192.168.11.0 netmask 255.255.255.0 {
range 192.168.11.123 192.168.11.199;
option routers 192.168.11.2;
}
EOF
systemctl enable --now dhcpd
systemctl start dhcpd
systemctl status dhcpd
touch hosts.txt
if ! rpm -q expect > /dev/null
then
echo "###expect 未安装,现在安装###"
yum install -y expect &>/dev/null ###安装expect###
if [ $? -ne 0 ]
then
echo "###expect 安装失败###"
exit 1
fi
fi
if [ ! -f ~/.ssh/id_rsa ]
then
echo "###请按3次enter键###"
ssh-keygen -t rsa
fi
passwd="123123"
ssh_expect () {
expect -c "set timeout -1;
spawn ssh-copy-id -f $1
expect {
"yes/no" { send -- yes\r;exp_continue;}
"password:" { send -- $2\r;exp_continue;}
eof:
}";
}
for ip in `cat hosts.txt |awk '{print $1}'`
do
ssh_expect $ip $passwd
done
ssh root:123123@192.168.11.12 <<eeoff
rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0
cat >> /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE=eth0
NAME=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp
#PADDR=192.168.11.12
#ATEWAY=192.168.11.2
#ETMASK=255.255.255.0
#NS1=223.5.5.5
#NS2=192.168.11.2
EOF
systemctl restart network
ip a
eeoff
echo done!