zoukankan      html  css  js  c++  java
  • 使用expect实现ssh免密码登陆

    使用expect向ip列表文件中的ip主机,执行ssh-copy-id命令复制密钥,以实现ssh免密登陆。

    安装expect

    yum install -y expect
    

    生成密钥对

    ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
    

    脚本

    //shell脚本内容
    # -------------------------
    vim ssh-copy-id.sh
    
    #!/bin/bash
    #
    [ $# -gt 0 -a -f "$1" ] || exit 1
    
    cat $1 | while read ip ;do
        user='root'
        password='centos'
    
        expect ./ssh-copy-id.exp $ip $user $password
    done
    
    
    
    
    //expect脚本内容
    -------------------------
    vim ssh-copy-id.exp
    
    #!/usr/bin/expect
    set ip [lindex $argv 0]
    set user [lindex $argv 1]
    set password [lindex $argv 2]
    set timeout 10
    spawn ssh-copy-id $user@$ip
    expect {
        "yes/no" { send "yes
    ";exp_continue }
        "password" { send "$password
    " }
    }
    expect eof
    
    
    
    //iplists文件
    -------------------------
    vim iplists.txt
    
    192.168.10.101
    192.168.10.102
    192.168.10.103
    192.168.10.104
    192.168.10.105
    
    
    
    //调用脚本
    sh ssh-copy-id.sh iplists.txt
    
    书山有路勤为径,学海无涯苦作舟
  • 相关阅读:
    centos git编译
    Unix/Linux小计
    centos gcc编译
    c++隐式转换(implicit conversion)
    通用c程序Makefile
    对弈的Python学习笔记
    LeetCode最长回文子串
    JDBC09 CLOB文本大对象
    JDBC08时间处理
    JDBC07 事务
  • 原文地址:https://www.cnblogs.com/haona_li/p/10182703.html
Copyright © 2011-2022 走看看