Scp batch uploads files to multiple machines (upgraded version)

Source: Internet
Author: User
Tags scp file ssh port

Upgrade the previous version, add support for password input from the command line, and add a prompt indicating that the upload is successful or failed.
It is very difficult for O & M to manage multiple machines at the same time. In particular, if cdn o & M needs to upload a file to 1000 machines, it is very difficult to manually upload files one by one, for this reason, I wrote a batch scp file to a small program on multiple machines.
CT is used:
In this program, keep CT is used to help automatically enter the scp password. Keep CT is mainly used to turn programs requiring manual interaction into programs for automation. This batch deployment system for O & M and batch unattended installation, execute commands in batches to upload and download files in batches.
Modern Shell provides minimum control (start, stop, and so on) for the program, and leaves the interaction feature to the user. This means that you cannot run some programs without interaction, such as passwd. Some programs can be run without interaction, but the flexibility is largely lost, such as fsck. This indicates that the construction logic of Unix tools is starting to have problems. CT just fills in some of the cracks and solves some problems that have existed in the Unix environment for a long time.

 
Expect uses Tcl as the core language. In addition, CT can be used no matter whether the program is interactive or non-interactive.

 
1. Source Code of multi_scp_upload.sh
 
#! /Bin/bash
# Author: yifangyou
# Create time: 2011-05-17
# Used to batch upload files or directories to the specified directory of the target machine through scp
# Configuration file format:
# Ssh_hosts = ("1.1.1.1" "2.2.2.2 ")
# Ssh_ports = ("22" "22"). The default value is 22, or the number is less than ssh_hosts. Use the default value.
# Ssh_users = ("root" "root"). The default value is root, or the number is smaller than ssh_hosts. Use the default value.
# Ssh_passwords = ("323" "222") can be set to the default value. If the default value is set to ssh_hosts, enter the value through the command line.
# Run sh multi_scp.sh conf_file_path file target
If [-z "$3"]
Then
Echo "sh multi_scp.sh conf_file_path file target ";
Exit;
Fi
Default_ssh_user = "root"
Default_ssh_port = "22 ";
# Upload shell script file path
Scp_upload = scp_upload.sh
# Configure file path
Conf_file = $1
# Then upload file path
Scp_file = $2
# Remote hosttarget file or dir path
Scp_target = $3
# Determine whether the conf_file configuration file exists
If [! -E "$ conf_file"]
Then
Echo "$ conf_file is not exists ";
Exit;
Fi
# Determine whether scp_file is a file or directory
If [! -E "$ scp_file"] & [! -D "$ scp_file"]
Then
Echo "$ scp_file is not exists ";
Exit;
Fi
# Read configure file
Source $ conf_file
# If the password is not provided in the configuration file, enter
If ["$ {# ssh_passwords [@]}" = "0"] | ["$ {# ssh_passwords [@]}"-lt "$ {# ssh_hosts [@]} "]
Then
Read-p "please input password:"-s default_ssh_password
Fi
Success_hosts = "";
Fail_hosts = "";
For (I = 0; I <$ {# ssh_hosts [@]}; I ++ ))
Do
# Remote ssh host
Ssh_host =$ {ssh_hosts [$ I]};
# Remote ssh port
Ssh_port =$ {ssh_ports [$ I]};
If ["$ ssh_port" = ""]
Then
Ssh_port = $ default_ssh_port;
Fi
# Remote ssh user
Ssh_user =$ {ssh_users [$ I]};
If ["$ ssh_user" = ""]
Then
Ssh_user = $ default_ssh_user;
Fi
# Remote ssh password
Ssh_password =$ {ssh_passwords [$ I]};
If ["$ ssh_password" = ""]
Then
Ssh_password = $ default_ssh_password;
Fi
Echo "[" 'date + "% F % T" '"] (scp-r $ scp_file $ ssh_user @ $ ssh_host: $ ssh_port: $ scp_target) start"
# Scp file or dir
/Usr/bin/empty CT scp_upload.sh "$ ssh_host" "$ ssh_port" "$ ssh_user" "$ ssh_password" "$ scp_file" "$ scp_target"
If ["$? "-Eq" 0 "]
Then
Success_hosts = "$ success_hosts, $ ssh_host"
Else
Fail_hosts = "$ fail_hosts, $ ssh_host"
Fi
Echo "[" 'date + "% F % T" '"] (scp-r $ scp_file $ ssh_user @ $ ssh_host: $ ssh_port: $ scp_target) end"
Echo ""
Done
Echo "success_hosts = [$ success_hosts]"
Echo "fail_hosts = [$ fail_hosts]"
2. Source Code of scp_upload.sh
 
#! /Usr/bin/CT
# Author: yifangyou
# Create time: 2011-05-17
# Host
Set scphost "[lindex $ argv 0]"
# Ssh port
Set port "[lindex $ argv 1]"
# Ssh user name
Set scpuser "[lindex $ argv 2]"
# Ssh Password
Set scppw "[lindex $ argv 3]"
# File name or directory to be uploaded
Set file "[lindex $ argv 4]"
# Name or directory of the file to be uploaded to the remote machine
Set target "[lindex $ argv 5]"
Spawn scp-r-P $ port $ file $ scpuser @ $ scphost: $ target
# Set the timeout time to prevent remote machine firewall from being suspended.
Set timeout 30
CT {
# Respose: "root@1.2.3.4s password:", automatically enter the password
"* Password *"{
Set timeout 30
Send "$ scppw"
}
# The first connect will respose "Are you sure you want to continue connecting (yes/no )? Yes"
"* Yes *"{
Set timeout 30
Send "yes"
Set timeout 30
Login CT "* password *"
Set timeout 30
Send "$ scppw"
}
Busy {send_user "<error: busy>"; exit 1 ;}
Failed {send_user "<error: failed>"; exit 2 ;}
Timeout {send_user "<error: timeout>"; exit 3 ;}
}
# Permission denied not try again, Return Error Information
Set timeout 30
CT {
"* Denied *"{
Send_user "<error: Permission denied>"
Exit 4
}
"* No such file *"{
Send_user "<error: No such file>"
Exit 5
}
Busy {send_user "<error: busy>"; exit 6 ;}
Failed {send_user "<error: failed>"; exit 7 ;}
Timeout {send_user "<error: timeout>"; exit 8 ;}
}
Exit 0
3. Configuration File Format: scp. conf
 
# Ssh_hosts = ("1.1.1.1" "2.2.2.2 ")
# Ssh_ports = ("22" "22") # wheen port_num # Ssh_users = ("root" "root") # wheen user_num # Ssh_passwords = ("323" "222") # wheen password_num 4. Run the code
Find a machine that can be connected to the machine to be uploaded and install the secondary CT (you can use the secondary CT command to test whether it has been installed)
Put scp_upload.sh, multi_scp_upload.sh, and scp. conf in the same directory and run multi_scp_upload.sh.
5. Running Effect

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.