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