Programming and more

Entries for August, 2012

Simple way to get md5 from string in java

You will see a lot of crazy shit when searching for this. I you want something simple, get this function to get md5 from string in java: static String md5(String text) { try { MessageDigest m=MessageDigest.getInstance("MD5"); m.update(text.getBytes(),0,text.length()); return new BigInteger(1,m.digest()).toString(16); } catch (Exception e) { e.printStackTrace(); } return ""; } static String md5(String text) { […]

How to log cron crontab output in linux

Question: I created a backup.sh shell script and added it to my crontab to execute it daily. How do I verify whether the backup cron script job ran successfully? Also, I have several echo statements inside my backup.sh shell script. How do I save the output of my script to a log file when it […]

Create Random String in java.

Useful in many many cases. Here is the code: import java.security.SecureRandom; import java.math.BigInteger;   final class randomstring {   static private SecureRandom rnd= new SecureRandom();   static public String nextRandomString(int size) { return new BigInteger(130, rnd).toString(size); } public static void main(String args[]) { System.out.println(nextRandomString(15));   System.out.println(nextRandomString(32)); }   }import java.security.SecureRandom; import java.math.BigInteger; final class randomstring […]

Ban ip with iptables plus bash script

To ban an ip with iptables: iptables -A INPUT -s -j DROP service iptables save service iptables restartiptables -A INPUT -s -j DROP service iptables save service iptables restart if you want to do that with bash here is a simple script that asks you for theip to ban then bans it: iptables […]

How to Stop URLConnection from following redirection (Prevent Location: header).

This was very hard for me to find in Google. I almost spend 5 minutes instead of the usual 32 seconds to find the answer. So here it is:   // Disable automatic redirects for all HTTP requests HttpURLConnection.setFollowRedirects(false);   // Disable automatic redirects for a particular connection try { // Create a URLConnection object […]

How to build and sent a POST request with java

This is very useful at many times, if you want to emulate a post request from a browser. I use it all the time, it’s very useful, so i put it here for you to see:   try { // Construct data String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8"); data += "&" […]

How to remote backup Direct Admin (rsync backup)

You can never be sure about backups. If you are have a dedicated server you should know, local backup is never enough. Hard disks fail. Every hard disk will fail eventually. So having a remote backup solution is a must. There are many solutions for that problem. The first think I tried was configure da […]