Linux batch copy data script
Recently, I want to get a Linux-based Batch Transfer copy deployment remote server script
Concept: 1. establish equivalence with remote clients
2. Copy data or execute remote server commands
The automated equivalence script is as follows:
========================================================== ======================================
The server generates an rsa key automatically.
#! /Usr/bin/CT
Rm-rf root/. ssh/known_hosts
CT-c"
Spawn ssh-keygen-t rsa
CT {
\ "* 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 ":")
CT-c"
Spawn ssh-copy-id-I/root/. ssh/id_rsa.pub root @ $ ip
CT {
\ "* Yes/no * \" {send \ "yes \ r \"; exp_continue}
\ "* Password * \" {send \ "$ password \ r \"; exp_continue}
\ "* Password * \" {send \ "$ password \ r \";}
}
"
Done
The content format of ip.txt is as follows:
192.168.1.56: 123456
========================================================== ======================================
Execute the push command from the server to the client
For h in $ (cat/script/ip.txt | cut-f1-d ":")
Do
Ssh root @ $ h "ls $ dire"
Dire = "/tmp/test"
If [$? -Eq 0];
Then
Ssh root @ $ h rm-rf "$ dire"
Set timeout 300
Ssh root @ $ h mkdir-p/tmp/test
Fi
Ssh root @ $ h touch lgl.txt
Scp/root/CentOS-5.3-x86_64-bin-DVD.iso root@192.168.1.56:/home
Set timeout 300
Done
========================================================== ======================================
The script is as follows:
[Root @ lgl script] # cat ssh. sh
#! /Usr/bin/CT
Rm-rf root/. ssh/known_hosts
CT-c"
Spawn ssh-keygen-t rsa
CT {
\ "* 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 ":")
CT-c"
Spawn ssh-copy-id-I/root/. ssh/id_rsa.pub root @ $ ip
CT {
\ "* 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 root @ $ h "ls $ dire"
Dire = "/tmp/test"
If [$? -Eq 0];
Then
Ssh root @ $ h rm-rf "$ dire"
Set timeout 300
Ssh root @ $ h mkdir-p/tmp/test
Fi
Ssh root @ $ h touch lgl.txt
Scp/root/CentOS-5.3-x86_64-bin-DVD.iso root@192.168.1.56:/home
Set timeout 300
Done
This article permanently updates the link address: