Use java to log in to remote LINUX and implement various operations on the service

When accessing linux, you first need to use the tool jar package: ganymed-ssh2

Log in to the remote server:

public boolean login(){
	//Create a remote connection, the default connection port is 22, if you do not use the default, you can use the method
	//new Connection(ip, port) creates an object
	Connection conn = new Connection(ip);        
	try {
		//connect to remote server
		conn.connect();
		//Log in with username and password
        return conn.authenticateWithPassword(usr, psword);
	} catch (IOException e) {   
		System.err.printf("User %s password %s failed to log in to server %s!", usr, psword, ip);
		e.printStackTrace ();
  }
  return false;
}
/**
  * Upload local files to the server directory
  * @param conn Connection object
  * @param fileName local file
  * @param remotePath server directory
  */
public void putFile(Connection conn, String fileName, String remotePath){
	SCPClient sc = new SCPClient(conn);
	try {
		// Put the local file in the specified directory of the remote server, the default file mode is 0600, which is rw,
		//To change the mode, you can call the method put(fileName, remotePath, mode), the mode must be 4 digits and start with 0
		sc.put(fileName, remotePath);
	} catch (IOException e) {
		e.printStackTrace ();
	}
}
/**
  * Download server files to local directory
  * @param fileName server file
  * @param localPath local directory
  */
public void copyFile(Connection conn, String fileName,String localPath){
	SCPClient sc = new SCPClient(conn);
	try {
		sc.get(fileName, localPath);
	} catch (IOException e) {
		e.printStackTrace ();
	}
}
/**
 * On the remote LINUX server, in the specified directory, get each attribute of the file
 * @param[in] conn Conncetion object
 * @param[in] remotePath The specified directory of the remote host
 */
public void getFileProperties(Conncetion conn, String remotePath){
	try {
		SFTPv3Client sft = new SFTPv3Client(conn);
		Vector<?> v = sft.ls(remotePath);
       
		for(int i=0;i<v.size();i++){
			SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
			s = (SFTPv3DirectoryEntry) v.get(i);
			//file name
			String filename = s.filename;
			// the size of the file
			Long fileSize = s.attributes.size;
		}
			
		sft.close();
       
	} catch (Exception e1) {
		e1.printStackTrace();
	}
}
/**
 * On the remote LINUX server, in the specified directory, delete the specified file
 * @param[in] fileName filename
 * @param[in] remotePath The specified directory of the remote host
 * @return
 */
public void delFile(String remotePath, String fileName){
	try {
		SFTPv3Client sft = new SFTPv3Client(conn);
		//Get the list of files in the remote directory
		Vector<?> v = sft.ls(remotePath);
   
		for(int i=0;i<v.size();i++){
			SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
			s = (SFTPv3DirectoryEntry) v.get(i);
			/ / Determine whether the file in the list has the same name as the specified file
			if(s.filename.equals(fileName)){
				//rm() method, must be the absolute path of the file + the file name
				sft.rm(remotePath + s.filename);
			}
		sft.close();
	} catch (Exception e1) {
		e1.printStackTrace();
	}
}

/**
 * Execute the script
 * @param conn Connection object
 * @param cmds Commands to execute on linux
 */
public int exec(Connection conn, String cmds){
	InputStream stdOut = null;
	InputStream stdErr = null;
	int ret = -1;
	try {
		//Open a new session in the connection
		Session session = conn.openSession();
		//Execute linux commands on the remote server
		session.execCommand(cmds);
		// output after command execution ends
		stdOut = new StreamGobbler(session.getStdout());
		//Error after command execution ends
		stdErr = new StreamGobbler(session.getStderr());
		//Wait for the command to finish executing
		session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
	   	//Get the status after the command execution ends
		ret = session.getExitStatus();
		
		conn.close();
	}catch(Exception e){
		 e.printStackTrace ();
	}

	return ret;
}






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325581799&siteId=291194637