批量修改ssh配置的expect腳本
更新時間:2013年03月02日 19:39:20 作者:
公司服務(wù)器一般通過ssh進行遠程管理。以前大家登錄的時候,都是隨意選內(nèi)外網(wǎng)IP進入。王總接手后,說這事隱患太大了,必須禁了外網(wǎng)ssh
第一思路,用iptables把外網(wǎng)ssh的包DROP掉;
第二思路,用tcpwrapper把sshd的allow寫死;
第三思路,修改sshd_config,只監(jiān)聽內(nèi)網(wǎng)請求。
由于一些說不清楚的原因,iptables的辦法沒法用;而tcpwrapper占用CPU資源較多;所以最后決定用第三種辦法。
公司服務(wù)器比較多,而且根據(jù)隨機登錄查看的結(jié)果,sshd_config內(nèi)容居然還太不一樣~~手工干了一天,改了兩組服務(wù)器后,終于下定決心要整個全自動腳本出來干活…… 目前的辦法是這樣的:
cat ssh.exp
復(fù)制代碼 代碼如下:
#!/usr/bin/expect -f
log_file exp.log
set timeout -1
set ipaddr [lrange $argv 0 0]
for {set i 1} {$i<4} {incr i} {
spawn ssh $ipaddr
expect {
"*password:" break
"to host" {sleep 2};
sleep 3
}
}
send "123456r"
expect "]#"
send "cd /etc/sshr"
send "cp sshd_config sshd_config.`date +%F-%T`.bakr"
send "sed -i /^ListenAddress.*$/d sshd_configr"
send "echo ListenAddress `/sbin/ifconfig eth0|awk '/inet /{print $2}'|awk -F: '{print $2}'` >> sshd_configr"
send "service sshd restartr"
send "exitr"
interact
cat do.sh
復(fù)制代碼 代碼如下:
#!/bin/sh
for ip in `cat ip.lst`
do
./ssh.exp $ip > /dev/null 2>&1
done
cat exp.log | grep host | awk '{print $5}'|sort|uniq >> errorip
echo "以下IP無法修改";cat errorip
相關(guān)文章
shell腳本正則匹配文件中的Email并寫入到文件中代碼分享
有時我們會處理日志文件,或其他文本文件,并將里面含有的Email讀取出來,可以利用shell處理文件的方法來讀取2014-04-04

