Home > Back-end >  Java SSH su command outputs of Linux have no reaction, strives for the great god
Java SSH su command outputs of Linux have no reaction, strives for the great god

Time:09-23

/* 
* To change this license header, choose license Headers in the Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
Package demo;

import java.io.IOException;
The import ch. Ethz. An ssh2. Connection;
The import ch. Ethz. An ssh2. Session;
The import ch. Ethz. An ssh2. StreamGobbler;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/*
@ the author: Liu Yuanyuan
Purpose: to test connecting remote computer and execute Linux command
*/

Public class Linux {

Public static void main (String [] args) {

String hostname="194.168.7.61";
String username="root";
String password="root";
//specify the IP address of the connection between a host
The Connection conn=new Connection (hostname);
The Session SSH=null;
Try {
//connected to the host
Conn. The connect ();
//use the user name and password check
Boolean isconn=conn. AuthenticateWithPassword (username, password);
if (! Isconn)
{
System. The out. Println (" the user name or password is not correct ");
}
The else
{
System. The out. Println (" already connected OK ");
SSH=conn. OpenSession ();
SSH. ExecCommand (" su - oracle; Up sqlplus/nolog ");
//SSH. ExecCommand (" perl/root/hello. Pl ");
//only allows you to use a line command, namely SSH object can be used only once execCommand this way,
//used multiple times will be abnormal
//use multiple command with a semicolon
//SSH. ExecCommand (" CD/root; Sh hello. Sh ");

//all the text on the Terminal screen printed
InputStream is=new StreamGobbler (SSH getStdout ());
BufferedReader BRS=new BufferedReader (new InputStreamReader (is));
While (true)
{
The String line=BRS. ReadLine ();
If (line==null)
{
break;
}
System. The out. Println (line);
}
}

} the catch (IOException e)
{
e.printStackTrace();
} the finally
{
//connect Session and Connection object needs to be shut down
SSH. The close ();
conn.close();
}

}

}
  • Related