2017-03-24 1 views
1

Jcraft Jschライブラリを使用してJavaアプリケーション経由でルータを管理しようとしています。JShでSSHで実行されるコマンドへの入力/サブコマンドの提供

私はTFTPサーバ経由でルータ設定を送信しようとしています。これはPuTTYで動作するため、問題は私のJavaコードにあります。

この私の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

関連する問題