• Linux shell (ssh批量配置免秘)读取配置文件,进行远程操作


    需要目标机器安装有 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 $1 |cut -d '.' -f 1)
        key=$(echo $1 |cut -d '.' -f 2)
        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 +1`
            for i in $nue
            do
                if [ $i -gt $nub ]; then
                    a=$i
                    break
                fi
            done
            nue=`expr $a -1`
            sed -n "$nub,$nue p" config |sed s/,//g
        else
            sed -n "/[$section]/,/[.*]/{    
             /^[.*]/d    
             /^[ 	]*$/d    
             /^$/d    
             /^#.*$/d    
             s/^[ 	]*$key[ 	]*=[ 	]*(.*)[ 	]*/1/p    
            }" config.ini    
    } 

    run.sh

    #!/bin/bash
    
    Create(){
        /usr/bin/expect -c"
        set timeout 30
        spawn ssh-keygen -t rsa
        expect ":"
        send "
    "
        expect ":"
        send "
    "
        expect ":"
        send "
    "
        expect eof
        exit
        "
    }
    
    if [ ! -d .ssh ];then
        Create
        if [ $? != 0 ];then
            echo "step for create failed"
            exit 0
        else
            cat id_rsa.pub >> .ssh/authorized_keys
            if [ $? != 0 ];then
                echo "step for cp failed"
                exit 0
            fi
        fi
    else
        cat id_rsa.pub >> .ssh/authorized_keys
        if [ $? != 0 ];then
            echo "step for cp failed"
            exit 0
        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
    "}
    spawn /usr/bin/scp -r id_rsa.pub $user@$i
    expect "password:" {send "$passwd
    "}
    expect eof
    exit
    "
    /usr/bin/expect << EOF
    set timeout 10
    spawn ssh user@$i
    expect "password:"
    send "$passwd
    "
    expect "]"
    send "sh run.sh
    "
    send "exit
    "
    expect eof
    EOF
    done

    配置文件

    [ip]
    ip={
    12.23.31.114
    ,21.34.54.112
    }
    
    [user]
    usr=hehehe
    
    [pass]
    passwd=wohehehda
  • 相关阅读:
    DLL注入之Appinit_Dlls
    VC下遍历文件夹中的所有文件的几种方法
    Windows下C语言的Socket编程例子(TCP和UDP)
    Windows进程间共享内存通信实例
    window下线程同步之(Mutex(互斥器) )
    如何安装win10和linux [ubuntu14]双系统
    Windows虚拟地址转物理地址(原理+源码实现,附简单小工具)
    Windows驱动中通过MDL实现用户态与核心态共享内存
    C# Label显示多行文本及换行(WinForm/WebForm)
    使用delegate实现简单的查询功能
  • 原文地址:https://www.cnblogs.com/tester-hehehe/p/5855649.html
Copyright © 2020-2023  润新知