Bulk Modify SSH configuration expect script _linux shell

Source: Internet
Author: User
Tags ssh iptables

The first idea, with iptables the external network SSH packet drop off;
The second idea, with Tcpwrapper to sshd allow write death;
The third idea, revise sshd_config, only listens intranet request.

Due to some unclear reasons, the iptables approach is not available, and Tcpwrapper occupies more CPU resources, so finally decided to use a third approach.

Company server more, and according to the results of random login view, sshd_config content incredibly still too different ~ ~ Hand dry Day, changed the two groups of servers, finally determined to complete the full automatic script out to work ... The current approach is this:

Cat Ssh.exp

Copy Code code as follows:

#!/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 $} ' |awk-f: ' {print $} ' >> Sshd_configr '
Send "service sshd RESTARTR"
Send "Exitr"
Interact

Cat do.sh

Copy Code code as follows:

#!/bin/sh
For IP in ' cat ip.lst '
Todo
./ssh.exp $ip >/dev/null 2>&1
Done
Cat Exp.log | grep Host | awk ' {print $} ' |sort|uniq >> Errorip
echo "The following IP cannot be modified"; Cat Errorip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.