Last active
June 20, 2019 18:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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