Monday, July 8, 2013

Sending and Receiving Data use Java socket programming

//java socket client example
import java.io.*;
import java.net.*;

public class socket_client
{
    public static void main(String[] args) throws IOException
    {
        Socket s = new Socket();
    String host = "dev.easy-conn.com";
    PrintWriter s_out = null;
    BufferedReader s_in = null;
       
        try
        {
        s.connect(new InetSocketAddress(host , 6557));
        System.out.println("Connected");
           
        //writer for socket
            s_out = new PrintWriter( s.getOutputStream(), true);
            //reader for socket
            s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        }
       
        //Host not found
        catch (UnknownHostException e)
        {
            System.err.println("Don't know about host : " + host);
            System.exit(1);
        }
       
        //Send message to server
    String message = "GET hosts\r\n\r\n";
    s_out.println( message );
           
    System.out.println("Message send");
       
    //Get response from server
    String response;
    while ((response = s_in.readLine()) != null)
    {
        System.out.println( response );
    }
    }
}

Thursday, July 4, 2013

Bind livestatus unix socket to TCP socket use Python and twisted

from twisted.internet import protocol, reactor
import socket,os

socket_path = "/var/lib/nagios3/rw/live"

class Echo(protocol.Protocol):
    def dataReceived(self, data):
 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

 s.connect(socket_path)

 s.send(data)
 s.shutdown(socket.SHUT_WR)
 answer = s.recv(100000000)
        self.transport.write(answer)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

reactor.listenTCP(6557, EchoFactory())
reactor.run()

Monday, July 1, 2013

One liner to download Oracle JDK with wget

wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2Fjdk-7u17-download-1501626.html;" http://download.oracle.com/otn-pub/java/jdk/7u25-b15/jdk-7u25-linux-i586.tar.gz --no-check-certificate -O jdk-7u25-linux-i586.tar.gz