Copy files and run commands through SSH
Connecting to the server via SSH is one of the primary means for managing *nix servers. Quite often, it is necessary to upload a file to a remote server or unload it, but there are no other means other than SSH connection. Fortunately, one of the regular functions of this Protocol is to copy files via a secure connection. You can implement it using a scp command on Linux systems, or using pscp.exe, which is part of the Putty SSH client on the Windows operating system.
Using the Linux OS
Execute the following commands:
scp [modifier] [source] [target_directory]
If a remote server serves as the source or target directory, execute the following commands:
After running the command, you need to enter the password for the remote server account.
To sum up, you can copy the local file named: /home/user/file.tgz to the root user home directory of the remote server 220.127.116.11 executing the following command:
scp /home/user/file.tgz firstname.lastname@example.org:/root
To download the same file from a remote server:
scp email@example.com:/root/file.tgz /home/user
You can copy several files at once. To do this, you must add them as the source. Separate them by a space – the last parameter will be considered as the target directory. For example, to upload file1 files.tgz and file2.tgz from the local directory to the remote server you have to execute the command:
scp file1.tgz file2.tgz firstname.lastname@example.org:/root
To copy the directory, you will need to use the –r command modifier. Copy the local directory /home/user/dir to the remote server:
scp-r /home/user/dir email@example.com:/root
When the SSH server is running on a non-default port, we need to get the help of the -P option. If you need to use port 10022:
scp-P 10022 /home/user/file.tgz firstname.lastname@example.org:/root
To find out what other modifiers you can use, simply execute scp without parameters and read the help.
Using the Windows OS
When using the Windows operating system and Putty as a client, the command formatting remains the same. However, we will change the name of the executable file. We need to use a syntax for specifying paths to Windows files and directories when mentioning the source or target directories. Run the command line (cmd.exe) or PowerShell, open the directory with the pscp .exe file and enter the command:
pscp.exe C: Tempfile.tgz email@example.com:/root
If you run it from another folder, you will need to specify the full path to pscp.exe. If any of the paths contain spaces, we need to use double quotes — «file path”:
“C:Program FilesPuttypscp.exe” C:Tempfile.tgz firstname.lastname@example.org:/root
Simply execute pscp (similarly to scp) without parameters and read the help on the modifiers as well as the command syntax information.
Running commands on a remote server using the SSH connection
SSH protocol supports both running interactive sessions and regular commands and scripts on a remote server.
Using the Linux OS
ssh [user]@[server] ‘[command]’
Enter the password of the specified user and get the command output in the SSH console, if any.
For example, we can find information about the operating system installed on the remote server:
ssh email@example.com ‘uname -a’
To run multiple commands using a single connection, you can use the “;” character as a separator. Check the network settings and active network connections on the remote server:
ssh firstname.lastname@example.org ‘ifconfig; netstat -anp tcp’
If you need to run a local script file on a remote server: run the command interpreter (in the script execution mode). It can be bash with the-s key and pass the script file to it for the standard input. Take a look at the example below:
ssh email@example.com ‘bash -s’ < /home/user/myscript.sh
The local file: /home/user/myscript.sh will be executed on a remote server.
Running the SSH command without parameters allows you to see a brief syntax reference and a list of additional modifiers that allow you to extend the command’s functionality.
Using the Windows OS
To connect to the remote server, we will use the Putty client with its executable plink.exe. File, if our computer supports Windows. Use the command line (cmd.exe) or PowerShell to work with this file.
To run the command on a remote server, use the following syntax:
plink.exe [server] - ssh-l [user] “[command]”
Check the network interfaces configuration:
plink.exe 18.104.22.168 -ssh-l root “ifconfig”
plink.exe allows you to use “;” as a separator to run multiple commands, likewise the SSH command in Linux:
plink.exe 22.214.171.124 -ssh-l root “ifconfig; netstat-anp tcp”
You can run commands from a local file using an additional key -m:
plink.exe 126.96.36.199 -ssh-l root-m “C:Tempmyscript.sh”
By running the plink.exe command without parameters, you can find a brief syntax reference and a list of additional command modifiers.