Skip to content

Instantly share code, notes, and snippets.

@jdrydn
Created October 31, 2024 12:56
Show Gist options
  • Save jdrydn/33e60d5409afc5ce7f460507351e9352 to your computer and use it in GitHub Desktop.
Save jdrydn/33e60d5409afc5ce7f460507351e9352 to your computer and use it in GitHub Desktop.
SSH-Shortcut-Script
<?php
/**
* @author: James Dryden <[email protected]>
* @license: MIT
* @link: http://jdrydn.com
*/
namespace SSH_Server;
/** @noinspection SpellCheckingInspection */
define("FAIL", " \033[0;31;49m[==]\033[0m");
/** @noinspection SpellCheckingInspection */
define("GOOD", " \033[0;32;49m[==]\033[0m");
/** @noinspection SpellCheckingInspection */
define("WARN", " \033[0;33;49m[==]\033[0m");
/** @noinspection SpellCheckingInspection */
define("TASK", " \033[0;34;49m[==]\033[0m");
/** @noinspection SpellCheckingInspection */
define("USER", " \033[1;1;49m[==]\033[0m");
function stdout()
{
fwrite(STDOUT, implode(" ", array_map(function ($v) { return print_r($v, true); }, func_get_args())) . PHP_EOL);
}
function stderr()
{
fwrite(STDERR, implode(" ", array_map(function ($v) { return print_r($v, true); }, func_get_args())) . PHP_EOL);
}
try
{
if (empty($argv[1]))
{
stderr(FAIL, "No PWD detected.");
exit(0);
}
/** @noinspection SpellCheckingInspection */
define("PWD", $argv[1]);
define("SSH_SERVER_INI", PWD . "/ssh-servers.ini");
if (!file_exists(SSH_SERVER_INI))
{
stderr(FAIL, "No ssh-servers.ini file at", PWD . ". Bye.");
exit(0);
}
if (empty($argv[2]))
{
stdout(GOOD, "ssh-servers.ini located at", PWD);
}
$servers = parse_ini_file(SSH_SERVER_INI, true);
$newServers = array(null);
foreach($servers as $name => $details)
{
$server = array_merge(
array(
"name" => $name,
"host" => null,
"user" => null,
"pass" => null,
"key" => null
),
$details
);
if (empty($server["host"]))
{
throw new \Exception("No host for '{$name}'.");
}
elseif (empty($server["user"]))
{
throw new \Exception("No user for '{$name}'.");
}
elseif (empty($server["key"]) && empty($server["pass"]))
{
throw new \Exception("No password / key for '{$name}'.");
}
if (!empty($server["key"]) && (substr($server["key"], 0, 1) !== "/") && (substr($server["key"], 0, 1) !== "~"))
{
$server["key"] = PWD . "/" . $server["key"];
}
$newServers[] = $server;
}
$servers = $newServers;
unset($newServers, $server, $name, $details);
// stdout(GOOD, PWD, $servers);
if (empty($argv[2]))
{
if (count($servers) === 2)
{
$server = $servers[1];
stdout(
GOOD, "Connecting to", $server["user"] . "@" . $server["host"], "with",
(!empty($server["key"]) ? "key located at " . $server["key"] : "password '" . $server["pass"] . "'")
);
exit(1);
}
stdout(PHP_EOL . " Where would you like to go?");
for($i = 1; $i < count($servers); $i++)
{
$padding = ((count($servers) > 10) && ($i < 10) ? " " : "");
$server = $servers[$i];
stdout(
" \033[1;1;49m[{$i}]\033[0m{$padding}",
$server["name"] . " (" . $server["user"] . "@" . $server["host"],
"with a " . (!empty($server["key"]) ? "key" : "password") . ")"
);
}
fwrite(STDOUT, PHP_EOL . "Please choose [1.." . (count($servers) - 1) . "]: ");
$selection = intval(trim(fgets(STDIN)));
if (empty($selection))
{
stderr(FAIL, "No server selected. Bye.");
exit(0);
}
elseif (($selection <= 0) || ($selection > (count($servers) - 1)))
{
stderr(FAIL, "Invalid number entered. Bye.");
exit(0);
}
$server = $servers[$selection];
stdout(
GOOD, "Connecting to", $server["user"] . "@" . $server["host"], "with",
(!empty($server["key"]) ? "key located at " . $server["key"] : "password '" . $server["pass"] . "'")
);
exit($selection);
}
else
{
if (empty($servers[$argv[2]]))
{
stderr(FAIL, "Unknown server to connect to.");
exit(1);
}
$server = $servers[$argv[2]];
stdout(
"ssh", $server["user"] . "@" . $server["host"] . (!empty($server["key"]) ? " -i '" . $server["key"] . "'" : "")
);
}
}
catch (ConfigException $e)
{
stderr(FAIL, "Invalid ssh-servers.ini:", $e->getMessage());
}
catch (\Exception $e)
{
stderr(FAIL, (string)$e);
}
exit(0);
#!/bin/bash
BASE_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
php $BASE_PATH/script.php "$(pwd)"
CODE=$?
if [ $CODE -ne 0 ]; then
CMD=$(php $BASE_PATH/script.php "$(pwd)" "$CODE")
exec $CMD
exit $?
fi
exit 1

SSH-Server

(Migrated from jdrydn/dotfiles)

A simple script to allow me to have multiple SSH server listings without clouding up my SSH config.

Example usage:

$ cd /path/to/a/working/directory
$ sshserver
 [==] ssh-servers.ini located at /Users/James/Documents/KentProjects

  Where would you like to go?
 [1]  name-of-server ([email protected] with a password)
 [2]  name-of-other-server ([email protected] with a key)
 [3]  name-of-yet-another-server ([email protected] with a key)

  Please choose [1..3]: 1
 [==] Connecting to [email protected] with password 'a1b2c3d4e5f6'
[email protected]'s password:
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-37-generic x86_64)
# root at name-of-server in ~ [20:14:23]

This requires a specific ini file named ssh-servers.ini in any generic folder containing the following:

[name-of-server]
host = some.ip.of.that.server
user = root
pass = a1b2c3d4e5f6

[name-of-other-server]
host = some.ip.of.this.server
user = some_user
key = other_server_rsa

[name-of-yet-another-server]
host = some.ip.of.the.server
user = ubuntu
key = ~/.ssh/id_rsa

There must be a host, a user field and either a pass (password) or key field, containing a password or a path to a file (respectively). If the key is missing a prefix (either ~/ or /) then the key file is relative to the current working directory.


Updates

Due to the difficulties I've been having with trying to get auto-competition working in bash and zsh, this script's usage has had to change. Because typing long names of servers (like name-of-yet-another-server) sucks.

It used to be like:

$ cd /path/to/a/working/directory
$ ssh-server live
$ cd /path/to/another/working/directory
$ ssh-server rs-web1
$ cd /path/to/yet/another/working/directory
$ ssh-server aws-database2

It's now been rewritten to be a welcoming screen. Taken from the welcome screen, obviously.

$ sshserver
 [==] ssh-servers.ini located at /Users/James/Documents/KentProjects

  Where would you like to go?
 [1]  name-of-server ([email protected] with a password)
 [2]  name-of-other-server ([email protected] with a key)
 [3]  name-of-yet-another-server ([email protected] with a key)

  Please choose [1..3]:

And when you select a number, it doesn't run the ssh process under a php process. Instead, the script continues to execute the ssh command so that when ssh exits (for whatever reason) you are returned to the shell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment