> ## Content Index
> Fetch the complete content index at: https://snubmonkey.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Running commands over SSH.
- URL: https://snubmonkey.com/running-commands-over-ssh/
- Published: 2021-10-29T15:52:00.000Z
- Updated: 2025-01-30T07:04:36.000Z
- Author: yannick Coffi
- Tags: how to 🪄, SSH 🕳, linux 🐧

Have you ever wanted to send a command or a script to a remote computer without ever actually logging in to the shell? - well, **SSH** (Secure Shell) is your friend.   
  
Let's say you would like to check your server process activity by using the "top" command, you would mostly perform the following steps in the Terminal:

1. 👻 Run the command `ssh username@host` to log in to the system.
2. 👺 When prompted, run `top` to view process activity on the remote system.
3. 💀 Exit `top` and back to the remote command line.
4. 🎃 `exit` to close the session.

This is the standard behavior and is good for performing system management tasks that take more than just a few steps, but sometimes you might only need to log in and run a single specific command or script and then exit so you no longer maintain a connection with the remote server.

Let's KISS (Keep this Short and Sweet) with a few examples. 👹 ☠️

**Get the remote server kernel version and Linux distro names:**

```zsh
$ ssh -p 5111 gemini@192.168.1.111 lsb_release -a

gemini@192.168.1.111's password: 
Distributor ID:	Ubuntu
Description:	X xxxx.xxx.xx
Release:	xxxx.xxx.xx
Codename:	xxxxxxxxx
```

**Get the remote server date & time:**

```zsh
$ ssh -p 5111 gemini@192.168.1.111 date

gemini@192.168.1.111's password: 
Fri 22 Oct 2021 01:58:54 PM UTC
```

**Get the remote server date +%\[format-option\]**

```zsh
$ ssh -p 5111 gemini@192.168.1.111 date +"%d-%m-%y"

gemini@192.168.1.111's password: 
22-10-21
```

**Get the remote server disk space usage:**

```zsh
$ ssh -p 5111 gemini@192.168.1.111 'df -h'

gemini@192.168.1.111's password: 
Filesystem                         Size  Used Avail Use% Mounted on
udev                               122.9G   0120.9G   0% /dev
tmpfs                              584M  8.2M  576M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   18G  9.6G  7.4G  57% /
tmpfs                              2.9G   12K  2.9G   1% /dev/shm
tmpfs                              5.0M  4.0K  5.0M   1% /run/lock
tmpfs                              2.9G     0  2.9G   0% /sys/fs/cgroup
/dev/sda2                          976M  203M  707M  23% /boot
tmpfs                              584M  4.0K  584M   1% /run/user/1000
```

**Sending multiple commands:**

```zsh
$ ssh -p 5111 gemini@192.168.1.111 'ls -l; ps -aux; whoami'

gemini@192.168.1.111's password: 

....
..
.
```

###   
\-t Option  
  
This **option** forces pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services, **interaction**. Multiple **\-t** options force tty allocation, even if **ssh** has no local tty.   
  
Below, we want to get the remote server number of ban IPs on crowdsec using the pipe `|` redirection.

```zsh
$ ssh -p 5111 gemini@192.168.1.111 -t 'sudo cscli decisions list | grep ban | wc -l'

Password: 
Verification code: 
[sudo] password for snubmonkey: 
16
Connection to 192.168.1.199 closed.
```

Here, we want to interactively monitor the server's processes in real-time.

```zsh
$ ssh -p 5111 gemini@192.168.1.111 -t 'htop'
```

  
Without the `-t` you will get an error that reads as:  
TERM environment variable not set or Error opening terminal: unknown.

  
Thank you for stepping by and Happy 2021 Halloween! 🎃