summaryrefslogtreecommitdiffstats
path: root/Dozentenmodul/src/Jsch/scriptExecutor.java
blob: 0ded43b8d2b52674ab8c6e38bba65a6e3258b6e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package Jsch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class scriptExecutor {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		executeScript();
	}
	
	public static void executeScript()
	{
		JSch j=new JSch();
		
		try {
			Session se=j.getSession("root", "141.79.128.121", 22);
			
			Properties config = new Properties();
	        config.setProperty("StrictHostKeyChecking", "no");
	        se.setConfig(config);
	        se.setPassword("!N4ye,04u.");
	        se.connect();
			ChannelExec ch=(ChannelExec) se.openChannel("exec");
			InputStream is=ch.getInputStream();
			
			ch.setCommand("sh /home/openslx/hello.sh 'test'");
			//ch.setCommand("ls");
			ch.connect();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	        String line;
	        int index = 0;

	        while ((line = reader.readLine()) != null)
	        {
	            System.out.println(++index + " : " + line);
	        }
	        
	        ch.disconnect();
	        se.disconnect();

	        System.out.println("Done!");
		} catch (JSchException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}