[Shell instance]--a script implementation to batch copy files to multiple servers (Nmap, SCP)

Source: Internet
Author: User
Tags bz2 set time

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-    -&GT;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)

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.