Recently wanted to get a bulk transfer copy deploy remote server script
Train of thought: 1. Establishing equivalence with remote clients
2. Copying data, or executing remote server commands
The automation equivalence script is as follows: If you install expect this package
================================================================
Server-side generation automatically generates RSA key
#!/usr/bin/expect
RM-RF root/.ssh/known_hosts
Expect-c "
Spawn ssh-keygen-t RSA
Expect {
\ "*id_rsa*\" {Send \r;exp_continue}
\ "*passphrase*\" {Send \r;exp_continue}
\ "*again*\" {Send \r;exp_continue}
}
===============================================================
Copy the generated key to the remote server
For P in $ (cat/script/ip.txt)
Do
Ip=$ (echo "$p" |cut-f1-d ":")
Password=$ (echo "$p" |cut-f2-d ":")
Expect-c "
Spawn ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected]$ip
Expect {
\ "*yes/no*\" {send \ "yes\r\"; Exp_continue}
\ "*password*\" {send \ "$password \r\"; Exp_continue}
\ "*password*\" {send \ "$password \r\";}
}
"
Done
Where the Ip.txt content format is as follows:
192.168.1.56:123456
===============================================================
Perform service-to-Client push commands
For h in $ (cat/script/ip.txt|cut-f1-d ":")
Do
SSH [email protected]$h "ls $dire"
Dire= "/tmp/test"
If [$?-eq 0];
Then
SSH [email protected]$h rm-rf "$dire"
Set Timeout 300
SSH [email protected]$h mkdir-p/tmp/test
Fi
SSH [email protected]$h Touch lgl.txt
Scp/root/centos-5.3-x86_64-bin-dvd.iso [email protected]:/Home
Set Timeout 300
Done
===============================================================
The final script is as follows:
[email protected] script]# cat ssh.sh
#!/usr/bin/expect
RM-RF root/.ssh/known_hosts
Expect-c "
Spawn ssh-keygen-t RSA
Expect {
\ "*id_rsa*\" {Send \r;exp_continue}
\ "*passphrase*\" {Send \r;exp_continue}
\ "*again*\" {Send \r;exp_continue}
}
"
For P in $ (cat/script/ip.txt)
Do
Ip=$ (echo "$p" |cut-f1-d ":")
Password=$ (echo "$p" |cut-f2-d ":")
Expect-c "
Spawn Ssh-copy-id-i/root/.ssh/id_rsa.pub[email protected]$ip
Expect {
\ "*yes/no*\" {send \ "yes\r\"; Exp_continue}
\ "*password*\" {send \ "$password \r\"; Exp_continue}
\ "*password*\" {send \ "$password \r\";}
}
"
Done
For h in $ (cat/script/ip.txt|cut-f1-d ":")
Do
Ssh[email protected]$h "LS $dire"
Dire= "/tmp/test"
If [$?-eq 0];
Then
Ssh[email protected]$h rm-rf "$dire"
Set Timeout 300
Ssh[email protected]$h mkdir-p/tmp/test
Fi
Ssh[email protected]$h Touch Lgl.txt
Scp/root/centos-5.3-x86_64-bin-dvd.iso[email protected]:/Home
Set Timeout 300
Done
This article is from the "Steven diligence in the Hippie:" blog, please be sure to keep this source http://steven2.blog.51cto.com/855881/1617514
Linux Bulk copy data script