Skip to content

Instantly share code, notes, and snippets.

Get ssh thumbprint or ssh fingerprint of every entry in known_hosts as json:

ssh-keygen -l -f known_hosts  | awk '{ print "{" "\042host\042:\042" $3 "\042" ",\042thumbprint\042:\042" $2 "\042" ",\042alg\042:\042" $4 "\042" ",\042size\042:\042" $1 "\042" "}"}' | sort | jtbl

formatted:

ssh-keygen -l -f known_hosts \
@djeikyb
djeikyb / InteropCancellationToken.cs
Last active March 1, 2024 18:54
How to cancel a long running native function — from c# — with a CancellationToken!
using System.Runtime.InteropServices;
using System.Threading;
[StructLayout(LayoutKind.Sequential)]
internal struct InteropCancellationToken
{
public InteropCancellationToken(CancellationToken ct)
{
IsCancellationRequested = () => ct.IsCancellationRequested;
}
@djeikyb
djeikyb / MelExtensions.cs
Last active December 22, 2023 01:41
Fluent methods to add scope state key-value pairs to your logs!
using Microsoft.Extensions.Logging;
public static class LogEx
{
public static ScopeStateBuilder<T> With<T>(this ILogger<T> logger, string k, object? v) => new(logger, k, v);
public sealed class ScopeStateBuilder<T> : ILogger<T>
{
private readonly ILogger<T> _logger;
private readonly IDictionary<string, object?> _state = new Dictionary<string, object?>();
@djeikyb
djeikyb / HttpSpy.cs
Last active November 15, 2023 21:26
Spies on http interactions and logs the details
// License is 0BSD
// https://gist.github.com/djeikyb/b7d7b7afdc651396b90a5cb2279b58b9
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
@djeikyb
djeikyb / Program.cs
Created October 16, 2023 23:26
Rabbitmq, MassTransit, & the dotnet generic host in a cli program.
using System.ComponentModel.DataAnnotations;
using System.Net.Mime;
using System.Text;
using MassTransit;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@djeikyb
djeikyb / flatten
Last active October 10, 2023 21:04
Turns an appsettings.json into flat json of key-value pairs, or, shell friendly list of plain key-value pairs
#!/bin/sh
toEnvVars() {
toFlatJson "$1" | jq --raw-output '. | to_entries[] | "\(.key)=\(.value)"'
}
toFlatJson() {
# thank you jeff mercado https://stackoverflow.com/a/42303836/659715
jq --arg delim '__' 'reduce (tostream|select(length==2)) as $i ({};
.[[$i[0][]|tostring]|join($delim)] = $i[1]
public static (X509Certificate2, RSA) Read(string publicCert, string privateKeyPemTaggedOrNot)
{
var rsa = RSA.Create();
if (privateKeyPemTaggedOrNot.StartsWith("-----BEGIN RSA PRIVATE KEY-----"))
{
rsa.ImportFromPem(privateKeyPemTaggedOrNot);
}
else
{
var key = Convert.FromBase64String(privateKeyPemTaggedOrNot);
@djeikyb
djeikyb / 20220504.gitconfig
Created May 4, 2023 19:43
current gitconfig
[user]
name = Jacob
email = jace42@thegoogles.mail
[alias]
unstage = restore --staged
changelog = log --format='* %s%n%w(,4,4)%+b'
pick = cherry-pick
ll = log --graph --pretty=format:'%C(auto)%h %ad %d %s %C(white)[%aN]' --date='format:%y·%b·%d' --use-mailmap
l = ll --remotes='origin/head' origin/head~5..HEAD
@djeikyb
djeikyb / docker-goto
Last active December 22, 2022 17:49
custom docker cli command to interactively connect to a container
#!/bin/bash
# official docs:
#
# https://docs.docker.com/engine/extend/cli_plugins/
#
#
# example plugin:
#
# https://github.com/SvenDowideit/docker-cli-plugins/blob/master/docker-env
@djeikyb
djeikyb / GitlabClient.cs
Last active December 9, 2022 19:13
Gitlab client with only a couple label endpoints implemented
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
class GitlabClient
{
private readonly HttpClient _client;
public GitlabClient(HttpClient client, string token)