commit 27cc308f58c651273505d48355879028be5a54de
parent 2af8b902d76efc60ee9c25886cd80b66c670b158
Author: Claudio Alessi <smoppy@gmail.com>
Date: Thu, 28 Jun 2018 23:02:20 +0200
[txthole] New script
Diffstat:
2 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -4,6 +4,7 @@ Useful shell scripts.
index
-----
+* [txthole](#txthole) - Simple netcat-based pastebin server
* [iwpick](#iwpick) - Connects to a network.
* [scrapthumb](#scrapthumb) - Get random images from Tumblr.
* [acpi](#acpi) - Simple power management.
@@ -22,6 +23,24 @@ index
* [moin](#moin) - Play a random song from a YouTube playlist.
* [fetchpic](#fetchpic) - Fetch a random pic from a random blog (of a given list).
+txthole
+-------
+Store and retrieve text stream. Given the script is up and running, here's a sample usage:
+
+```
+$ echo Hello |nc your.host 2023
+echo GmKwL |nc your.host 2023
+$ echo GmKwL |nc your.host 2023
+Hello
+$
+```
+
+Of course you can also paste files:
+
+ $ cat /tmp/t |nc your.host 2023
+
+This should be enough for most use cases.
+
iwpick
------
Connects to a network. Authentication is not mandatory and only WPA2 is supported at the moment. It will works with no pain on any Linux system equipped with iwconfig, sdhcp and wpa_supplicant. Just run:
diff --git a/src/txthole.sh b/src/txthole.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+# simple pastebin server
+#
+# Sample usage (assuming ./txthole.sh is running):
+# cat /my/file |nc host 2023 # put file
+# echo "my text" |nc host 2023 # put text
+# echo paste-code |nc host 2023 # get
+
+pastes="/tmp/pastes"
+fifo="/tmp/txthole.fifo"
+port="2023"
+
+run() {
+ paste="$1"
+ label="$(basename "$paste" |cut -d. -f2)"
+
+ read s
+ p="$pastes/txthole.$s.paste"
+ if [ -f "$p" ]; then
+ cat "$p"
+ else
+ echo "echo $label |nc your.host $port"
+ fi
+}
+
+main() {
+ mkdir -p "$pastes"
+ rm -f "$fifo"
+ mkfifo "$fifo"
+ while : ; do
+ paste="$(mktemp -q "$pastes/txthole.XXXXX.paste")"
+ cat "$fifo" |tee -a "$paste" | run "$paste" | nc -4Nl "$port" > "$fifo"
+ [ "$(wc -c "$paste" |cut -d' ' -f1)" -eq 0 ] && rm -f "$paste"
+ done
+ rm -f "$fifo"
+}
+
+main