#!/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'."
2条答案
按热度按时间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或类似的在线帮助可以帮助您。
xdyibdwo2#
字符串