Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Sunday, May 01, 2016

Lifesaver Snippets

Universal JDBC CLI Client

I work on the Unix/Linux boxes running Java application servers regularly. One of the problems I always face is that access to the server is restricted, yet occasionally I have the need to access the most protected resources of all - the database. Without direct access to the database server, all my GUI database client tools are useless. What is available to me is only the JVM in an SSH session.

I find that, while I can't install any database tools on the servers without going through a lot of bureaucratic red tapes, I can usually make use of what is already available on the server to do my work. With only h2, the JDBC driver and some shell script, I am able to connect to the database and get the data I want. Here's how.

First, put all the jar files into the same folder. You should at least have h2 and the JDBC driver in the folder. Create a script h2shell:

#!/bin/bash

CP=.
for j in *.jar; do
  CP=$CP:$j
done

java -Xmx512m -classpath $CP org.h2.tools.Shell "$@"

Make the script executable:

chmod +x h2shell

Next, create a helper script with the database connection properties. Let's call the script mydb:

#!/bin/bash

./h2shell -url url -user username -password password

Make the script executable too:

chmod +x mydb

You can now connect to the DB by running mydb:

./mydb

telnet substitute

Due to the security policy of one of my clients, they remove the telnet program from all of their servers. telnet is one of common tools to test network connectivity. I found a crude alternative by writing some Python scripts.

import telnetlib

t = telnetlib.Telnet('some.host', port)
print t.read_eager()
t.write(message)
print t.read_all()
t.close()

Besides telnetlib, Python has a lot of other wonderful modules such as urllib, socket.

Tuesday, January 15, 2013

How to extract a range of lines from a large file?

It is not advisable to use vi to view a large file as on most system vi will create a swap file of similar size. It is better to extract the lines of interest to another file. Say you want to extract lines 20001 to 30000 from a log file. There are many ways to accomplish that and I am going to show you 2 of them here.

sed (my preferred way)
sed -n -e '20001,30000p' log.txt > /tmp/log-small.txt
-n means don't print anything unless the p command is used. -e means execute the following expression (those inside '....'). 20001,30000p means for lines 20001 to 30000 inclusive, execute the command p, which prints them to the console.

the head & tail way
head -30000 log.txt | tail -10000 > /tmp/log-small.txt

The head command will output lines 1 to 30000 and pipe to tail which will print the last 10000 lines, i.e. lines 20001 to 30000.
As you can see, the head & tail way requires some mental arithmetics and so it is not my preferred way.

Saturday, January 05, 2013

How to Get IP Address from MAC

One way to get the IP address of a remote host in the same subnet given a MAC address is by using the "arp -an" command. However, this will only work if the we have previously connected to the remote host.

With the help of nmap, we can find out the IP address. For example, the command below will scan the 10.1.1.0/24 subnet with nmap. After the scan we search through the output of arp. If the remote host that we are searching for is up, its MAC address will be displayed together with its IP address.

$ nmap -sP 192.168.0.0/24 > /dev/null
$ arp -an | grep -i "12:34:56:78:90:ab"
? (192.168.0.105) at 12:34:56:78:90:ab [ether] on wlan0

Saturday, October 27, 2012

The amazing date command

I use the Linux date command regularly. Normally I use it to format current date, e.g.
date +"%Y-%m-%d"
Yesterday, while writing a backup script, I found out that the command also understands simple English. E.g.
date +"%Y-%m-%d %H:%M:%S" --date "yesterday"
date +"%Y-%m-%d %H:%M:%S" --date "-7days"
date +"%Y-%m-%d %H:%M:%S" --date "-7days 00:00:00"
date +"%Y-%m-%d %H:%M:%S" --date "2 weeks ago"
This helps when I want a date that represents, say, 3pm yesterday.

Monday, August 31, 2009

The Linux cut command

cut is one of the lesser-known commands in the same category of tools such as grep, awk and sed. Nevertheless, I find it more convenient than the other tools in some circumstances. Let's say I have the following lines with ":" as the field delimiter:
abc:12:DEF
ghi:3a4:JKLM
opqr:56b:STUV


To print out the values in the second column, I can use the command

cut -d: -f2

where -d: specifies that the field delimiter is ":", -f2 specifes that the field to print is 2.

If the delimiter is the space character, the command will become

cut -d\ -f2

Notice there are 2 space characters after the backslash. The backslash will escape the space character that follows it.

Wednesday, January 07, 2009

Convert MDB file in Linux

I have some old, standalone Java projects which used Access MDB file as database. As I have always wanted to use an installation-free database for my application, I was looking for ways to convert the data in the MDB file to HSQLDB. I choose HSQLDB because it's simple to use and cross platform.

I started off researching with unixODBC but ended up installing odbcinst. Eventually I stumbled upon mdbtools. That solves my problem. After installating mdbtools on my Ubuntu laptop, I exported the schema

mdb-schema data.mdb > schema.sql

I edited the schema to HSQLDB syntax and created the new database in HSQLDB.

Then, I only need to do

mdb-export -I data.mdb table | sed -e "s/)$/);/" > table.sql

for each of the tables in the MDB file. The sed command is to append a ';' as mdb-export doesn't end the insert statements with ';'.

I then edit the INSERT statements and run them in HSQLDB.

This is simpler than most of the other approaches I have read on the web.