Splunk Dev

splunk java sdk connection to splunk failed

disha
Contributor

I have just installed the splunk-sdk and build it. trying to execute basic example of "connect to Splunk"
http://dev.splunk.com/view/SP-CAAAECX

It is showing this error

Sam@SamPC /cygdrive/c/splunk-sdk-java/splunk
$ ls
com splunk.iml splunk.jar SplunkTest.class SplunkTest.java staticdocs.css

Sam@SamPC /cygdrive/c/splunk-sdk-java/splunk
$ javac SplunkTest.java

Sam@SamPC /cygdrive/c/splunk-sdk-java/splunk
$ java SplunkTest
Exception in thread "main" java.lang.RuntimeException: Unrecognized SSL message, plaintext connection?
at com.splunk.HttpService.send(HttpService.java:312)
at com.splunk.Service.send(Service.java:1110)
at com.splunk.HttpService.post(HttpService.java:210)
at com.splunk.Service.login(Service.java:950)
at com.splunk.Service.connect(Service.java:150)
at SplunkTest.main(SplunkTest.java:19)

I am very new to java. Any help please?

Tags (3)
0 Karma
1 Solution

Damien_Dallimor
Ultra Champion

Please post the source of SplunkTest.java so we can help diagnose the issue.

Furthermore , ensure that your connection credentials are correct, notably that you are using HTTPS over Port 8089 to your SplunkD Server.

As a guess, you might be trying to connect to SplunkWeb on port 8000, which is not where the REST endpoints are located.

View solution in original post

ntbahriti_splun
Splunk Employee
Splunk Employee

See a working example below that uses TLS v1.2 protocol with Splunk API version 1.4.0.
The new Java sdk no longer implements SSL v3, instead TLS v1.2 should be used (unless you enable the SSLv3 in your Java properties: $JAVA_SDK/lib/security/java.security file and comment the line #jdk.tls.disabledAlgorithms=SSLv3).

If you need to use SSL v3 you have 2 possibilities:
1. Remove the line "HttpService.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2);"
2. Keep the line above and replace TLSv1_2 by SSLv3: HttpService.setSslSecurityProtocol(SSLSecurityProtocol.SSLv3;

 import java.io.*;
 import com.splunk.*;

 public class SearchExport {
     public static void main(String[] args) throws IOException {
         if(args.length != 4) {
             System.out.println("Usage:\n\tjava SplunkConnect <username> <password> <hostname> <port>");
             System.out.println("\tE.G. java SplunkConnect admin P@55w0rd 127.0.0.1 8089\n");
             System.exit(1);
         }


         /* Overriding the static method setSslSecurityProtocol to implement the security protocol of choice */
         HttpService.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2);
         /* end comment for overriding the method setSslSecurityProtocol */

         ServiceArgs loginArgs = new ServiceArgs();
         loginArgs.setUsername(args[0]);
         loginArgs.setPassword(args[1]);
         loginArgs.setHost(args[2]);
         loginArgs.setPort(new Integer(args[3]));

         Service svc = Service.connect(loginArgs);
         JobExportArgs exportArgs = new JobExportArgs();
         exportArgs.setSearchMode(JobExportArgs.SearchMode.NORMAL);
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.print("Enter a search string then press <ENTER>: ");
         String mySearch = br.readLine();

         if(!(mySearch.trim().startsWith("|")) && !(mySearch.substring(0, 6).equalsIgnoreCase("search"))) {
             mySearch = "search " + mySearch;    
         }

         InputStream exportSearch = svc.export(mySearch, exportArgs);
         MultiResultsReaderXml resultsReader = new MultiResultsReaderXml(exportSearch);

         long counter = 0;
         for(SearchResults searchResults : resultsReader) {
             for(Event event : searchResults) {
                 System.out.println("**** Event " + ++counter + " ****");
                 for(String key : event.keySet()) {
                     System.out.println("\t" + key + ": " + event.get(key));
                 }
             }
         }
         resultsReader.close();
     }
 }

sreeash77
New Member

Hi ntbahriti,
I tried the above solution but I am getting the below given error.
Exception in thread "main" java.lang.RuntimeException: Unrecognized SSL message, plaintext connection?

Could you please help?

Regards,
Y.S.Sridhar

0 Karma

chinmayc469
Explorer

Hi,

Did you able to resolve this issue, even i am getting same error.

Thanks.

0 Karma

Damien_Dallimor
Ultra Champion

Please post the source of SplunkTest.java so we can help diagnose the issue.

Furthermore , ensure that your connection credentials are correct, notably that you are using HTTPS over Port 8089 to your SplunkD Server.

As a guess, you might be trying to connect to SplunkWeb on port 8000, which is not where the REST endpoints are located.

Damien_Dallimor
Ultra Champion

What is the error you get ?

0 Karma

disha
Contributor

I got it. Thanks but now I am not able to connect to remote machine. In SplunkTest.java I am giving this:
Args loginArgs = new Args();
loginArgs.add("username", "admin");
loginArgs.add("password", "xxxx");
loginArgs.add("host", "96.220.112.23");
loginArgs.add("port", 8089);

0 Karma

Damien_Dallimor
Ultra Champion

Why are you installing an Apache HTTP Server ??
You should install Splunk , and the REST client(Java SDK) will communicate to Splunk on port 8089 over HTTPS.

0 Karma

disha
Contributor

Thankyou for the quick reply.Yes it was port number. I was trying to connect 8000. BUT there is one important mistake was there.:) HTTP server was not running on my machine.Now I have installed Apache http server and code is working fine. Thankyou..!!!

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...