第一思路,用iptables把外網(wǎng)ssh的包DROP掉;
第二思路,用tcpwrapper把sshd的allow寫死;
第三思路,修改sshd_config,只監(jiān)聽內(nèi)網(wǎng)請求。
由于一些說不清楚的原因,iptables的辦法沒法用;而tcpwrapper占用CPU資源較多;所以最后決定用第三種辦法。
公司服務器比較多,而且根據(jù)隨機登錄查看的結果,sshd_config內(nèi)容居然還太不一樣~~手工干了一天,改了兩組服務器后,終于下定決心要整個全自動腳本出來干活…… 目前的辦法是這樣的:
cat ssh.exp
復制代碼 代碼如下:
#!/usr/bin/expect -f
log_file exp.log
set timeout -1
set ipaddr [lrange $argv 0 0]
for {set i 1} {$i4} {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
復制代碼 代碼如下:
#!/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