
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
Running a Shell Script on a 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 webmaster@172.17.0.2 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 webmaster@172.17.0.2 ‘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 Linux@172.17.0.2 ./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.
- Related Articles
- Aborting a shell script on any command fails
- How to Create a crontab Through a Script on Linux
- Ensure Only One Instance of a Bash Script Is Running on Linux
- Linux Commands Using Secure Shell (ssh)
- Storing a Command in a Variable in a Shell Script
- 10 Best PuTTY Alternatives for SSH Remote Connection
- How to terminate a MongoDB shell script earlier?
- What is Shell Script?
- Showing a GUI Notification From a Shell Script in Linux
- How to copy a file to a remote server in Python using SCP or SSH?
- Copying SSH Keys to different Linux Machine
- Get output of MongoDB shell script?
- What is a memory error in a Python Machine-Learning Script?
- A Shell Script to Send Email Alert When Memory Gets Low
- Use result from MongoDB in shell script?
