Skip to content

Instantly share code, notes, and snippets.

@se1983
se1983 / capture_linux_procs.py
Last active March 31, 2021 11:09
using async python to capture the outputs of all processes of the current user
import asyncio
import logging
import os
from dataclasses import dataclass
from typing import Iterable
from aiofile import AIOFile
logging.basicConfig(level=logging.INFO)
@se1983
se1983 / deadmans_switch.py
Last active January 22, 2021 22:22
signal.SIGALARM watching the runtime of a procedure
import asyncio
import signal
from random import randint
class Timeout(Exception):
pass
def handle_timeout(signum, frame):
@se1983
se1983 / tokio_mpsc.rs
Created December 31, 2020 10:14
most simple multi-producer-single-consumer in rust tokio
use tokio::sync::mpsc;
#[tokio::main]
pub async fn main() {
let (tx, mut rx) = mpsc::channel(32);
let tx2 = tx.clone();
tokio::spawn(async move {
tx.clone().send("sending from first handle").await;
});
@se1983
se1983 / porter.rs
Created October 25, 2020 10:11
TCP Portscanner with threadpool in rust
use std::net::{TcpStream, IpAddr, SocketAddr};
use std::time::Duration;
use threadpool::ThreadPool;
use std::sync::mpsc::{channel, Sender};
use ipnet::Ipv4Net;
use std::thread::sleep;
static NETWORK: &str = "172.111.0.0/10";
static PORT: u16 = 80;
static POOLSIZE: usize = 7000;
@se1983
se1983 / split_seperator_keep.rs
Created July 26, 2020 12:26
Split string at regex and keep seperator
use regex::Regex; // 1.1.8
fn split_keep<'a>(r: &Regex, text: &'a str) -> Vec<&'a str> {
let mut result = Vec::new();
let mut last = 0;
for (index, matched) in text.match_indices(r) {
if last != index {
result.push(&text[last..index]);
}
result.push(matched);
@se1983
se1983 / tokio_join.rs
Created July 5, 2020 08:14
Playing around with tokios join makro
use tokio::process;
use std::process::Stdio;
use std::time::Instant;
async fn sleep() -> Result<String, Box<dyn std::error::Error>> {
let ps = process::Command::new("sleep")
.stdout(Stdio::piped())
.arg("3")
.output().await?;
@se1983
se1983 / regextract.rs
Last active July 1, 2020 18:40
Extract fields from logline with regex in rust.
use regex::{Regex, Captures};
use strfmt::strfmt;
use std::collections::HashMap;
fn main() {
#[derive(Debug)]
struct LogSchema{
datetime: String,
@se1983
se1983 / log_regex.rs
Created June 30, 2020 17:40
regex loglines with rust
use regex::Regex;
fn main() {
let datetime = r"[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9]";
let host = r"([^\s]+)";
let service = r"([^\s]+)";
let message = r"(.*)";
let re = Regex::new(&format!("(?P<datetime>({d})) (?P<hostname>({h}))] (?P<service>({s})) (?P<message>({m}))",
d = datetime, h = host, s = service, m = message)).unwrap();
@se1983
se1983 / playground.rs
Last active April 19, 2020 06:39 — forked from rust-play/playground.rs
Channels between two threads
use std::thread;
use std::sync::mpsc;
fn main() {
let (tx, rx) = mpsc::channel();
let h1 = thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});
@se1983
se1983 / boxed_cacher_part_of_struct.rs
Last active January 3, 2024 12:51
Caching http requests
use ureq;
use std::collections::HashMap;
use std::time::{Instant, Duration};
pub struct Cacher {
call: Box<dyn Fn(&str) -> String>,
data: HashMap<String, (String, Instant)>,
cache_time: Duration,
}