ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 转:ssh调用expect使用以及shell同时传入两个参数调用

转:ssh调用expect使用以及shell同时传入两个参数调用

Linux操作系统 作者:luckyfriends 时间:2016-06-11 18:55:54 0 删除 编辑

在此之前遇到两个问题,一个是如何对shell同时进行两个变量的循环

http://bbs.chinaunix.net/thread-4096418-1-1.html

另一个是如何在shell中调用expect内容

http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4096482&page=1#pid23955323


在QQ群和论坛大神的协助下顺利解决这俩问题

最终结果为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
while read ip password
        do
                expect <<EOF
                set timeout 3
                #spawn ssh root@$ip
                spawn scp /root/.ssh/id_rsa.pub root@$ip:/root/.ssh/authorized_keys
                expect {
                        "*yes/no" {send "yes\r";exp_continue}
                        "*password:" {send "$password\r"}
                }
                expect eof
EOF
        done < ip.txt



另一个方法为QQ群里的大神提供

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
#2013-7-17
#version 1.0
autologin(){
expect -c "
set timeout -1;
#a=cat 1.txt
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $1
expect {
        "*yes/no" {send "yes"\r;exp_continue}
        "*assword*" {send $2\r}
}
interact;"
}
for ((i=1;i<=`cat 1.txt|wc -l`;i++))
do
  line=`sed -n "${i}p" 1.txt`
  IP=`echo $line|awk '{print $1}'`
  PASS=`echo $line|awk '{print $2}'`
  autologin $IP $PASS
done


运维参考资料

http://item.taobao.com/item.htm?spm=a1z10.5.w4002-1279326270.15.0tzUHO&id=18619178113


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14710393/viewspace-2118574/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
长期从事IT相关工作。

注册时间:2009-06-06

  • 博文量
    465
  • 访问量
    3307376