Skip to content

Instantly share code, notes, and snippets.

@antonmedv
Created June 19, 2017 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonmedv/53181dce02a876872e74cb56c9090c62 to your computer and use it in GitHub Desktop.
Save antonmedv/53181dce02a876872e74cb56c9090c62 to your computer and use it in GitHub Desktop.
Check SSH multiplexing initialization
<?php
$host = 'beta'; // WRITE YOU HOSTNAME HERE
$run = function ($cmd, $write = '') {
static $i = 0;
$i++;
$descriptors = array(
array("pipe", "r"),
array("pipe", "w"),
array("pipe", "w")
);
echo "$i:$cmd\n";
$process = proc_open($cmd, $descriptors, $pipes);
if (is_resource($process)) {
echo "$i:run '$write'\n";
fwrite($pipes[0], $write);
fclose($pipes[0]);
echo "$i:" . stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value = proc_close($process);
echo "$i:command returned $return_value\n";
} else {
echo "$i:proc_open failure\n";
}
};
$ssh = function ($args, $write) use ($run, $host) {
$run("ssh $args $host 'bash -s'", $write);
};
$generateControlPath = function () use ($host) {
$connectionData = "$host";
$tryLongestPossible = 0;
$controlPath = '';
do {
switch ($tryLongestPossible) {
case 1:
$controlPath = "~/.ssh/deployer_mux_$connectionData";
break;
case 2:
$controlPath = "~/.ssh/deployer_mux_%C";
break;
case 3:
$controlPath = "~/deployer_mux_$connectionData";
break;
case 4:
$controlPath = "~/deployer_mux_%C";
break;
case 5:
$controlPath = "~/mux_%C";
break;
case 6:
throw new Exception("The multiplexing control path is too long. Control path is: $controlPath");
default:
$controlPath = "~/.ssh/deployer_mux_$connectionData";
}
$tryLongestPossible++;
} while (strlen($controlPath) > 104); // Unix socket max length
return $controlPath;
};
$controlPath = $generateControlPath();
$options = "-o ControlMaster=auto -o ControlPersist=60 -o ControlPath=$controlPath";
$ssh("$options", 'pwd');
$run("ssh -O check $options $host 2>&1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment