需要目标机器安装有 expect 命令

分成五个文件config.ini(配置文件)、id_ras.pub(公钥)、read.sh(一个函数,用于读取配置文件)、test.sh(执行文件)、run.sh(远程运行脚本) 是一个配置文件.

步骤1:读取配置文件 ip,user,pwd

步骤2:将id_rsa.pub、run.sh scp到远程机器上

步骤3:远程运行脚本

read.sh

#!/bin/bash

GetKey(){
section=$(echo $ |cut -d '.' -f )
key=$(echo $ |cut -d '.' -f )
if [ "$section"x = "ip"x ];then
nub=`cat config.ini | grep -n "$key={" |awk -F ":" '{print $1}'`
nue=`cat config.ini | grep -n "}" |awk -F ":" '{print $1}'`
nub=`expr $nub +`
for i in $nue
do
if [ $i -gt $nub ]; then
a=$i
break
fi
done
nue=`expr $a -`
sed -n "$nub,$nue p" config |sed s/,//g
else
sed -n "/\[$section\]/,/\[.*\]/{
/^\[.*\]/d
/^[ \t]*$/d
/^$/d
/^#.*$/d
s/^[ \t]*$key[ \t]*=[ \t]*\(.*\)[ \t]*/\/p
}" config.ini
}

run.sh

#!/bin/bash

Create(){
/usr/bin/expect -c"
set timeout
spawn ssh-keygen -t rsa
expect \":\"
send \"\r\"
expect \":\"
send \"\r\"
expect \":\"
send \"\r\"
expect eof
exit
"
} if [ ! -d .ssh ];then
Create
if [ $? != ];then
echo "step for create failed"
exit
else
cat id_rsa.pub >> .ssh/authorized_keys
if [ $? != ];then
echo "step for cp failed"
exit
fi
fi
else
cat id_rsa.pub >> .ssh/authorized_keys
if [ $? != ];then
echo "step for cp failed"
exit
fi
fi

test.sh

#!/bin/bash
. ./read.sh ip=$(GetKey "ip.ip")
passwd=$(GetKey "pass.passwd")
user=$(GetKey "user.usr") for i in $ip
do
/usr/bin/expect -c"
set timeout10
spawn /usr/bin/scp -r run.sh $user@$i
expect \"password:\" {send \"$passwd\r\"}
spawn /usr/bin/scp -r id_rsa.pub $user@$i
expect \"password:\" {send \"$passwd\r\"}
expect eof
exit
"
/usr/bin/expect << EOF
set timeout
spawn ssh user@$i
expect "password:"
send "$passwd\r"
expect "]"
send "sh run.sh\r"
send "exit\r"
expect eof
EOF
done

配置文件

[ip]
ip={
12.23.31.114
,21.34.54.112
} [user]
usr=hehehe [pass]
passwd=wohehehda

最新文章

  1. Web API 入门指南 - 闲话安全
  2. [KMP]【学习笔记】
  3. JPG转TIFF
  4. 最新AFNetworking
  5. 史上最强大网盘,网络上的赚钱神器【Yunfile网盘】,注册就送8元
  6. mac 安装 php nginx mysql
  7. 先装.net后装iis的问题
  8. 【转】Bloom Filter布隆过滤器的概念和原理
  9. Uiautomator打包使用第三方库,报错的解决方案
  10. CAPI HTTP服务搭建(文件在本机)
  11. JSON字符串与JSON对象的区别及转换
  12. 适合初学者的一个分布式环境搭建过程(spring boot + zookeeper + dubbo + mybatis + mysql)
  13. Java Web 单表操作
  14. PHP Extension开发(Zephir版本)
  15. JS 多选文件或者选择文件夹
  16. One-hot encoding 独热编码
  17. HDU 1232 畅通工程 (并查集)
  18. java课件运行实践
  19. HDUOJ----More is better(并查集)
  20. JavaScript动态加载js文件

热门文章

  1. JAMon监控SQL执行时间
  2. java的poi技术读取Excel[2003-2007,2010]
  3. 深入理解JS函数作用域链与闭包问题
  4. avro序列化详细操作
  5. Spark(十一) -- Mllib API编程 线性回归、KMeans、协同过滤演示
  6. mongodb 踩坑记录
  7. SQL Server 高性能写入的一些经验总结
  8. 使用IntelliJ IDEA创建Maven多模块项目
  9. 【DP】【单调队列】【NOI2005】瑰丽华尔兹
  10. ssh之&lt;context:component-scan base-package=&quot;com.xx&quot; /&gt;