Thursday, December 29, 2016

Java Telnet example : testing remote service state



In most of the cases in daily job, you must have get into some situation where you needed to telnet to a machine. Although telnet is not a secure way to access remote machine, but still it can be useful if all we want to know if the particular service port on particular machine is running or not(for example you want to check say on local ip address 192.168.110 on port 3389 the RDP service is running or not and there may be other cases)

Below, we will be showing up sample code in java which you can use the automate the process. This can also be used anywhere like if you are writing a service to report health of the multiple services, just a case.

import org.apache.commons.net.telnet.TelnetClient;

import java.io.IOException;
import java.net.ConnectException;

public class JavaTelnetExample {

    private static final String RDP_SERVER = "10.66.6.62.1";
    private static final int RDP_SERVER_PORT = 3389;
    private static final String HTTP_SERVER = "10.66.62.2";
    private static final int HTTP_SERVER_PORT = 80;
    // Similar to above you can test state for number of other services on number of other machines
    public static void main(String[] args) {
        System.out.println("Rdp service status on server ip : " + RDP_SERVER + " , port : " + RDP_SERVER_PORT + " = " + isServiceRunning(RDP_SERVER, RDP_SERVER_PORT));
        System.out.println("Ssh service status on server ip : " + HTTP_SERVER + " , port : " + HTTP_SERVER_PORT + " = " + isServiceRunning(HTTP_SERVER, HTTP_SERVER_PORT));

    }

    // If able to connect successfully, return true for service state else return false.        
    private static boolean isServiceRunning(String ip, int port) {
        try {
            TelnetClient telnetClient = new TelnetClient();
            telnetClient.connect(ip, port);
            return true;
        } catch (ConnectException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
    }
}

Note:
To run this code, you can download the dependencies 'Apache Commons Net' from the https://mvnrepository.com/artifact/commons-net/commons-net depending upon build tool you are using maven/gradle or jar file

Feel free to leave comment if you get any issue or any suggestion.

Sunday, June 19, 2016

2 Steps to restrict Jenkins access by IP address or Host Name



Steps to fresh jenkins installtion on on Apache Tomcat Server : http://www.automatethebox.com/2016/06/installing-jenkins-on-apache-tomcat.html

If Jenkins is installed on Tomcat Server 

  1.  Add any of the below entry in the conf\context.xml file :
    • <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127.0.0.1"/><!--Allow from Ip Address-->
    • <Valve className="org.apache.catalina.valves.RemoteAddrValve" deny="127.0.0.1"/><!--Deny from Ip Address-->
    • <Valve className="org.apache.catalina.valves.RemoteHostValve" allow="yahoo.com"/><!--Allow from a Domain-->
    • <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|202.43.25.244"/><!--Allow from multipl ip's-->
  2.  Restart the Tomcat Server
Now, when you try to access Jenkins url, you should get 403 error

If Jenkins is installed on Apache Server 

  1. Add all of the below entries in the conf\.htaccess file to allow access from ip '45.67.87.67' and '10.66.62.0/24' LAN only
    • Order Deny,Allow
    • Deny from all
    • Allow from 45.67.87.67
    • Allow from 10.66.62.0/24
  2. Restart the Apache Server

Now, when you try to access Jenkins url, you should get 403 error




Installing Jenkins on Apache Tomcat Server


First of all if you have not installed the Apache Tomcat Server, We will be going to install it on Windows following the below steps :
  • Download the Apache Server from Url :  http://www.apache.org/dyn/closer.cgi

  • Extract the Tomcat zip folder
  • Navigate to to 'conf' directory, path may be somewhat like '..\apache-tomcat-9.0.0.M8\conf' from where you have extracted the zip.
  • Open the Server.xml file and edit the PORT where you want the Tomcat to listen for the requests

  • Now start Command Prompt and go to '..\apache-tomcat-9.0.0.M8\bin' and call 'startup.bat'
  • Now start a browser and navigate to url '<your_system_ip:port_specified_in_step4>'. Example : 127.0.0.1:8080
    • You should see Tomcat Successfully installed page. Congratulation!


Now, we will be moving to next part of configuring Jenkins behind the Tomcat Server  :
  •  Download Jenkins.war file from https://jenkins.io/
  • Once downloaded, unzip and move the jenkins.war to directory '.\apache-tomcat-9.0.0.M8\webapps'
  • Restart the Tomcat. CTRL+C or call the .\apache-tomcat-9.0.0.M8\bin\shutdown.bat' and then start again.
    • Now when you navigate to 127.0.0.1/jenkins, you should see jenkins successfully installed.

Feel free to leave comments !!!

Sunday, March 20, 2016

Connecting to MySql from java with Maven

In this example we will be creating a simple maven java project to connect to the local mysql instance, without actually installing any jdbc connector and setting it to classpath (maven will take care of this :))

1. Add MySQL connector maven dependency in the pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.automatethebox</groupId>
    <artifactId>mysql-connect-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>
</project>

2. Sample java source file: Connecting to local mysql instance and listing tables;

package com.automatethebox.mysql_connect_maven;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MySqlConnectionWithMaven {

    public static void main(String[] args) {

        // Databases url (here 'information_schema' is database name)
        String databaseUrl = "jdbc:mysql://localhost/information_schema";
        String jdbcConnectorClass = "com.mysql.jdbc.Driver";
        String mysqlUser = "root";
        String myUserPass = "Admin@123";
        String query = "Select distinct(table_name) from INFORMATION_SCHEMA.TABLES";

        try {
            Class.forName(jdbcConnectorClass);
            Connection connection = DriverManager.getConnection(databaseUrl, mysqlUser, myUserPass);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                System.out.println("Table : " + tableName);
            }
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Friday, March 4, 2016

Disable Java JVM Default DNS Caching


You can disable the Java Virtual machine default DNS caching following any of the below methods
( By Default java 1.6 caches all the DNS queries ):



  • Method 1(Changes while staring up JVM)
    • Add -Dsun.net.inetaddr.ttl=0 while starting up the JVM.

  • Method 2(Changes in java config file)
    • Add/Edit the property networkaddress.cache.ttl=0 in %JRE%/lib/security/java.security file. Here JRE refers to Java Runtime Environment folder.

  • Method 3(Changes in your code)
    • Set the property in you java code as java.security.Security.setProperty("networkaddress.cache.ttl", "0" );

AWS Certified Solutions Architect Associate - AWS Introduction - Questions

All the Best !!! Show Result !! Try Again !! ×