Make a remote copy command script with parameters under Ubuntu

In Linux systems, both rcp and scp are command tools for remote file transfer.

  1. rcp (Remote Copy Protocol): rcp is a remote file copy protocol for transferring files between local and remote hosts. It uses the rsh (Remote Shell) protocol to establish remote connections and transmit data in clear text. Use the rcp command to copy files from the local to the remote host, or copy files from the remote host to the local.

Example:

rcp local_file                             remote_username@remote_ip:remote_file
rcp remote_username@remote_ip:remote_file  local_file
  1. scp (Secure Copy Protocol): scp is a remote file copy tool based on the SSH (Secure Shell) protocol, which provides higher security. It uses an SSH connection for encrypted transmission, ensuring data confidentiality and integrity. Like rcp, scp can also copy files between local and remote hosts.

Example:

scp local_file                            remote_username@remote_ip:remote_file
scp remote_username@remote_ip:remote_file local_file

The role of cp and scp is to copy files from the local host to the remote host, or copy files from the remote host to the local host. This is useful when you need to transfer files between different hosts, such as backing up files, synchronizing data, etc. At the same time, because scp uses SSH connection for encrypted transmission, it is more commonly used in scenarios where transmission security needs to be guaranteed.

Generally, we can use rcp in the local area network.

If we want to synchronize files and directories from one machine to other machines, we can create a script file. The following runRcp.sh script is to copy all files and directories under /home/xtwh/kaifa/exe/ from this machine to a remote machine Under the same directory:

vi runRcp.sh 
rcp -rp /home/xtwh/kaifa/exe/* 192.168.30.42:/home/xtwh/kaifa/exe/

But in this way, the script needs to be changed every time a machine is copied.

To runRcp.shchange the script file into a form with parameters, you can use special variables of the shell script $1, $2etc. to receive the incoming parameters. Here is an example of a modified script:

#!/bin/bash

# 检查参数数量
if [ $# -ne 1 ]; then
    echo "Usage: $0 <destinationIp>"
    exit 1
fi

# 获取传入的参数
destinationIp=$1

# 执行 rcp 命令
rcp -rp /home/xtwh/kaifa/exe/* "$destinationIp":/home/xtwh/kaifa/exe/

In the above example, $1represents the first parameter, which is the destination address. Add a parameter count check at the beginning of the script to ensure that only one parameter is passed in. After the script receives the target address parameter, it assigns it to destination_addressthe variable. Then, use variables "$destinationIp"to replace the original fixed target address to achieve dynamic parameter passing.

When saving the modified script file and executing it, you can pass in the target address parameter in the following ways:

./runRcp.sh 192.168.30.42

Among them, 192.168.30.42passing in the script as the target address parameter achieves the desired function, so that the same script can easily synchronize files and directories of multiple machines.

Guess you like

Origin blog.csdn.net/weixin_39466327/article/details/132466291