第六周作业
1、编写脚本实现登陆远程主机编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
[root@localhost data]#cat auto.sh
#!/usr/bin/expect
set timeout 5
#设置超时时间
set host [lindex $argv 0]
#定义位置变量
set pd [lindex $argv 1]
#定义位置变量
spawn ssh ${host}
#启动命令ssh
expect {
"No route to host" exit
#错误退出
"Connection refused" exit
#错误退出
"(yes/no)" {send "yes\n";exp_continue}
#输入yes
"*password:" {send "${pd}\n"}
#输入密码
}
interact
[root@localhost data]#./auto.sh 192.168.91.101 123123
spawn ssh 192.168.91.101
root@192.168.91.101's password:
Last login: Mon Oct 11 12:49:34 2021 from 192.168.91.100
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.91.101 netmask 255.255.255.0 broadcast 192.168.91.255
inet6 fe80::eca:d785:4cb4:decc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4a:d7:ad txqueuelen 1000 (Ethernet)
RX packets 1134 bytes 109346 (106.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 489 bytes 72988 (71.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions
2、生成10个随机数保存于数组中,并找出其最大值和最小值
#!/bin/bash
read -p "请输入正整数字,并已空格隔开:" num
a=( $num )
max=${a[0]}
for ((i=0;i<${#a[*]};i++))
do
if [[ $max -lt ${a[$i+1]} ]]
then
max=${a[$i+1]}
fi
done
echo "最大值是$max"
min=${a[0]}
for ((i=0;i<${#a[*]}-1;i++))
do
if [[ $min -ge ${a[$i+1]} ]]
then
min=${a[$i+1]}
fi
done
echo "最小值是$min"
3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
vim paixu.sh
#!/bin/bash
read -p "请输入正整数字,并已空格隔开:" num
a=( $num )
for ((i=1;i<${#a[*]};i++))
do
for ((j=0;j<${#a[*]}-i;j++))
do
if [ ${a[$j]} -gt ${a[`expr $j + 1`]} ]
then
tmp=${a[`expr $j + 1`]}
a[`expr $j + 1`]=${a[$j]}
a[$j]=$tmp
fi
done
done
echo ${a[@]}
4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)
CPU:
[root@localhost data]#top
[root@localhost data]#top -b -n 1 | grep Cpu | awk -F ',' '{print $4}' | awk '{print $1*10}'
968
内存:
[root@localhost data]#free -mh |awk /Mem/'{print $3}'
#已使用内存
309M
[root@localhost data]#free -mh |awk /Mem/'{print $2}'
#总大小
1.8G
硬盘:
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 5G 0 part /boot
└─sda2 8:2 0 15G 0 part
├─centos-root 253:0 0 10G 0 lvm /
└─centos-swap 253:1 0 5G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 10G 0 part
sr0 11:0 1 4.2G 0 rom
[root@localhost ~]# lsblk|grep disk
#过滤出所有硬盘(disk)
sda 8:0 0 20G 0 disk
sdb 8:16 0 20G 0 disk
sdc 8:32 0 20G 0 disk
[root@localhost ~]#lsblk |grep disk|awk -F "[ G]+" '{print $4}'
20
20
20
[root@localhost ~]# sum=0;for i in $total ;do sum=$[sum+i];done;echo $sum
#求出磁盘总和
60
[root@localhost ~]#cat disk.sh
#!/bin/bash
sum=0
total=`lsblk |grep disk|awk -F "[ G]+" '{print $4}'`
for i in $total
do
let sum=$[$sum+$i]
done
echo $sum
~
i/o
[root@localhost data]#iostat -kx | grep sda| awk '{print $4}'
5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
[root@localhost data]#bash for.sh
1连通性 success
2连通性 success
3连通性 fail
4连通性 fail
5连通性 fail
6连通性 fail
#!/bin/bash
for i in {1..254}
do
ping -c 3 192.168.91.$i &>/dev/null
if [ $? -eq 0 ]
then
echo "$i连通性 success"
else
echo "$i连通性 fail"
fi
done
while
#!/bin/bash
i=1
while [ $i -lt 255 ]
do
ping -c3 192.168.91.$i &>/dev/null
if [ $? -eq 0 ]
then
echo "$i连通性 success"
else
echo "$i连通性 fail"
fi
let i++
done
[root@localhost data]#bash w.sh
1连通性 success
2连通性 success
3连通性 fail
4连通性 fail
5连通性 fail
6连通性 fail
6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
[root@localhost data]#cat back.sh
#!/bin/bash
name=`date -d -1day +%F-%H`
[ -e /backup ]
if [ $? -eq 0 ]
then cp -a /etc/* /backup/etcbak$name &>/dev/null
else
mkdir /backup
cp -a /etc/* /backup/etcbak$name &>/dev/null
fi
[root@localhost data]#ln -s /data/back.sh /usr/local/bin/
[root@localhost data]#crontab -e
30 1 * * 1-5 /usr/local/bin/back.sh
[root@localhost data]#crontab -l
30 1 * * 1-5 /usr/local/bin/back.sh
[root@localhost data]#bash back.sh
[root@localhost data]#ls /backup/
etcbak2021-10-14-01
[root@localhost data]#ls /backup/etcbak2021-10-14-01/
abrt firewalld makedumpfile.conf.sample request-key.conf
adjtime flatpak man_db.conf request-key.d
aliases fonts maven resolv.conf
aliases.db fprintd.conf mime.types rpc
alsa fstab mke2fs.conf rpm
alternatives fuse.conf modprobe.d rsyncd.conf
anacrontab gconf modules-load.d rsyslog.conf
asound.conf gcrypt motd rsyslog.d
at.deny gdbinit mtab rwtab
audisp gdbinit.d mtools.conf rwtab.d
audit gdm multipath samba
autofs.conf geoclue my.cnf sane.d
autofs_ldap_auth.conf GeoIP.conf my.cnf.d sasl2
auto.master GeoIP.conf.default named scl
auto.master.d ghostscript named.conf securetty
auto.misc gnupg named.iscdlv.key security
auto.net GREP_COLORS named.rfc1912.zones selinux
auto.smb groff named.root.key services
avahi group nanorc sestatus.conf
bash_completion.d group- netconfig setroubleshoot
bashrc grub2.cfg NetworkManager setuptool.d
binfmt.d grub.d networks sgml
bluetooth gshadow nfs.conf shadow
brltty gshadow- nfsmount.conf shadow-
brltty.conf gss nsswitch.conf shells
centos-release gssproxy nsswitch.conf.bak skel
centos-release-upstream host.conf nsswitch.conf.rpmnew smartmontools
certmonger hostname ntp sos.conf
cgconfig.conf hosts ntp.conf speech-dispatcher
cgconfig.d hosts.allow numad.conf ssh
cgrules.conf hosts.deny oddjob ssl
cgsnapshot_blacklist.conf hp oddjobd.conf sssd
chkconfig.d httpd oddjobd.conf.d statetab
chrony.conf idmapd.conf openldap statetab.d
chrony.keys init.d opt subgid
cifs-utils inittab os-release subuid
cron.d inputrc PackageKit sudo.conf
cron.daily ipa pam.d sudoers
cron.deny iproute2 passwd sudoers.d
cron.hourly ipsec.conf passwd- sudo-ldap.conf
cron.monthly ipsec.d pbm2ppa.conf sysconfig
crontab ipsec.secrets pinforc sysctl.conf
cron.weekly iscsi pkcs11 sysctl.d
crypttab issue pki systemd
csh.cshrc issue.net plymouth system-release
csh.login java pm system-release-cpe
cups jvm pnm2ppa.conf target
cupshelpers jvm-commmon polkit-1 tcsd.conf
dbus-1 kdump.conf popt.d terminfo
dconf kernel postfix tmpfiles.d
default krb5.conf ppp trusted-key.key
depmod.d krb5.conf.d prelink.conf.d tuned
dhcp ksmtuned.conf printcap udev
DIR_COLORS ld.so.cache profile udisks2
DIR_COLORS.256color ld.so.conf profile.d updatedb.conf
DIR_COLORS.lightbgcolor ld.so.conf.d protocols UPower
dleyna-server-service.conf libaudit.conf pulse usb_modeswitch.conf
dnsmasq.conf libibverbs.d purple vconsole.conf
dnsmasq.d libnl python vimrc
dracut.conf libreport qemu-ga virc
dracut.conf.d libuser.conf qemu-kvm vmware-tools
drirc libvirt radvd.conf vsftpd
e2fsck.conf locale.conf rc0.d wgetrc
enscript.cfg localtime rc1.d wpa_supplicant
environment login.defs rc2.d wvdial.conf
ethertypes logrotate.conf rc3.d X11
exports logrotate.d rc4.d xdg
exports.d lsm rc5.d xinetd.d
favicon.png lvm rc6.d xml
fcoe machine-id rc.d yum
festival magic rc.local yum.conf
filesystems mailcap rdma yum.repos.d
firefox mail.rc redhat-release