ssh非交互式expect

 

rpm -qa expect

 yum install expect -y

检查是否安装,如果没有安装一下

 

非交互式生成密钥及实现批量管理

 


第一步所有机器创建用户跟密码

useradd daxian001

echo 12345|passwd --stdin daxian001


 

第二部生成密钥对

 ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa > /dev/null 2>&1

 

第三部分发密钥(通过脚本)

#!/usr/bin/expect

if { $argc != 2 } {
 send_user "usage: expect ssh_expect.exp file host\n"
 exit
}

set file [lindex $argv 0]
set host [lindex $argv 1]
set password "12345"
spawn ssh-copy-id -i $file -p 22 daxian001@$host
expect {
        "yes/no"    {send "yes\r";exp_continue}
        "*password" {send "$password\r"}
}

expect eof

exit -onexit {
  send_user "say good bye to you!\n"
}

#expect ssh_expect.exp ~/etc/hosts 192.168.70.125:~

 

测试分发

expect ssh_expect.exp ~/.ssh/id_dsa.pub 192.168.70.125

 

测试发送成功是否

ssh daxian001@192.168.70.125 /sbin/ifconfig  eth0

 

 添加分发脚本 fenfa.sh

#!/bin/sh
. /etc/init.d/functions

for ip in 125 126 127 128
do
 expect ssh_expect.exp ~/.ssh/id_dsa.pub 192.168.70.$ip >/dev/null 2>&1
 if [ $? -eq 0 ];then
    action "$ip" /bin/true
 else
    action "$ip" /bin/false
 fi
done

执行脚本
 sh fenfa.sh 

 

测试

ssh daxian001@192.168.70.127 /sbin/ifconfig eth0

 

 

一键安装telnet  实战


第一步创建用户

useradd daxian002
echo 12345|passwd --stdin daxian002

 

第二部授权sudo

echo "daxian002 ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers

 

第三部,创建分发密钥脚本(daxian002创建)

 

#!/usr/bin/expect

if { $argc != 2 } {
 send_user "usage: expect ssh_expect.exp file host\n"
 exit
}

set file [lindex $argv 0]
set host [lindex $argv 1]
set password "12345"
spawn ssh-copy-id -i $file -p 22 daxian002@$host
expect {
        "yes/no"    {send "yes\r";exp_continue}
        "*password" {send "$password\r"}
}

expect eof

exit -onexit {
  send_user "say good bye to you!\n"
}

 

第四部,创建分发脚本

#!/bin/sh
. /etc/init.d/functions

#create dsa
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa > /dev/null 2>&1
 if [ $? -eq 0 ];then
    action "create dsa $ip" /bin/true
 else
    action "create dsa $ip" /bin/false
    exit 1
 fi

#fen fa dsa
for ip in 125 126
do
 expect ssh_expect.exp ~/.ssh/id_dsa.pub 192.168.70.$ip >/dev/null 2>&1
 if [ $? -eq 0 ];then
    action "$ip" /bin/true
 else
    action "$ip" /bin/false
 fi
done

#fen fa fa scripts

for i in 125 126
do
 scp -P 22 -rp  ~/scripts   daxian003@192.168.70.$i:~
done

# install telnet
for n in 125 126
do
ssh -t -p 22 daxian003@192.168.70.$n sudo /bin/bash ~/scripts/install.sh
done

第五步 创建安装telnet脚本

mkdir -p script

#!/bin/sh
yum
install telnet -y

第六步运行

sh fenfa.sh

 

第七部检验125,126都创建好telnet

 rpm -qa telnet
telnet-0.17-48.el6.x86_64

 

posted @ 2018-01-03 16:59  大仙儿没溜儿  阅读(217)  评论(0编辑  收藏  举报