Support >
  About independent server >
  Use the nc and pv commands to transfer files between the two servers
Use the nc and pv commands to transfer files between the two servers
Time : 2024-11-14 11:59:20
Edit : Jtti

To transfer files between the two servers using the 'nc' (Netcat) and 'pv' commands, follow these steps.

If transferring A single file, action on server A (sender) : Use 'dd' or another command to generate a test file, or select an existing file for transfer.

dd if=/dev/zero of=CentOS6_20190825.iso bs=1024M count=1

Use the 'pv' command combined with the 'nc' command to send the file and display the transfer progress.

pv CentOS6_20190825.iso | nc -l 2345

'-l' indicates the listening mode, and '2345' is the selected port number.

Operations on server B (receiver) :

Use the 'nc' command to receive the file from server A and the 'pv' command to display the transfer progress.

nc 10.20.10.60 2345 | pv > CentOS6-20190825.iso

'10.20.10.60' is the IP address of server A and '2345' is the port number on which server A listens.

If you want to transfer the entire directory using 'nc' and 'pv'. Action on the sender (server A) : Go to the directory you want to transfer.

cd /usr/local/mysql/data

Compress the directory using the 'tar' command and send it through 'pv' and 'nc'.

tar -zcf -  | pv | nc -l 3456

Here '-l' represents the listening mode and '3456' is the port number.

Operation on the receiver (server B) : Use the 'nc' command to receive the compressed directory from server A, and use the 'pv' command to display the transfer progress.

nc 10.20.10.60 3456 | pv | tar -zxf -

Here '10.20.10.60' is the IP address of server A and '3456' is the port number on which server A listens.

This is how to use 'nc' and 'pv' to complete the transfer of files and directories between two servers. The transfer progress can also be displayed. The above steps provide a simple and effective method, especially if no 'scp' or 'rsync' is available.

If it is two servers using the network is not the same. Transferring files with the 'nc' (Netcat) and 'pv' commands requires consideration of network reachability and port openness.

Ensure network accessibility. Ensure that the network between the two servers is connected, that is, you can SSH from one server to the other server.

Open the necessary ports. If the server has a firewall, make sure that the port selected for transmission (TCP port by default) is open on both servers.

Use 'nc' to listen to the port. On the receiving server, listen to a port with the 'nc' command, ready to receive the file.

nc -l 1234 > received_file

'-l' indicates the listening mode, '1234' is the selected port number, and 'received_file' is the received file name.

Use 'nc' to send files. On the sending server, use the 'nc' command to connect to the IP address and port of the receiving server and send the file.

nc Receiver server IP address 1234 < file_to_send

Here 'receiver server IP' is the IP address of the receiving server, '1234' is the port number listened by the receiving server, and 'file_to_send' is the file name to send.

Use 'pv' to monitor the transfer progress. If you want to monitor the progress of the file transfer, you can use the 'pv' command on the sending side.

pv file_to_send | nc IP address of the receiver server 1234

The 'pv' command displays the real-time speed and progress of the transfer.

Compress files for efficiency. If the file is large, consider compressing the file before transferring it to reduce the transfer time.

tar czvf-file_to_send | pv | nc IP address of the receiver server 1234

The receiving end needs to be decompressed:

nc -l 1234 | pv | tar xz

Handle SSH connections across the network: If two servers cannot connect directly over SSH, you may need to set up SSH related technologies to establish a connection.

Consider using encrypted transmissions. If the transmitted data contains sensitive information, consider using an SSH tunnel to encrypt the transmission.

Note that these steps assume that you have sufficient permissions to perform these actions on the server and that the network environment allows such transfers. In actual operations, you may need to adjust the network environment and server configuration.

JTTI-Defl
JTTI-COCO
JTTI-Selina
JTTI-Ellis
JTTI-Eom