下月有一个系统上线23台主机,虚拟机模板不带应用用户,所以只能手动来一个一个建立,于是想到了写一个小脚本解脱重复劳动的痛苦。
整个脚本分为脚本和配置文件:
配置文件如下:
1
2
3
4
5
|
username=root
password=1q2w3e
ip1=192.168.10.129
ip2=192.168.10.130
ip3=192.168.10.128
|
脚本文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
UNAME=`
cat
.
/sysinfo
.conf|
grep
username|
awk
-F
'='
'{print $2}'
`
PASSWD=`
cat
.
/sysinfo
.conf|
grep
password|
awk
-F
'='
'{print $2}'
`
cat
.
/sysinfo
.conf|
grep
ip|
awk
-F
'='
'{print $2}'
|
while
read
IP
do
ping
-c 2 $IP >
/dev/null
2>&1
conn_check=$?
if
[ $conn_check -
eq
0 ];
then
expect << EOF
spawn
ssh
$UNAME@$IP
expect
"*password:"
{ send
"$PASSWD\r"
}
expect
"*]#"
{ send
"lvcreate -L 500M -n lv_test system\r"
}
expect
"*]#"
{ send
"mkdir /test\r"
}
expect
"*]#"
{ send
"mkfs.ext4 /dev/system/lv_test\r"
}
expect
"*]#"
{ send
"mount /dev/system/lv_test /test\r"
}
expect
"*]#"
{ send
"exit\r"
}
EOF
elif
[ -s .
/error
.log ];
then
echo
"`date` The hostmachine $IP is not connected!"
>>.
/error
.log
else
echo
"`date` The hostmachine $IP is not connected!"
>.
/error
.log
fi
done
|
备注:里面的IP为案例IP并非我主机实际IP
本文转自 yuri_cto 51CTO博客,原文链接:http://blog.51cto.com/laobaiv1/1978665,如需转载请自行联系原作者