jquery教程

推荐列表 站点导航

当前位置:首页 > jquery > jquery教程 >

批量修改ssh配置的expect脚本

来源:网络整理  作者:网友投稿  发布时间:2020-12-28 04:17
描述:公司服务器一般通过ssh进行远程管理。以前大家登录时,都是随意选内外网IP进入。现在要求必须禁了外网ss...

描述:
公司服务器一般通过ssh进行远程管理。以前大家登录时,都是随意选内外网IP进入。现在要求必须禁了外网ssh。
第一思路,用iptables把外网ssh的包DROP掉;
第二思路,用tcpwrapper把sshd的allow写死;
第三思路,修改sshd_config,只监听内网请求。

原因不明的没有用iptables;而tcpwrapper占用CPU资源较多;所以最后决定用第三种办法。

公司服务器比较多,而且根据随机登录查看的结果,sshd_config内容居然还太不一样~~手工干了一天,改了两组服务器后,终于下定决心要整个全自动脚本出来干活。
目前的脚本,ssh.exp:
 

复制代码 代码如下:

#!/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

shell文件: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

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jq/jc/10032.shtml

相关文章
最新文章
PHP识别相片是否是颠倒的 PHP识别相片是否是颠倒的

时间:2020-12-28

python编程有哪些ide python编程有哪些ide

时间:2020-12-28

python开发工程师是做什么 python开发工程师是做什么

时间:2020-12-28

php构造函数的作用 php构造函数的作用

时间:2020-12-28

php怎么跟数据库连接 php怎么跟数据库连接

时间:2020-12-28

php实现顺序线性表 php实现顺序线性表

时间:2020-12-28

Python多重继承中的菱形继 Python多重继承中的菱形继

时间:2020-12-28

php中break的作用 php中break的作用

时间:2020-12-28

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

批量修改ssh配置的expect脚本

2020-12-28 编辑:网友投稿

描述:
公司服务器一般通过ssh进行远程管理。以前大家登录时,都是随意选内外网IP进入。现在要求必须禁了外网ssh。
第一思路,用iptables把外网ssh的包DROP掉;
第二思路,用tcpwrapper把sshd的allow写死;
第三思路,修改sshd_config,只监听内网请求。

原因不明的没有用iptables;而tcpwrapper占用CPU资源较多;所以最后决定用第三种办法。

公司服务器比较多,而且根据随机登录查看的结果,sshd_config内容居然还太不一样~~手工干了一天,改了两组服务器后,终于下定决心要整个全自动脚本出来干活。
目前的脚本,ssh.exp:
 

复制代码 代码如下:

#!/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

shell文件: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

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jq/jc/10032.shtml

相关文章

风云图片

推荐阅读

返回jquery教程频道首页