Notes by Dr. Xi
Displaying notes 81 - 90
Created by Dr. Xi on March 30, 2011 13:43:05
Last update: March 30, 2011 13:45:27
Method 1 - use javap with verbose flag:
$ javap HelloWorld -verbose | head
Compiled fro...
Method 2 - use a utility class:
import java.io.*;
import java.nio.ByteBuffer;
...
According to the VM Spec , a Java class file has this structure:
ClassFile {
u4 magic;
...
Created by Dr. Xi on March 28, 2011 20:51:30
Last update: March 28, 2011 20:53:46
HTTP basic authentication is just Base64 encoded user name and password passed in as the Authorization header. So the following code works:
// encode user name and password
String credent...
However, since JDK 1.2, there's a more Java friendly way:
Authenticator.setDefault(new Authenticator() {
...
Test code:
import java.net.*;
import java.io.*;
pub...
Created by Dr. Xi on October 16, 2008 20:45:40
Last update: March 28, 2011 20:23:22
Java's built-in classes are way too complex/flexible for a simple protocol like HTTP. This is a wrapper to simplify HTTP GET and POST.
import java.io.*;
import java.net.*;
imp...
A simple test:
import java.io.*;
import java.util.*;
...
Created by Dr. Xi on March 28, 2011 15:54:20
Last update: March 28, 2011 15:54:20
From the nc man page:
The nc (or netcat) utility is used for just about anything under the sun involving TCP or UDP. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6.
To bind to port 8080:
nc -l 8080
To connect to the port above
nc localhost 8080
Type something in either window it will be echoed in the other.
To get the home page of google:
echo -e -n "GET / HTTP/1.1\r\nHost:www.google.com\...
To send email via SMTP:
nc -C localhost 25 << EOF
HELO host.example.com...
Port scan of localhost:
$ nc -z localhost 20-8080
Connection to localho...
Created by Dr. Xi on March 28, 2011 11:11:33
Last update: March 28, 2011 11:13:21
grep is a versatile command with many variations (grep, egrep, fgrep, then various implementations). It uses a regula expression (regex) pattern to filter input. But then there are basic and extended flavors of regex - leading to even more confusion. And, beware that there are lots of bad examples of regex in the wild... There are two critical questions to ask when you use grep: which grep implementation are you using? what is the flavor of the regex? Here are some examples for gnu grep v2.7:
# Find all numbers (no decimal point), basic regex... Use the -o flag to show only the matching part instead of the whole matching line: grep -o -E '\b[0-9]{2}\b' The good thing about the gnu grep is that it...
Created by Dr. Xi on December 12, 2007 22:57:22
Last update: March 25, 2011 15:05:40
1. Install windows resource kit .
2. Install srvany as a service:
C:\tools\putty>instsrv ssh_tunnel "C:\Program File...
3. Edit the following registry template to suite your environment:
Windows Registry Editor Version 5.00
[H...
4. Import the registry entry.
5. Start the service with "net start ssh_tunnel" or from the services applet.
You may choose to use a batch file for srvany (instead of plink.exe ), but you won't be able to shut down the tunnel when you stop the service.
Also, if the Windows SYSTEM user cannot see the host key for SSH connection, the tunnel cannot be established.
Created by Dr. Xi on March 24, 2011 16:30:12
Last update: March 25, 2011 08:26:52
A forward tunnel binds to a port on the ssh client side, forwards all traffic to that port to the sshd, which then forwards to the destination host and port. A reverse tunnel does the reverse: it binds to a port on the side of the sshd. Any traffic to that port on the sshd host will be forwarded to the ssh client, which then forwards to the destination host and port. The syntax is (compare that to the -L switch for forward tunnel):
ssh -R [bind_address:]port:forward_host:forward_po... where forward_host is resolved at the ssh client. By default reverse tunnel only binds to the loop back address (127.0.0.1). You must set the GatewayPorts option in /etc/ssh/sshd_config in order for it to bind to other addresses: GatewayPorts...
Created by Dr. Xi on March 24, 2011 12:11:14
Last update: March 24, 2011 12:22:03
This is the task: your client wants to know how the web application is used. That is pretty easy. A plethora of commercial tools or any of the free log analysis tools such as analog and AWStats would fit the bill. But here's the catch: they want to know not only what pages are visited by how many people and when, but also who logged in and did what. Your application is using form based authentication and therefore, everyone is anonymous in the web access log. What to do? This is a servlet filter that generates a web access log with authenticated user info that can be fed to log analysis tools such as analog and AWStats . Filter code (the output format is Apache...
Created by Dr. Xi on March 02, 2011 11:39:18
Last update: March 09, 2011 12:19:30
Some peculiarities about Java PrintWriter: PrintWriter never throws any exceptions. From JavaDoc : Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError(). When error occurs, you'll never know anything more than that it occured, because checkError returns boolean. When a character is out of the range of the character encoding of the PrintWriter, it prints a question mark (?). But this is not an error. Test code:
import java.io.*; public class TestPrintWri... Latin1 test result: java TestPrintWriter iso-8859-1 | od -bc 000000... UTF-8 test result: java TestPrintWriter utf-8 | od -bc 0000000 141... Also, the constructor throws a FileNotFoundException when you try to write to a...
Created by Dr. Xi on March 08, 2011 15:53:33
Last update: March 08, 2011 15:53:33
Use the date_format function to format a date:
mysql> select now() now;
+---------------------...