Skip to content

Instantly share code, notes, and snippets.

@jenrik
Last active June 20, 2019 18:00
Show Gist options
  • Save jenrik/39e8236323be2ee3304576114657e46e to your computer and use it in GitHub Desktop.
Save jenrik/39e8236323be2ee3304576114657e46e to your computer and use it in GitHub Desktop.
Cron script for updating ssh key from Github periodically, and logging which servers fetches keys
<?php
$github = "jenrik";
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
header("Cache-control: private, max-age=0, no-cache");
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
die("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}
if (!($stmt = $mysqli->prepare("INSERT INTO ssh_keys_requests(last_request, ip) VALUES (CURRENT_TIMESTAMP(), ?) ON DUPLICATE KEY UPDATE last_request=CURRENT_TIMESTAMP()"))) {
dir("Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error);
}
if (!$stmt->bind_param("s", $_SERVER['REMOTE_ADDR'])) {
die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
}
if (!$stmt->execute()) {
die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
}
header("Location: https://github.com/" . $github . ".keys", true, 302);
exit();
?>
#!/bin/sh
set -e
TEMP_KEYS=$(mktemp)
curl --location --output "$TEMP_KEYS" "http://ssh-keys.jener.dk/?username=$USER" 2> /dev/null
mv "$TEMP_KEYS" ~/.ssh/authorized_keys2
chmod 644 ~/.ssh/authorized_keys2
(rm ~/.ssh/authorized_keys || true) 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment