将文件从一个服务器复制到另一个服务器的Shell脚本

6tr1vspr  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(150)

我有2个solaris服务器。我想写一个shell脚本,将文件从一个服务器复制到其他。

scp /tmp/test/a.war [email protected]:/tmp/

字符串
在PUTTY中执行上述命令时,将要求我输入目的地的密码。使用PUTTY时,这很好。
如何在通过shell脚本运行scp命令时输入密码?
Thanks in advance

xa9qqrwz

xa9qqrwz1#

您必须设置SSH私钥/公钥。
生成公钥行后,将公钥行条目放在目标服务器和用户的~/.ssh/authorized_keys文件中。
确保源机器上的文件(对于将运行scp/ssh命令的用户)将具有推荐的文件权限(400)。
https://unix.stackexchange.com/questions/182483/scp-without-password-prompt-using-different-username
http://docs.oracle.com/cd/E19253-01/816-4557/sshuser-33/index.html或类似的在线帮助可以帮助您。

xdyibdwo

xdyibdwo2#

#!/bin/bash

# Check if the input file exists
input_file="input.txt"
if [ ! -f "$input_file" ]; then
echo "Error: Input file '$input_file' not found."
exit 1
fi

# Output file to log the copied files and errors
output_file="copied_files.log"

# Read input data line by line from the input file
while IFS= read -r line || [[ -n "$line" ]]; do
# Extract remote server IP and destination path from the current 
line
remote_server_ip=$(echo "$line" | awk '{print $1}')
destination_path=$(echo "$line" | awk '{print $2}')

# Display what is being read from the input file for 
troubleshooting
echo "Reading from input file:"
echo "REMOTE_SERVER_IP=$remote_server_ip"
echo "DESTINATION_PATH=$destination_path"
echo "-----------------------------"

# Check if remote server IP and destination path are provided
if [ -z "$remote_server_ip" ] || [ -z "$destination_path" ]; then
    echo "Error: Remote server IP or destination path not provided 
in the input file." >> "$output_file"
else
    # List of files to copy
    files_to_copy=("file1.txt" "file2.txt" "file3.txt")

    # Copy files to the remote server using scp and log the 
details/errors to the output file
    for file in "${files_to_copy[@]}"; do
        # Check if the file exists in the destination folder
        ssh "$remote_server_ip" "[ -e \"$destination_path/$file\" 
]"
        if [ $? -eq 0 ]; then
            echo "File: '$file', Server IP: '$remote_server_ip', 
Destination Path: '$destination_path' - Already Exists" >> 
"$output_file"
        else
            # Attempt to copy the file
            scp "$file" "$remote_server_ip":"$destination_path" 2>> 
"$output_file"
            if [ $? -eq 0 ]; then
                echo "File: '$file', Server IP: 
'$remote_server_ip', Destination Path: '$destination_path' - Copy 
Successful" >> "$output_file"
            else
                echo "File: '$file', Server IP: 
'$remote_server_ip', Destination Path: '$destination_path' - Copy 
Failed" >> "$output_file"
            fi
        fi
    done
fi
done < "$input_file"

echo "Copying process completed. Details/errors logged in 
'$output_file'."

字符串

相关问题