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’ 

%~dp0

):


MY_PATH="dirname \"$0\""
echo "$MY_PATH"

For the absolute, normalized path:


MY_PATH="dirname \"$0\""              # relative
MY_PATH="( cd \"$MY_PATH\" && pwd )"  # 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
fi
echo "$MY_PATH"

 1,060 total views

Use
http://ifconfig.me/all/json
https://github.com/dominictarr/JSON.sh

#!/bin/bash

# Grab external IP and copy to Dropbox or other remote locations.
curl -s http://ifconfig.me/all/json | /home/seth/scripts/system/JSON.sh -b | grep -i "ip_addr" | cut -d "	" -f 2 | cut -d "\"" -f 2 > /home/seth/Dropbox/track_data/$HOSTNAME.ip

Or you can use this.

lynx -dump -hiddenlinks=ignore -nolist http://checkip.dyndns.org:8245/ | awk '{ print $4 }' | sed '/^$/d; s/^[ ]*//g; s/[ ]*$//g'

 894 total views

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 http://www.comp.eonworks.com/scripts/obfuscate_shell_script-20011012.html
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
modified.

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.

obfsh -h

You can vary the way it works.

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

obfsh -i -f script.sh > newfile_script.sh

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 http://www.datsi.fi.upm.es/~frosal/ 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. http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz

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.

shc -r -f newfile_script.sh

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.

shc -r -e 01/01/2015 -m "Expired on New Years Day." -f newfile_script.sh

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

# shc -f cleanlog.sh
cleanlog.sh.x.c:108:22: error: sys/stat.h: No such file or directory
cleanlog.sh.x.c:109:23: error: sys/types.h: No such file or directory
cleanlog.sh.x.c:111:19: error: errno.h: No such file or directory
cleanlog.sh.x.c:112:19: error: stdio.h: No such file or directory
cleanlog.sh.x.c:113:20: error: stdlib.h: No such file or directory
cleanlog.sh.x.c:114:20: error: string.h: No such file or directory
cleanlog.sh.x.c:115:18: error: time.h: No such file or directory
cleanlog.sh.x.c:116:20: error: unistd.h: No such file or directory
cleanlog.sh.x.c: In function 'key_with_file':
cleanlog.sh.x.c:178: error: array type has incomplete element type
cleanlog.sh.x.c:179: error: array type has incomplete element type
cleanlog.sh.x.c:185: warning: incompatible implicit declaration of built-in function 'memset'
.....................
.....................

then install the following packages:

# apt-get install gcc libc6-dev

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,672 total views

From: http://hacktux.com/bash/socket


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:

exec 3<>/dev/tcp/localhost/22

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

cat <&3
SSH-2.0-OpenSSH_5.6

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

ls -l /dev/tcp
ls: cannot access /dev/tcp: No such file or directory

ls -l /dev/udp
ls: cannot access /dev/udp: No such file or directory

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

exec 3<>/dev/tcp/www.fedora.org/80
echo -e "GET /\n" >&3
cat <&3

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

#!/bin/bash

##########################################################
# Config

NICK="mynick"
SERVER="irc.freenode.net"
PORT=6667
CHANNEL="#bashirc"

##########################################################
# Main

exec 3<>/dev/tcp/${SERVER}/${PORT}
echo "NICK ${NICK}" >&3
echo "USER ${NICK} 8 * : ${NICK}" >&3
echo "JOIN ${CHANNEL}" >&3
cat <&3

exit $?

Sources

tldp.org: Advanced Bash-Scripting Guide – Chapter 29
thesmithfam.org: Bash socket programming with /dev/tcp


 1,599 total views