
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Run Shell Script on Remote Machine through SSH
Abstract
It's challenging to envision what would happen if you couldn't control your computer remotely because remote access to computers has long been necessary. The best way to connect to a remote machine is by SSH for Linux-based machines. The SSH client application can be used to log into a distant computer or server and run commands on that computer. When a command is supplied, it is executed instead of a login shell on the remote host or server.
Users frequently need to work with distant systems. which requires them to log into the remote server, carry out specific actions, and then end that session. The ssh client makes it feasible. In this tutorial, we'll look at several ssh configurations for executing remote commands locally.
Note ? Linux commands are case-sensitive.
SSH
"SSH" stands for Secure Shell or Secure Socket Shell. When connecting to a remote server, the secure shell is helpful for security. The data transit between the client and the host occurs in encrypted form, thanks to the secure ssh protocol, which is used by the ssh command. It sends the host the input via the client and receives the output sent by the host. TCP/IP port 22 is used for its execution.
Running Commands over SSH
To acquire the date from the remote machine, let's run the single command "date". In the following example, we will see how to fetch the date of the remote machine,
Example
$ ssh [email protected] date
Output
Mon June 15 08:55:40 IST 2022
To acquire the disk space usage of the remote machine, let's run the single command "df -h".
In the below example, we will see how to fetch the remote server disk space usage,
Example
$ ssh [email protected] ?df -h'
Output
Filesystem Size Used Avail Use% Mounted on overlay 875G 24G 807G 3% / tmpfs 64M 0 64M 0% /dev shm 64M 0 64M 0% /dev/shm /dev/nvme0n1p3 875G 24G 807G 3% /home/cg/root tmpfs 63G 0 63G 0% /proc/acpi tmpfs 63G 0 63G 0% /proc/scsi tmpfs 63G 0 63G 0% /sys/firmware
Running a Shell Script
We can also run scripts remotely using SSH, thus remote execution is not just restricted to commands. All we need to do is give the local script the absolute path of the SSH command.
Let's create a straightforward shell script with the following elements and call it "sys- info.sh".
#! /bash/sh echo "Id" = "` w ` echo "Time" = "` date ` echo "Host" = "` hostnamectl `
Now we will make this script executable by providing the essential permissions using the "chmod" command, and running the shell script "sys-info.sh" using the ssh command on the remote machine. In the generated output we will get the hostname, Id and date,
Example
$ chmod +x sys-info.sh $ ssh [email protected] ./sys-info.sh
Output
Id = User Time = Tue Jan 28 07:18:55 IST 2022 Host = Static hostname: Ubuntu.tutorialspoint.com Icon name: computer-vm Chassis: vm Machine ID: 002f47b82af248f562d52f1c98f Boot ID: dca9a1ba06374d7d96678f94617 Virtualization: kvm Operating System: Ubuntu Linux (Core) CPE OS Name: cpe:/o:Ubuntu:Ubuntu:7 Kernel: Linux 5.13.0-40-generic.x86_64 Architecture: x86_64
Conclusion
In this tutorial, we learned how to use SSH to execute scripts on remote machines. We explored some actual instances. The use of SSH for secure access to the remote machines. First, we saw how to connect to the remote machine by running single commands using SSH such as the date and disk space usage.
Later we created a shell script and ran the script on the remote machine using SSH. These are the most useful Linux commands when using SSH for gaining access to remote machines with their examples.
I hope you find these examples of the commands useful.