LINUX

Shell Script para transferir archivos usando SCP

Hoy tenemos un script de shell interactivo que transferirá archivos o directorios desde el origen al destino. Este script permitirá copiar varios archivos y directorios. El script solo funcionará suponiendo que haya configurado la autenticación basada en claves ssh entre dos máquinas.

El script solicitará ingresar el nombre de host de destino o la dirección IP y la ruta de la carpeta de destino. En la nueva línea, debe proporcionar el nombre de archivo o directorio que debe transferirse. Solo se le permitirá copiar un archivo o directorio por línea. Al presionar enter, se le permitirá proporcionar el siguiente archivo / directorio. El script se cerrará una vez que reciba un campo en blanco.

#!/bin/bash
#next line prints hearer of script
echo "Interactive Script to Copy File (files) / Directory using scp"
#next line check if entered value is not null, and if null it will reask user to enter Destination Server
while [ x$desthost = "x" ]; do
#next line prints what userd should enter, and stores entered value to variable with name desthost
read -p "Destination Server Name : " desthost
#next line finishes while loop
done
#next line check if entered value is not null, and if null it will reask user to enter Destination Path
while [ x$destpath = "x" ]; do
#next line prints what userd should enter, and stores entered value to variable with name destpath
read -p "Destination Path : " destpath
#next line finishes while loop
done
#next line put null value to variable filename
filename="null"
#next line check if entered value is null, and If not null it will reask user to enter file(s) to copy
while ! [ x"$filename" = "x" ]; do
#next line prints what userd should enter, and stores entered value to variable with name filename
read -p "Path to source directory / file : " filename
#next line checks if entered value is not null, and if not null it will copy file(s)
if ! [ x"$filename" = "x" ];
then
#next line prints header
echo -n "Copying $filename ... "
#next like copy pre-entered file(s) or dir to destination path on destination server
scp -r "$filename" "$desthost":"$destpath"
#end of if
fi
#next line finishes while loop
done

script de shell scp

Ejecutando el script SCP

[root@TestNode1 ~]# sh scpcopyscript.sh
Interactive Script to Copy File (files) / Directory using scp
Destination Server Name : 192.168.0.2
Destination Path : /tmp
Path to source directory / file : /root/backup.txt
backup.txt 100% 0 0.0KB/s 00:00
Path to source directory / file : /root/docsdir
file2.txt 100% 0 0.0KB/s 00:00
file3.txt 100% 0 0.0KB/s 00:00
file1.txt 100% 0 0.0KB/s 00:00
Path to source directory / file :
[root@TestNode1 ~]#

Publicaciones relacionadas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Botón volver arriba
Cerrar