2017-03-24 15 views
1

Jcraft Jsch 라이브러리를 사용하여 Java 응용 프로그램을 통해 라우터를 관리하려고합니다.JSh를 사용하여 SSH를 통해 실행되는 명령에 입력/부속 명령 제공

TFTP 서버를 통해 라우터 구성을 보내려고합니다. PuTTY와 작동하기 때문에 문제는 Java 코드에 있습니다.

이 내 자바 코드 :

int port=22; 
String name ="R1"; 
String ip ="192.168.18.100"; 
String password ="root"; 

JSch jsch = new JSch(); 
Session session = jsch.getSession(name, ip, port); 
session.setPassword(password); 
session.setConfig("StrictHostKeyChecking", "no"); 
System.out.println("Establishing Connection..."); 
session.connect(); 
System.out.println("Connection established."); 

ChannelExec channelExec = (ChannelExec)session.openChannel("exec"); 

InputStream in = channelExec.getInputStream(); 
channelExec.setCommand("enable"); 

channelExec.setCommand("copy run tftp : "); 
//Setting the ip of TFTP server 
channelExec.setCommand("192.168.50.1 : "); 
// Setting the name of file 
channelExec.setCommand("Config.txt "); 

channelExec.connect(); 

BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
String line; 
int index = 0; 
StringBuilder sb = new StringBuilder(); 
while ((line = reader.readLine()) != null) 
{ 
    System.out.println(line); 
} 
session.disconnect(); 

는 내가 그 연속적인 명령을 실행할 수있는

라인이 잘못된 자동 명령 '192.168.50.1'

는 문제가있다 얻을 .

답변

1

ChannelExec.setCommand 번을 여러 번 호출해도 아무 효과가 없습니다.

그럼에도 불구하고 192.168.50.1 :Config.txt은 명령이 아니지만 copy run tftp : 명령에 대한 입력이 아닌 것으로 생각됩니다.

그런 경우 명령 입력에이를 써야합니다. 이 같은

뭔가 :

ChannelExec channel = (ChannelExec) session.openChannel("exec"); 
channelExec.setCommand("copy run tftp : "); 
OutputStream out = channelExec.getOutputStream(); 
channelExec.connect(); 
out.write(("192.168.50.1 : \n").getBytes()); 
out.write(("Config.txt \n").getBytes()); 
out.flush(); 
+1

고마워요. 이제 작동합니다. – user6624302