Java connect to remote port

Remote JMX Connection example using JConsole

The process of remote JMX connection is quite different from the local JMX connection. There are 4 ways in which you can do JConsole remote monitoring.

  1. SSL and authentication both disabled.
  2. SSL enabled and authentication disabled.
  3. Authentication enabled and SSL disabled.
  4. Both SSL and authentication enabled.

Each way of remote connection has its own advantages and disadvantages. You can choose the appropriate way based on the ease with which you want to connect and how secure you want your connection to be. In this tutorial, we will study all the 4 different types of JMX remote connection examples and the pros and cons of each. We will also learn how to connect JConsole to Tomcat, JBoss etc in subsequent tutorials.

What is JMX RMI Port and is there any default port?

The JMX RMI port is the port number on which the Java process that you want to monitor listens for incoming connections from the client (Remote management applications) such as JConsole. For monitoring a local Java process, there is no need to specify the JMX RMI port number. But for monitoring a remote process, you need to assign an RMI port number to your Java application.

Читайте также:  Postgresql создание базы python

There is no default JMX port number due to security and other reasons. If you are starting your application without providing the JMX RMI port number, you will not be able to establish a remote connection because without the port number the JMX agent will not start an RMI connector in your host machine’s JVM.

How do I define the JMX port number?

You have to define the JMX port number by setting the system property “com.sun.management.jmxremote.port”. Be careful not to assign TCP ports that are already in use. If you try to use a port that is already in use, your Java application will fail at startup and throw an ExportException. For development purpose you can pick any port between 49152-65535, as these ports are safer compared to the reserved ports.

For the production environment, be careful to pick up a port between 49152-65535. These ports can be dynamically assigned by the OS to running programs based on their availability. If your application is stopped for a while and in the mean time the JMX port number is assigned to some other service by the OS. Your application will not start at all until the client changes the port number or the port is freed by the current application that is using it.

JConsole – Connect to remote JMX Agent without authentication and SSL.

This is the easiest way but also the least safe method to establish a remote JMX connection. You should use this type of connection when you are in the development phase or monitoring your application over a private network. You need to set the following 4 properties to establish the remote JMX Connection.

Читайте также:  Задания по питону программирование

1. com.sun.management.jmxremote.ssl – The default value is true, which enables Secure Socket Layer (SSL) communication between the client and the JMX agent. We will set the value to false to disable SSL for this example.

2. com.sun.management.jmxremote.authenticate – The default value is true, which enables authentication using username and password. For this example, we will set this to false.

3. com.sun.management.jmxremote.port – As mentioned above, this property is used to set the JMX RMI port for communication.

4. java.rmi.server.hostname – The IP address of the host machine where your Java application is running. This default value is the IP address of the local host. If you only have a single network interface, then you need not set this property. But in case, you have multiple active network adapters. Then you have to specify this value as the Java program can pick IP address of any one of the network adapter.

Starting the Java Application.

We will create a very simple Java program, and try to connect it from a remote machine. The main thread on startup will sleep for 10 minutes.

For Eclipse IDE , we will add the following arguments in the Run Configuration ->Arguments -> VM Arguments.

“-Dcom.sun.management.jmxremote.port= -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=IP-Add of Host>” and run the program.

VM Arguments for running JConsole on remote machine without authentication/ssl.

For running the program from command line .

java -Dcom.sun.management.jmxremote.port= -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=IP-Add of Host> Fully Qualified Class Name.

Command Line option for running application to support remote JConsole connection.

Starting JConsole.

Open JConsole on the remote machine. Select the remote Process radio button. Enter the IP Adress of the host and the JMX port in the following format. : .

Establishing Remote JMX Connection example using JConsole.

Since we are not using authentication in this remote connection example, you can leave those two fields empty.

JConsole remote connection without SSL.

As we disabled the SSL protocol using the system property, we will get the above warning “Secure Connection failed. Try Insecurely”. Click on Insecure to establish the connection.

Remote connection established.

Finally, we are connected to the Host using a remote machine.

JConsole – Connect to remote JMX Agent with authentication.

This method safer than not using any password authentication. The passwords are stored in clear text on the host on which your application is running. Since Secure socket layer (SSL) is not used, an intruder can listen to the non-encrypted communication between the server and client and get the username and password. This is not recommended for production environment.

To enable authentication, you need to create access and password files. The access file contains the username and the role assigned to that user. The two roles are read only and read/write role. These read and read/write role are in relation to the operations that can be performed on the MBeans. With read only role, you can only call a getter method on an attribute of a MBean. But with read/write role, you can perform a set operation on an attribute or invoke an operation defined by the MBean interface. The password file contains the username and its password.

jmxremote.access

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

admin readwrite
guest readonly

Источник

Make an SSH connection with a remote server using Java

We can build an ssh connection to any remote server by following some simple steps mentioned below.

Adding dependencies

We will use the JSch library to connect with the remote server. So, we will add its dependency here –

Now, we can connect with the remote server with Jsch using two methods –

Let’s see each of them one by one.

Making an SSH connection with Jsch using a password

So, let us see what we need to connect to a remote server. Well, there are only a few things-

  • Remote server IP
  • Remote server port ( The port on which you want to connect on, say, 33000 )
  • Username
  • Password

Below is the sample code to connect with your server using the password –

import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.JSchException; public class Codekru < public static void main(String[] args) < try < JSch jsch = new JSch(); String username = "codekru"; // your username String host = "127.0.0.1"; // your remote server address int port = 33000; // your remote server port String password = "root"; // your username's password Session session = jsch.getSession(username, host, port); session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.setTimeout(15000); session.connect(); >catch (JSchException e) < e.printStackTrace(); >> >

This is it if you want to make an ssh connection using the password. Now, let’s move on to making a connection using a key.

Making an SSH connection using key

So, We will have to store our key someplace in our machine. We kept it at “/Users/codekru/key”. Now, let’s use this key and make an ssh connection.

import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.JSchException; public class Codekru < public static void main(String[] args) < try < JSch jsch = new JSch(); String user = "codekru"; // your username String host = "127.0.0.1"; // your remote server address int port = 33000; // your remote server port String yourKeyName = "/Users/codekru/key"; jsch.addIdentity(yourKeyName); Session session = jsch.getSession(user, host, port); session.setConfig("StrictHostKeyChecking", "no"); session.setTimeout(15000); session.connect(); >catch (JSchException e) < e.printStackTrace(); >> >

Using this, you can establish an ssh connection with Jsch using a key.

What if we used an invalid key? What exception we will get?

We will get an invalid privatekey JSchException.

com.jcraft.jsch.JSchException: invalid privatekey: [[email protected] at com.jcraft.jsch.KeyPair.load(KeyPair.java:948) at com.jcraft.jsch.KeyPair.load(KeyPair.java:561) at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40) at com.jcraft.jsch.JSch.addIdentity(JSch.java:406) at com.jcraft.jsch.JSch.addIdentity(JSch.java:366)
What if we used the wrong password?

Here, we will get an Auth fail JSchException.

com.jcraft.jsch.JSchException: Auth fail at com.jcraft.jsch.Session.connect(Session.java:519) at com.jcraft.jsch.Session.connect(Session.java:183)
Related Articles –

I hope you liked this article. If you found anything wrong or have any doubts, please feel free to write us in the comments or mail us at [email protected].

Источник

Java connect to remote port

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью