配置文件格式/data/shell/serverlist
jackin_128 192.168.237.128
jackin_131 192.168.237.131
批量修改主机名
OLDIFS=$IFS
IFS=$'n'
for line in $(cat /data/shell/serverlist);do
myhost=$(echo $line | awk '{print $1}')
myip=$(echo $line | awk '{print $2}')
echo "myhost is $myhost. myip is $myip"
ssh $myip "hostnamectl set-hostname $myhost"
ssh $myip "echo $myip $myhost >>/etc/hosts"
done
IFS=$OLDIFS
批量执行Shell命令
OLDIFS=$IFS
IFS=$'n'
for line in $(cat /data/shell/serverlist);do
myhost=$(echo $line | awk '{print $1}')
myip=$(echo $line | awk '{print $2}')
echo "myhost is $myhost. myip is $myip"
ssh $myip "uptime; ip addr show"
done
IFS=$OLDIFS
批量观察服务器日志
OLDIFS=$IFS
IFS=$'n'
for line in $(cat /data/shell/serverlist);do
myhost=$(echo $line | awk '{print $1}')
myip=$(echo $line | awk '{print $2}')
echo "myhost is $myhost. myip is $myip"
ssh $myip "timeout 1 tail -f /var/log/messages"
done
IFS=$OLDIFS
批量上传文件
OLDIFS=$IFS
IFS=$'n'
for line in $(cat /data/shell/serverlist);do
myhost=$(echo $line | awk '{print $1}')
myip=$(echo $line | awk '{print $2}')
echo "myhost is $myhost. myip is $myip"
scp /tmp/jackin.txt $myip:/usr/local/src
done
IFS=$OLDIFS
批量拉取文件
OLDIFS=$IFS
IFS=$'n'
for line in $(cat /data/shell/serverlist);do
myhost=$(echo $line | awk '{print $1}')
myip=$(echo $line | awk '{print $2}')
echo "myhost is $myhost. myip is $myip"
scp $myip:/usr/local/src/jackin.txt /tmp/jackin.txt.$myip
done
IFS=$OLDIFS



