Skip to content

Instantly share code, notes, and snippets.

@cjsmeele
Created October 24, 2019 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjsmeele/b022a1a0900105a69bddcfca9b2eb6d3 to your computer and use it in GitHub Desktop.
Save cjsmeele/b022a1a0900105a69bddcfca9b2eb6d3 to your computer and use it in GitHub Desktop.
Small socat wrapper for my two most common usecases
#!/usr/bin/env perl
use 5.12.0;
use warnings;
=head1 SYNOPSIS
sok [-PORT [SOCAT_FLAGS...]]
sok HOST [PORT [SOCAT_FLAGS...]]
=over 4
=item C<sok [-PORT]>
listens on that TCP port (default 1234).
=item C<sok HOST [PORT]>
connects to that host on that TCP port (default 1234).
=back
=cut
my ($l, $host, $port);
if (not @ARGV) {
$l = 1;
$port = 1234
} elsif ($ARGV[0] =~ /^-(\d+)?$/) {
$l = 1;
$port = $1 // 1234;
shift;
} else {
$host = shift;
$port = shift // 1234;
}
if ($l) {
exec "socat", @ARGV, "TCP-LISTEN:$port", "-"
} else {
exec "socat", @ARGV, "-", "TCP:$host:$port"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment