1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
1.显示用户个数
[root@bogon ~]# grep -v /sbin/nologin /etc/passwd | cut -d: -f1 | wc -l
6
2.显示用户
[root@bogon ~]# grep -v /sbin/nologin /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
bahaoranc7
mageia
2、查出用户UID最大值的用户名、UID及shell类型
[root@bogon ~]# sort -nr -t : -k 3 /etc/passwd | head -1 | cut -d: -f1,3,7
slackware:2002:/sbin/nologin
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
查看连接数
[root@bogon data]# ss -nt | grep -E -c '([0-9]{0,3}\.){3}[0-9]{3}'
2
ESTAB 0 0 10.0.0.101:22 10.0.0.1:1903
ESTAB 0 52 10.0.0.101:22 10.0.0.1:1882
从大到小排序
[root@bogon data]# ss -nt | grep -E -c '([0-9]{0,3}\.){3}[0-9]{3}' | uniq -c | sort -nr
1 2
4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@bogon ~]# vim disk.sh
在插入模式中写入下列代码:
#!/bin/bash
df -h | tr -s ' ' % | cut -d % -f 5 | grep -E -o '[0-9]{0,3}' | sort -nr | head -1
保存退出
[root@bogon ~]# chmod -x disk.sh
[root@bogon ~]# bash disk.sh
15
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
vim systeminfo.sh
#! /bin/hostname
#\033[1;32m和\033[0m为改变中间字符的颜色;为了更好观看,把命令结果放入了下一行
echo -e "\033[1;32m*hostname\033[0m :
$HOSTNAME"
echo -e "\033[1;32m*IPv4 adddress\033[0m :
`ifconfig ens33 | grep -Eo '([0-9]{1,3}.){3}[0-9]' | head -1`"
echo -e "\033[1;32m*OS verson\033[0m :
`cat /etc/os-release | head -2`"
echo -e "\033[1;32m*core verson\033[0m :
`uname -r`"
echo -e "\033[1;32m*CPU model\033[0m :
`lscpu`"
echo -e "\033[1;32m*memory size\033[0m :
`free -g`"
echo -e "\033[1;32m*hard disk size\033[0m :
`lsblk`"
保存退出
加权限执行结果
chmod systeminfo.sh
[root@bogon data]# bash systeminfo.sh
*hostname :
bogon
*IPv4 adddress :
10.0.0.1
*OS verson :
NAME="CentOS Linux"
VERSION="7 (Core)"
*core verson :
3.10.0-1127.el7.x86_64
*CPU model :
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 2
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 158
Model name: Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz
Stepping: 9
CPU MHz: 2501.000
BogoMIPS: 5002.00
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 6144K
NUMA node0 CPU(s): 0-3
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec arat spec_ctrl intel_stibp flush_l1d arch_capabilities
*memory size :
total used free shared buff/cache available
Mem: 1 0 1 0 0 1
Swap: 3 0 3
*hard disk size :
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 154G 0 part
├─centos-root 253:0 0 100G 0 lvm /
├─centos-swap 253:1 0 4G 0 lvm [SWAP]
└─centos-data 253:2 0 50G 0 lvm /data
sr0 11:0 1 9.6G 0 rom