Practice Environment:
(1) All servers will shut down firewalls and SELinux
(2) The root password for all servers is set to AIXOCM
(3) All servers are 10.0.100.* network segments and are guaranteed to communicate with other hosts
(4) All servers ensure that the SSHD service is started (open the default 22 port)
The script requires implementation:
Package the file with the Conf extension in the native/etc directory to tar.bz2, and then copy the file to the 10.0.100.1-->10.0.100.254 address segment for all open hosts with the/tmp/sjjy/file. You do not need to enter a password manually when using SCP replication.
The approximate idea of the script:
(1) Tar_etc () function: Implement the packaging of all *.conf directories under/etc and determine if the package is successful.
(2) Nmap_ip () function: Implements scanning of hosts that are turned on. Implemented with nmap command. and write these host IPs to the/mydate/ip.txt file.
(3) Scp_ip () function: implements the sending of a compressed package to the specified directory of each host. The input implemented using the #expect command.
#!/bin/bash#============================================ #function-->tar_etc file under package/etc/*.conf #============== ==============================tar_etc () {Tar jcvf/tmp/lyj.tar.bz2/etc/*.conf &>/dev/null if [$?-eq 0] Then echo "Compress package complete" Else echo "package failed check" fi}#========================================= #function- ->NMAP_IP Scan Network Segment Open Host #=========================================nmap_ip () {[-f/mydate/ip.txt] if [$?-eq 0] Then cat/dev/null >/mydate/ip.txt Else touch/mydate/ip.txt fi nmap-n-sp 10.0.100.1-254 | grep 10.0.100 | awk ' {print $} ' >/mydate/ip.txt}#====================================== #function-->scp_ip copy files to each host #======= ===============================scp_ip () {cat/mydate/ip.txt | while read Linedo (/usr/bin/expect << EOF set time Spawn scp/tmp/lyj.tar.bz2 [email protected] $line:/tmp/sjjy expect {"*yes/no*" {send "yes\r" ; Exp_continue} "*PASSWOrd: "{send" aixocm\r "}} expect eofeof) &>/dev/null if [$?] -eq 0] then echo "Copy files to $line success! "Else echo" failed to copy file to $line! "Fidone}tar_etcnmap_ipscp_ip
[Shell instance]--a script implementation to batch copy files to multiple servers (Nmap, SCP)