背景:由于公司服务器比较多,放长假的时候要全部关机,一台一台关机很费事,所以决定写个脚本控制,于是就想到了expect。
以下代码经验证完全达到了预期。
#!/usr/bin/expect -f
spawn ssh dsxadmin@192.168.1.17
expect "*pass*"
send "123456\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "123456\n"
expect eof
spawn ssh server10@192.168.1.20
expect "*pass*"
send "123456\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "123456\n"
expect eof
spawn ssh dsxadmin@192.168.1.223
expect "*pass*"
send "123456\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "123456\n"
expect eof
spawn ssh server09@192.168.1.19
expect "*pass*"
send "123456\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "123456\n"
expect eof
spawn ssh server05@192.168.1.16
expect "*pass*"
send "123456\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "123456\n"
expect eof
spawn ssh server08@192.168.1.22
expect "*pass*"
send "123456\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "123456\n"
expect eof
spawn ssh server11@192.168.1.21
expect "*pass*"
send "123456\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "123456\n"
expect eof
spawn ssh server12@192.168.1.23
expect "*pass*"
send "123456\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "123456\n"
expect eof
spawn ssh dsxadmin@192.168.1.98
expect "*pass*"
send "admin123\n"
expect "*:~*"
send "sudo poweroff\n"
expect "*pass*"
send "admin123\n"
expect eof
有了这个脚本之后,再多的服务器也不在话下!