Linux Shell编程自动化运维实现——Shell循环
发布日期:2021-05-10 08:19:17 浏览次数:17 分类:精选文章

本文共 4535 字,大约阅读时间需要 15 分钟。

shell���������������������������

Shell������������������������������������������������������������������������������������������������IP���������������������������������������������������shell���������������������������������������������������

1. for������������������������������

shell������������for���������������������������������������������������������������������������������������������

������������

for ��������� in [������������] do
���������
done

������1���������������������

���������������������192.168.100.0/24������������������������������ping���������������������������

#!/bin/bash
# ������������������
> ip.txt
for ip in {2..254} do
if ping -c1 -W1 $ip > /dev/null; then
echo "$ip" >> ip.txt
fi
done

������2���������������������������

������������������for���������������������������������������root���������

#!/bin/bash
# ������������������
for ip in $(cat ip.txt) do
# ������IP������������
if [[ $(ping -c1 $ip >> /dev/null 2>&1) ]] then
# ������������������������������
ssh $ip "echo new_password | passwd root"
# ������������������
echo "$ip" >> action.log
else
echo "$ip ��jkildeqi"
fi
done

2. ���������������������-controls

���������������������������������������������������������������������������������������������������������������������������������

������3������������������������

���������������IP������������������IP������ping������������������������������������������������������

#!/bin/bash
# ������������������
> ip.txt
# ���������������������
max_THREADS=10
i=0
while getopts "t:" opt do
case $opt in
"t") max_THREADS=$OPTARG ;;
esac
done
for ip in {2..254} do
{ # ���������������������enetiy
echo "$ip" > ip.txt
ping -c1 -W1 $ip >> /dev/null
if [ $# -eq 0 ]; then
ssh $ip "echo new_password | passwd --stdin root"
fi
} & > /dev/null &&
while [ $i - le max_threads ]; do
sleep 1
i=$((i+1))
done
# ������������������������������
sleep 1
done

3. while���until������

���������������������������������������������������while���until���������������������������

while���������apped with���������

��������������������������������������������������������������� them modern���������������������������������������

#!/bin/bash
# live������������
while true do
echo -n "id: "
read id
echo -e "\n������������ID: $id"
sleep 2
done

until������

���������������������������������������������������������������������������������������

#!/bin/bash
# ������������������������
until [ -f /tmp/file ]; do
echo -n "���������������������"
sleep 1
done

4. Expect���������shell���������������������

Expect��������� powerful������������������������������������������������������������������������������������������������������������������������

Expect������������������������������������������

������������������expect���������������������������������������������

#!/usr/bin/expect
set timeout 30
spawn ssh root@192.168.0.111
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "strong_password\r" }
}
interact

###(statefulDNS������������) ������expect������DNS���������������������������������������������������DNS������������

#!/usr/bin/expect
set timeout 10
spawn dhcpclient eno1
expect {
"DHCP-client finishedParseException..." { exit 1 }
interact
}

5. ���������������������������������������������

������������������expect������������������������������������������������������������������

#!/bin/bash
> ip_list.txt
for i in $(seq 192.168.0.2 192.168.0.254) do
{ #������������
ping $i >> /dev/null
echo "$i ��jkildeqi" >> ip_list.txt
} &
done

������������������������

���������������������������������������������������������������

cat ip_list.txt | grep '��jkildeqi'

6. Expect������������DNS������������������

���������������expect���������������DNS���������������������������������������������

#!/usr/bin/expect
set result ""
set default_bootfile=node-default.boot
if [ "$1" != "" ] then {
set bootfile=$1
}
spawn systemctl restart-dnsmq
expect {
"Processing..." { sleep 30; set result=yes }
"dnsmq[992]:" { send "]]> $bootfile\r"; sleep 1 }
���
"Input" { send "\r"; sleep 2 }
"RESET" { exit }
}

���������������������������������shell���������������������������������������������������������������������������������������������������������Expect������������������������������������������������������������������������������������������������

上一篇:基于翻译的模型-TransE,TransH,TransR,TransD
下一篇:hive UDF里写了main方法,运行提示 错误: 找不到或无法加载主类

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年03月31日 00时14分23秒