您现在的位置是:网站首页> 编程资料编程资料
批量修改ssh配置的expect脚本_linux shell_
2023-05-26
376人已围观
简介 批量修改ssh配置的expect脚本_linux shell_
第一思路,用iptables把外网ssh的包DROP掉;
第二思路,用tcpwrapper把sshd的allow写死;
第三思路,修改sshd_config,只监听内网请求。
由于一些说不清楚的原因,iptables的办法没法用;而tcpwrapper占用CPU资源较多;所以最后决定用第三种办法。
公司服务器比较多,而且根据随机登录查看的结果,sshd_config内容居然还太不一样~~手工干了一天,改了两组服务器后,终于下定决心要整个全自动脚本出来干活…… 目前的办法是这样的:
cat 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
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
相关内容
- 分享awk变量$0的妙用_linux shell_
- sed删除文件中的一行内容的脚本代码_linux shell_
- bash scp command not found的解决方法_linux shell_
- linux中mysql备份shell脚本代码_linux shell_
- linux下使用perl获取本机ip的几种方法介绍_linux shell_
- Linux下使用Shell脚本实现ftp的自动上传下载的代码小结_linux shell_
- 神奇的shell命令行输入与输出功能介绍_linux shell_
- 获取两个日期间隔时间的shell脚本代码_linux shell_
- linux shell中 if else以及大于、小于、等于逻辑表达式介绍_linux shell_
- 判断文件是否存在的shell脚本代码_linux shell_
