I’ve grown accustomed to being able to run a quick command on my Mac or Linux machines to query cht.sh . In fact, when I’m in a tmux session, I have a keybind that launches a tmux pop-up window allowing me to query cht.sh directly.

I have missed this on Windows, so I wrote a quick PowerShell function that utilizes Invoke-RestMethod to access the cheat.sh API.

The Code

function chtsh {
    param(
        [Parameter (Mandatory = $true)] [String]$command
    )

    if (-not $command) {
        $command = Read-Host "Enter command to search"
    }

    $command = $command.replace(' ', '+')

    try {
        Invoke-RestMethod -Uri "https://cht.sh/$command"
    } catch {
        Write-Host "Error fetching cheat sheet. Check your internet connection." -ForegroundColor Red
    }
}
function chtsh {
	param(
	[Parameter (Mandatory = $true)] [String]$command
	)
	$command = $command.replace(' ', '+')
	curl cht.sh/$command
}

How It works

  • Accepts a mandatory parameter command
    • Ex: chtsh ssh
    • If the parameter is not supplied, it will prompt for it
  • Replaces all instances of spaces with + characters, for proper url form.
  • Finally, we call Invoke-RestMethold on the URL formed from the query and the cht.sh URL.

We could also call curl here as it has been available on all standard builds of Windows for a while, but this is more the “PowerShell” way.

But why?

I can never remember how to specifiy a port with ssh and with scp as for whatever reason, they’re two different flags, so I use this for looking that up all the time.

chtsh ssh

# ssh
# OpenSSH SSH client (remote login program)

# SSH in via PEM file, which normally needs 0600 permissions.
ssh -i /path/to/file.pem user@example.com

# Connect through a non-standard port. It's recommended not to use the default
# port of 22, as it is so often targeted, due to it being so commonplace.
ssh -p 2222 user@example.com

# Connect and forward the authentication agent.
ssh -A user@example.com

# Execute a command on a remote server.
ssh -t user@example.com 'the-remote-command'

# Tunnel an X session over SSH, via X11 Forwarding.
ssh -X user@example.com

# Redirect traffic with a tunnel between local host (port 8080) and a remote
# host (remote.example.com:5000) through a proxy (personal.server.com).
ssh -f -L 8080:remote.example.com:5000 user@personal.server.com -N

# Launch a specific X application over SSH.
ssh -X -t user@example.com 'chromium-browser'

# Create a SOCKS proxy on localhost and port 9999.
ssh -D 9999 user@example.com

# Connect to server, but allow for X11 forwarding, while also using Gzip
# compression (can be much faster; YMMV), and using the `blowfish` encryption.
# For more information, see: http://unix.stackexchange.com/q/12755/44856
ssh -XCc blowfish user@example.com

# Copy files and directories, via SSH, from remote host to the current working
# directory, with Gzip compression. An option for when `rsync` isn't available.
#
# This works by creating (not temporary!) a remote Tar archive, then piping its
# output to a local Tar process, which then extracts it to STDOUT.
ssh user@example.com 'tar -C /var/www/Shared/ zcf - asset1 asset2' | tar zxf -

# Explicitly specify a key for connection. Useful if you have too many
# authentication failures for a given username.
ssh -i some_id_rsa -o IdentitiesOnly=yes them@there:/path/

# Temporarily disable `pubkey` authentication for this instance.
ssh -o PubkeyAuthentication=no username@hostname.com

# Mount a remote directory or filesystem, through SSH, to a local mount point.
# Install SSHFS from: https://github.com/libfuse/sshfs
sshfs name@server:/path/to/folder /path/to/mount/point

# EMACS can read files through SSH. Below, is a link to related documentation.
#
#   http://www.gnu.org/software/emacs/manual/html_node/emacs/Remote-Files.html
#
emacs /ssh:name@server:/path/to/file

# Get help for SSH escape sequences. Useful for terminating unresponsive
# sessions. The default escape character is ~ (tilde), escapes are only
# recognized immediately after a newline.

Or for scp

chtsh scp

# scp
# Secure copy (remote file copy program)

# Securely copies files from remote ADDR's PATH to the current-working-directory.
# By default here, port 22 is used, or whichever port is otherwise configured.
scp ADDR:PATH ./

# Using aliases (not Bash aliases) work with scp(1) as well. In this example, -
# the PATH1 of the first remote source defined as ALIAS1 is sent to PATH2 of
# the remote destination defined as ALIAS2.
scp ALIAS1:PATH1 ALIAS2:PATH2

# You can use the `-P` flag -- uppercase, unlike ssh(1) -- to determine the
# PORT, in-case it's non-standard (not 22) or not defined within an alias.
scp -P PORT ADDR:PATH ./

 cheat:scp 
# To copy a file from your local machine to a remote server:
scp <file> <user>@<host>:<dest>

# To copy a file from a remote server to your local machine:
scp <user>@<host>:<src> <dest>

# To scp a file over a SOCKS proxy on localhost and port 9999 (see ssh for tunnel setup):
scp -o "ProxyCommand nc -x 127.0.0.1:9999 -X 4 %h %p" <file> <user>@<host>:<dest>

# To scp between two remote servers from the third machine:
scp -3 <user>@<host1>:<src> <user>@<host2>:<dest>

 tldr:scp 
# scp
# Secure copy.
# Copy files between hosts using Secure Copy Protocol over SSH.
# More information: <https://man.openbsd.org/scp>.

# Copy a local file to a remote host:
scp path/to/local_file remote_host:path/to/remote_file

# Use a specific port when connecting to the remote host:
scp -P port path/to/local_file remote_host:path/to/remote_file

# Copy a file from a remote host to a local directory:
scp remote_host:path/to/remote_file path/to/local_directory

# Recursively copy the contents of a directory from a remote host to a local directory:
scp -r remote_host:path/to/remote_directory path/to/local_directory

# Copy a file between two remote hosts transferring through the local host:
scp -3 host1:path/to/remote_file host2:path/to/remote_directory

# Use a specific username when connecting to the remote host:
scp path/to/local_file remote_username@remote_host:path/to/remote_directory

# Use a specific ssh private key for authentication with the remote host:
scp -i ~/.ssh/private_key local_file remote_host:/path/remote_file

Of course, you can use this for whichever command you’d like, these are just my most common queries, regardless of which system I’m running on.

Conclusion

Admitedly I find this infintely more useful on MacOS/Linux than I do on Windows, but, never-the-less, it still holds its usefulness on Windows.

Until next time!