Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Wednesday, 13 June 2012

bash based netcat

Recently, i've stumbled upon machine that didn't have a nc installed.

I've needed it so badly ;) that i've decided to install manually preconfigured version and then i realized that i can try it do implement simple replacement nc with bash.

Here is the result:

$ echo -e "GET / HTTP/1.0\r\n\r\n" |  ./bashnc google.pl 80
HTTP/1.0 302 Found
Location: http://www.google.fi/
Cache-Control: private
(...)

works like a charm.

The script bashnc is ridiculously simple:

#/bin/bash
(
    cat <&3 & # 3 is original stdin
    cat >&4 & # 4 is original stdout
    wait
) 3<&0 4>&1 > /dev/tcp/$1/$2 0<&1

Works like a charm.

As a proof of soundness, i've tried to run ssh on top of this bashnc proxy:

$ ssh -oProxyCommand='bash -c "( cat <&3 & cat >&4 & wait ) 3<&0 4>&1 > /dev/tcp/%h/%p 0<&1 "' some_host uname
Linux some_host 3.2.0-2-amd64 #1 SMP Tue Mar 13 16:54:04 UTC 2012 x86_64 GNU/Linux
$ 

Thursday, 2 July 2009

make variable introspection

Ever wondered how to get value of some Makefile variable? Just invoke:
makespy Makefile MAKEVAR
makespy script below:
#!/bin/sh
original_makefile=`readlink -f $1`
name=$2

shift
shift

get_name_target=_makespy_get_${name}

(
        cat << EOF
include ${original_makefile}

${get_name_target}:
        echo \$($name)
EOF
) | make -C$(dirname $original_makefile) -s -f- ${get_name_target} "$@"

(remember about TAB character at the beginning of echo \$($name))

Usage: makespy MAKEFILENAME VARIABLENAME [makefile options...]

Prints actual value of variable in some makefile.

(works with GNU Make).

Update1 (2012-06-22): fixed a little, note about TAB