Linux Shell (SSH bulk configuration secret) read the configuration file for remote operation

Source: Internet
Author: User

Requires the target machine to have the expect command installed

Divided into five files config.ini (config file), id_ras.pub (public key), read.sh (a function for reading the configuration file), test.sh (execution file), run.sh (remote run script) is a configuration file.

Step 1: Read the configuration file ip,user,pwd

Step 2: Id_rsa.pub, run.sh SCP to the remote machine

Step 3: Run the script remotely

read.sh

#! /bin/Bashgetkey () { section=$(Echo$1|Cut-D'.'-F1) Key=$(Echo$1|Cut-D'.'-F2)    if["$section"x ="IP"X]; ThenNUB=`CatConfig.ini |grep-N"$key ={"|awk-F":" '{print $}'' Nue=`CatConfig.ini |grep-N"}"|awk-F":" '{print $}'' nub=`Expr$nub +1`         forIinch$nue Do            if[$i-gt $NUB]; Thena=$i Breakfi         DoneNue=`Expr$a-1`        sed-N"$nub, $nue p"Config |seds/,//g    Else        sed-N"/\[$section \]/,/\[.*\]/{/^\[.*\]/D/^[\t]*$/D/^$/D/^#.*$/D S/^[\t]* $key [\t]*=[\t]*\ (. *\) [\t]*/\1/P}"Config.ini}

run.sh

#! /bin/bashcreate () {/usr/bin/expect-c"Set timeout -SpawnSsh-keygen-t RSA expect \":\"Send \"\r\ "Expect \":\"Send \"\r\ "Expect \":\"Send \"\r\ "expect EOF exit"}if[!-D.SSH]; ThenCreateif[ $? !=0]; Then        Echo "step for Create failed"Exit0    Else        CatId_rsa.pub >>.SSH/Authorized_keysif[ $? !=0]; Then            Echo "step for CP failed"Exit0        fi    fiElse    CatId_rsa.pub >>.SSH/Authorized_keysif[ $? !=0]; Then        Echo "step for CP failed"Exit0    fifi

test.sh

#!/bin/bash../read.SHIP=$ (GetKey"Ip.ip")passwd=$ (GetKey"pass.passwd") User=$ (GetKey"user.usr") forIinch$ip Do/usr/bin/expect-c"Set Timeout10spawn/usr/bin/SCP-R run.SH[email protected] $iexpect \"password:\ " {Send \ "$passwd \r\"}spawn/usr/bin/SCP-r id_rsa.pub [email protected] $iexpect \"password:\ " {Send \ "$passwd \r\"}expect Eofexit"/usr/bin/expect <<eofset TimeoutTenSpawnSSH[email protected] $iexpect"Password:"Send"$passwd \ r"expect"]"Send"SH run.sh\r"Send"exit\r"expect eofeof Done

Configuration file

[Ip]ip={12.23.  31.114,21.34. 54.112 }[user]usr=Hehehe[pass]passwd=wohehehda

Linux Shell (SSH bulk configuration secret) read the configuration file for remote operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.