Find bash script location


This can be useful if you need to cd to the script location from within crontab. cd to the script location from within the script itself !

For the relative path (i.e. the direct equivalent of Windows’ 


echo “$MY_PATH”[/crayon]
For the absolute, normalized path:

” # relative

” # absolutized and normalized
if [ -z “$MY_PATH” ] ; then
# error; for some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
exit 1 # fail
echo “$MY_PATH”[/crayon]

733 total views, no views today

Track a laptop IP with BASH



Or you can use this.

586 total views, no views today

Encrypting and Compiling BASH code


If you are looking to protect your BASH script from prying eyes, try encrypting it and then compiling it.

1st: Encrypting
1] obfsh – You will want this tool from
obfsh is quite flexible and can obfuscate any type of shell script. The
obfuscated script version is printed to stdo. The original script is not

Using obfsh options cleverly, one may fool more then just a casual intruder
or snooper, and certainly make understanding of the obfuscated script harder
and more time consuming.

Read some of the options first.

You can vary the way it works.

So to encrypt it, take this tool and make your script hard to read.

2] ?(Another method is out there. Just have to find it again.)

2nd: Compile
Take your newly encrypted file from step 1 and use it here with the tool called shc.

Check your distro or goto and download shc (shc-3.8.9.tgz version of this posting).
Edit: After trying to get this to work smoothly, and failing to get “make” to work, I then tested version 3.8.7. This one ran “make” correctly and “make install”. I suggest using it instead.

Use -r, this will relax security to create a redistributable binary that executes on other systems that runs the same operating system as the one on which it was compiled.

3rd (optional): Specifying Expiration Date for Your Shell Script
This makes it so the compiled bash script will not run after the set date and will display a message instead.

If you get the following error messages upon give the shc command:

then install the following packages:

Last but not the least. There is no guarantee that this utility will provide you a very strong security protection. Experienced users or hackers who have sufficient knowledge about “gdb” or other debugger tools can decrypt your shell script(when using shc alone). Although it does provide a good starting point to encrypt (hide) shell scripts from “regular” users if you are a system administrator.

6,048 total views, 2 views today

Bash Socket Programming



You can connect to a socket using Bash by using exec and redirecting to and from the pseudo-path /dev/tcp/<hostname>/<port> or /dev/udp/<hostname>/<port>. For instance, to connect to your localhost SSH port using TCP:

Then, use cat and echo to read or write to the socket. Here is an example read:

Notice that there is no such file as /dev/tcp or /dev/udp. Bash interprets the pseudo-path.

As another example, maybe you want to download a webpage:

Finally, let’s say you wanted to connect to an IRC server. Here is an example:

Sources Advanced Bash-Scripting Guide – Chapter 29 Bash socket programming with /dev/tcp

1,121 total views, no views today