zoukankan      html  css  js  c++  java
  • ssh 免密码登录实现批量处理

    搭建集群的时候ssh 免密码登录是一个问题以下脚本将实现批量处理

    文件1主机名:host

    17.19.18.11:123
    17.19.18.12:123

    文件2:ssh_setup.py

    #!/usr/bin/python
    
    import subprocess
    import os
    
    file_dir='/home/hosts'
    
    with open(file_dir) as data:
        for each_line in data.readlines():
            if each_line != '':
                (ip,passwd)=each_line.split(':',2)
                print('./sshkey.exp '+ip+' root '+passwd.strip('
    ')+' | grep ssh-rsa >> ~/.ssh/authorized_keys')
                subprocess.Popen('./sshkey.exp '+ip+' root '+passwd.strip('
    ')+' | grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
              #  subprocess.Popen('./sshkey.exp '+ip+' root '+passwd+' \| grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
            else:
                pass
    subprocess.Popen('chmod 755 ~/.ssh/authorized_keys',shell=True)
    #subprocess.Popen('/home/ssh_distribute.py',shell=True) 

    文件3:ssh_distribute.py

    #!/usr/bin/python
    
    import subprocess
    import os
    
    file_dir='/home/hosts'
    
    with open(file_dir) as data:
        for each_line in data.readlines():
            if each_line != '':
                (ip,passwd)=each_line.split(':',2)
                print('./noscp.exp ~/.ssh/authorized_keys '+ip+':~/.ssh '+'root '+passwd.strip('
    '))
                subprocess.Popen('./noscp.exp ~/.ssh/authorized_keys '+ip+':~/.ssh '+'root '+passwd.strip('
    '),shell=True)
              #  subprocess.Popen('./sshkey.exp '+ip+' root '+passwd+' \| grep ssh-rsa >> ~/.ssh/authorized_keys',shell=True)
            else:
                pass
    #subprocess.Popen('chmod 755 ~/.ssh/authorized_keys',shell=True)

    文件4:noscp.exp

    #!/usr/bin/expect
    
    #noscp.exp
    
    if {$argc<4} { 
     puts stderr "Usage: $argv0 localfile  remotefile user passwd " 
     exit 1 
    }
    
    set localfile [ lindex $argv 0 ] 
    set remotefile  [ lindex $argv 1 ] 
    set user  [ lindex $argv 2 ] 
    set pwd  [ lindex $argv 3 ]
    
    set timeout 30
    
    spawn scp ${localfile}  ${user}@${remotefile}
    
    expect { 
     "*yes/no" { send "yes
    "; exp_continue } 
     "*password:" { send "$pwd
    " } 
    }
    
    expect eof

    文件5:sshkey.exp

    #!/usr/bin/expect
    
    #sshkey.exp
    
    if {$argc<3} { 
     puts stderr "Usage: $argv0 host  user  passwd " 
     exit 1 
    }
    
    set host [ lindex $argv 0 ] 
    set user  [ lindex $argv 1 ] 
    set pwd  [ lindex $argv 2 ]
    set timeout 30
    #spawn ssh  ${user}@${host} "rm -rf ~/.ssh/id_rsa*" 
    # 
    #expect { 
    # "*yes/no" { send "yes
    "; exp_continue } 
    # "*password:" { send "$pwd
    "; exp_continue  } 
    #}
    
    
    spawn ssh  ${user}@${host} "ssh-keygen -t rsa"
    
    expect { 
     "*yes/no" { send "yes
    "; exp_continue } 
     "*password:" { send "$pwd
    "; exp_continue  } 
     "Enter file in which to save the key*" { send "
    
    "; exp_continue } 
     "Overwrite*" { send "y
    "; exp_continue }  
     "Enter passphrase (empty for no passphrase):" { send "
    
    "; exp_continue } 
     "Enter same passphrase again:" { send "
    
    " } 
    }
    
    spawn ssh  ${user}@${host} "cat ~/.ssh/id_rsa.pub"
    expect { 
     "*yes/no" { send "yes
    "; exp_continue } 
     "*password:" { send "$pwd
    "  } 
    }
    
    expect eof

    步骤:

    1.将此文件夹内文件拷贝到/home目录下
    2.host内添加所有待处理的 ip:密码 
    3.安装expect 如果没有 yum install expect
    4.执行./ssh_setup.py 
    5.执行./ssh_distribute.py

  • 相关阅读:
    python : 将txt文件中的数据读为numpy数组或列表
    ROS :为IDE配置环境变量
    ROS 教程之 navigation :在 catkin 环境下创建costmap layer plugin
    ROS 教程之 vision : 用各种摄像头获取图像
    ROS 教程之 network:多台计算机之间网络通信(2)
    MS sql 查询动态表名的方法
    把ocx打包成CAB,并签名
    DataSet 转换成List
    C# 反序列化datetime的处理
    PSP软件开发过程管理
  • 原文地址:https://www.cnblogs.com/zhanggl/p/5704020.html
Copyright © 2011-2022 走看看