Linux 实用脚本

批量格式化磁盘,方便组建阵列

root@ubuntu:~# cat fdisk.sh 
#!/bin/bash
#date:2022/1/29
#aa=`lsblk | grep disk | grep 12.8T | awk -F " " '{print $1}'`
aa=`cat /root/bb.txt`
for i in $aa;
do
#echo $i 
parted -a optimal /dev/$i << EOF
rm 1
lgnore
rm 9
lgnore
quit
EOF
done

用于批量采集服务器硬件系统:CPU,内存,磁盘,disk-SN,IP 等等

cat ip.txt
192.168.1.1
192.168.1.2

# 这里的登录方式需要配置免密才方便。

for i in `cat ip.txt`;do ssh root@$i "ip add | grep 10.42 |awk '{print $2}' && hostnamectl |grep Operating |awk -F: '{print $2}' && lsblk  | grep disk | grep nvme && lscpu|grep Model |sed -n '2'p|sed 's/^.*(R)//g'|cut -b 2-50 && free -h | grep Mem | awk '{print $2}' && lshw -class disk|grep -A 6 "WDC"  | grep serial |wc -l  &&lshw -class disk|grep -A 6 "WDC" | grep serial |sed 's/^.*serial: //g'  && echo ############## "  >> 5.txt;done

批量ping 检查主机

# 2021/10/5  11:30

主机列表:
monitor@ubuntu:~/hlm-monitor$ cat ping_ip.txt 
10.44.8.6
10.44.8.52
10.44.8.55

# ping测试脚本:
monitor@ubuntu:~/hlm-monitor$ cat ping_test.sh 
#!/bin/bash
# filename: /home/dbatlbb/script/liweiwie/ping.sh
#ping -c 2 -W1 -w 0.1 10.43.5.72 
for ips in `cat /home/monitor/hlm-monitor/ping_ip.txt`
do
	result=`ping -w 2 -c 3 ${ips} | grep packet | awk -F" " '{print $6}'| awk -F"%" '{print $1}'| awk -F' ' '{print $1}'`
	if [ $result -eq 0 ]; then
        echo ""${ips}" is ok !"
        else
        echo ""${ips}" is not connected ....."
        fi

done

Windows 电脑批量ping

Windows 平台ping 测试

for /L %i in (100,1,110) do ping -n 1 -w 60 192.168.0.%i  | find "192" >> C:\Users\admin\Desktop\pinglog.txt


10.60.17.17
10.60.17.18

手工免密批量登录,传输文件或者脚本 (ansible,saltstack)

#服务端的机器需要提取安装 expect 软件,来模拟手工输入密码的操作
cat iplist
192.168.1.1
192.168.1.2

vim scp.sh
#!/bin/bash
passwd=123
for host in `cat ./iplist`
do
/usr/bin/expect << EOF
spawn scp ./commond.sh root@$host:/root/
expect {
"*yes/no" {send "yes\r";exp_continue}
"*password:" {send "$passwd\r"}
}
interact
expect eof
EOF
done

vim execfil.sh
#!/bin/bash
passwd=123
for host in `cat ./iplist`
do
/usr/bin/expect << EOF
spawn ssh root@$host "bash /root/commond.sh"
expect {
"*yes/no" {send "yes\r";exp_continue}
"*password:" {send "$passwd\r"}
}
interact
expect eof
EOF
done

你可能感兴趣的:(Linux,linux,bash,ubuntu)