[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens160
[root@localhost network-scripts]# vim ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=static
NAME=ens160
UUID=bb246651-8995-4594-a461-47a9675bcde5
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.200.110
NETMASK=255.255.255.0
GATEWAY=192.168.200.2
DNS1=202.106.0.20
[root@localhost network-scripts]# ifdown ens160 && ifup ens160
成功停用连接 "ens160"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/1)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@localhost network-scripts]# ping 192.168.200.2
PING 192.168.200.2 (192.168.200.2) 56(84) bytes of data.
64 bytes from 192.168.200.2: icmp_seq=1 ttl=128 time=0.953 ms
64 bytes from 192.168.200.2: icmp_seq=2 ttl=128 time=0.205 ms
--- 192.168.200.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 53ms
rtt min/avg/max/mdev = 0.205/0.579/0.953/0.374 ms
二、 脚本创建多个用户并添加到组
[root@localhost ~]# vim yonghu.sh
#!/bin/bash
groupadd zx21-2
for name in zx21{1..26}
do
# userdel -r $name
# groupdel zx21-2 &>/dev/null
useradd -g zx21-2 $name
echo "123456" | passwd --stdin $name &> /dev/null
done
[root@localhost ~]# sh yonghu.sh
[root@localhost ~]# tail -10 /etc/passwd
zx2117:x:1017:1001::/home/zx2117:/bin/bash
zx2118:x:1018:1001::/home/zx2118:/bin/bash
zx2119:x:1019:1001::/home/zx2119:/bin/bash
zx2120:x:1020:1001::/home/zx2120:/bin/bash
zx2121:x:1021:1001::/home/zx2121:/bin/bash
zx2122:x:1022:1001::/home/zx2122:/bin/bash
zx2123:x:1023:1001::/home/zx2123:/bin/bash
zx2124:x:1024:1001::/home/zx2124:/bin/bash
zx2125:x:1025:1001::/home/zx2125:/bin/bash
zx2126:x:1026:1001::/home/zx2126:/bin/bash
[root@localhost ~]# vim yonghu.sh
#!/bin/bash
#groupadd zx21-2
for name in zx21{1..26}
do
userdel -r $name
groupdel zx21-2 &>/dev/null
# useradd -g zx21-2 $name
echo "123456" | passwd --stdin $name &> /dev/null
done
[root@localhost ~]# sh yonghu.sh
三、 脚本检测网段存活率
[root@localhost ~]# vim ping.sh
#!/bin/bash
for i in {1..254};do
ping -c 1 -w 1 -i 0.3 192.168.200.$i &>/dev/null
if [ $? -eq 0 ];then
echo -e "192.168.200.$i 主机可达"
else
echo -e "192.168.200.$i 主机不可达"
fi
done
~
[root@localhost ~]# sh ping.sh
192.168.200.1 主机不可达
192.168.200.2 主机可达
192.168.200.3 主机不可达
四、 while语句不定循环语句
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TIMES=0
echo "商品价格范围0-999,猜猜看是多少?"
while true
do
read -p "请输入你猜测的价格数目" INT
let TIMES++
if [ $INT -eq $PRICE ];then
echo "恭喜你的价格才对了,实际价格是$PRICE"
echo "你总共猜测了$TIMES次"
exit 0
elif [ $INT -gt $PRICE ];then
echo "太高了!"
else
echo "太低了!"
fi
done
五、 检查端口22是否开启
[root@localhost ~]# vim net.sh
#!/bin/bash
netstat -lnpt | grep 22 &>/dev/null
if [ $? -eq 0 ];then
echo "端口已开启"
else
echo "端口未开启"
fi
[root@localhost ~]# sh net.sh
端口已开启
六、 创建本地yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# mkdir back
[root@localhost yum.repos.d]# mv *.* back
[root@localhost yum.repos.d]# vim centos.repo
[base]
name=base
baseurl=file:///media/BaseOS
enabled=1
gpgcheck=0
[APP]
name=app
baseurl=file:///media/AppStream
enabled=1
gpgcheck=0
[root@localhost yum.repos.d]# mount /dev/sr0 /media/
mount: /media: WARNING: device write-protected, mounted read-only.
[root@localhost yum.repos.d]# yum clean all
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
0 文件已删除
[root@localhost yum.repos.d]# yum makecache
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base 36 MB/s | 2.2 MB 00:00
app 28 MB/s | 5.6 MB 00:00
元数据缓存已建立。
[root@localhost yum.repos.d]# yum -y install lrzsz
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:00:28 前,执行于 2022年09月29日 星期四 09时41分12秒。
依赖关系解决。
===========================================================================================================
软件包 架构 版本 仓库 大小
===========================================================================================================
安装:
lrzsz x86_64 0.12.20-43.el8 base 84 k
事务概要
===========================================================================================================
安装 1 软件包
总计:84 k
安装大小:187 k
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : lrzsz-0.12.20-43.el8.x86_64 1/1
运行脚本: lrzsz-0.12.20-43.el8.x86_64 1/1
验证 : lrzsz-0.12.20-43.el8.x86_64 1/1
Installed products updated.
已安装:
lrzsz-0.12.20-43.el8.x86_64
完毕!
七、 脚本创建yum源并验证
[root@localhost ~]# vim yum.sh
#!/bin/bash
#yum安装
mount /dev/cdrom /media
#创建yum文件
cd /etc/yum.repos.d/
mkdir backup
mv *.repo backup
echo "[local]
name=Centos
baseurl=file:///media/BaseOS
enable=1
gpgcheck=0
[app]
name=app
baseurl=file:///media/AppStream
enabled=1
gpgcheck=0" >cent.repo
#清楚并重建缓存
yum clean all && yum makecache
[root@localhost ~]# sh yum.sh
mount: /media: /dev/sr0 already mounted on /media.
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
12 文件已删除
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Centos 282 MB/s | 2.2 MB 00:00
app 324 MB/s | 5.6 MB 00:00
元数据缓存已建立。