Telnet-clientは疲れすぎて、SSH-Clientよりもはるかに多く、可能な限り使用できます。
org.apache.commons.net.telnet.telnetclien- ApacheのCommons-netパッケージ、Commons-Net-3.0.1-bin.zipを使用しました。
コードコピーは次のとおりです。
パッケージorg.sl.util;
org.apache.commons.net.telnet.telnetclientをインポートします。
java.io.*をインポートします。
java.nio.bytebufferをインポートします。
パブリッククラスのtelnetutil {
文字列charset = null;
byte [] buff = new byte [2048];
telnetclient telnetclient = new TelnetClient();
BufferedReader TelnetReader = null;
bufferedwriter telnetwirter = null;
inputstream telnetin = null;
outputstream telnetout = null;
public telnetutil(){
telnetclient = new TelnetClient();
}
/**
*サーバーに接続します
* @Param IP
* @paramポート
* @Throws unsupportedencodingException
* @throws ioexception
*/
public void connect(string ip、int port)は、unsupportedencodingexception、ioexceptionをスローします{
telnetclient.connect(ip、port);
setioStream();
}
/**
*サーバーに接続します
* @Param IP
* @Throws unsupportedencodingException
* @throws ioexception
*/
public void connect(string ip)throws unsupportedencodingException、ioexception {
telnetclient.connect(ip);
setioStream();
}
void setioStream()はunsupportedencodingceptionをスローします{
telnetin = telnetclient.getInputStream();
telnetout = telnetclient.getOutputStream();
if(null == charset){
TelnetReader = new BufferedReader(new inputStreamReader(Telnetin));
telnetwirter = new BufferedWriter(new outputStreamWriter(Telnetout));
}それ以外{
TelnetReader = new BufferedReader(new inputStreamReader(telnetin、charset));
telnetwirter = new BufferedWriter(new outputStreamWriter(Telnetout、charset));
}
}
/**
* ログイン
* @paramユーザー
* @param passwd
* @returnログインが成功したかどうか。
* @throws ioexception
*/
public boolean login(string user、string passwd)ioexception {
文字列read = readstring();
for(int i = 0;; i ++){
if(-1 == read.indexof( "login")){
read = readstring();
}それ以外{
壊す;
}
}
writeText(user);
read = readstring();
for(int i = 0;; i ++){
if(-1 == read.indexof( "password")){
read = readstring();
}それ以外{
壊す;
}
}
writeText(passwd);
のために(;;){
read = readstring();
//system.out.println("last:"+read);
if(-1!= read.indexof( "last")){
trueを返します。
} else if(-1!= read.indexof( "Incorrect")){
falseを返します。
}
}
}
/**
*これはテスト方法であり、さりげなく書くだけです。
* @throws ioexception
*/
public void show()throws ioexception {
// system.out.println(readstring());
// system.out.println(readstring());
// bytebuffer tmp = bytebuffer.allocate(1024);
// byte [] buff = new byte [1024];
// while(telnetin.abailable()> 0){
// int readlen = readbytes(fuff、0,1024);
// tmp.put(buff、0、readlen);
//}
// system.out.println(new String(tmp.array()));
system.out.println( "1"+readstring());
System.out.println( "2"+readstring());
system.out.println( "3"+readstring());
writeText( "root");
System.out.println( "4" + readstring());
writeText( "123456");
System.out.println( "5"+readstring());
// system.out.println( "6"+readstring());
// system.out.println( "7"+readstring());
}
public int readbytes(byte [] buff、int offset、int len)はioexceptionをスローします{
return telnetin.read(buff、offset、len);
}
/**
*文字列<br/>を読み取ります
* readbyte()を文字列に変換することに相当します
* @戻る
* @throws ioexception
*/
public string readstring()throws ioexception {
int readlen = readbytes(this.buff、0、this.buff.length);
if(0 <readlen)
新しい文字列(buff、0、readlen).trim()を返します。
それ以外
戻る "";
}
/**
*行を読む<br/>
*サーバーとクライアントが同じオペレーティングシステムでない場合、この方法は行の計算に失敗する可能性があります。
* @戻る
* @throws ioexception
*/
public string readline()throws ioexception {
文字列read = telnetreader.readline();
null == read? "":read.trim();
}
public void writebytes(byte [] buff、int offset、int len)はioexceptionをスローします{
telnetout.write(buff、offset、len);
}
/**
*サーバーに文字列を書き込みます
* @paramテキスト
* @throws ioexception
*/
public void writeText(文字列テキスト)IOExceptionをスローする{
telnetwirter.write(テキスト);
telnetwirter.write( '/r');
telnetwirter.write( '/n');
telnetwirter.flush();
}
/**
*コマンドを実行し、結果を返します<br/>
*相当:<br>
* writeText();
* ReadString();
* @Param CMD
* @戻る
* @throws ioexception
*/
public string exec(string cmd)throws ioexception {
writeText(CMD);
ReadString();
}
string login1(string user、string passwd)ioexception {
文字列read = null;
readstring();
readstring();
read = readstring();
if(-1!= read.indexof( "login")){
writeText(user);
}
read = readstring();
if(-1!= read.indexof( "password")){
writeText(passwd);
}
read = readstring();
read += readstring();
読み取ります。
// stringbuffer sb = new StringBuffer();
// while(null!=(read = readstring())){
// sb.append(read);
//}
//
// sb.toString()を返します。
}
/**
*閉鎖
*/
public void close(){
試す{
writeText( "exit");
writeText( "exit");
writeText( "exit");
} catch(例外Ex){
}
試す {
if(null!= telnetin)telnetin.close();
} catch(例外e){
}
試す {
if(null!= telnetout)telnetout.close();
} catch(例外e){
}
試す {
if(null!= telnetclient)telnetclient.disconnect();
} catch(例外e){
}
}
/**
* Telnet通信中に文字セットを設定<br/>
*注:この文字セットには、サーバー側の文字セットとは必要な関係がありません<br/>
*この方法は、connect()の前に呼び出す必要があります
* @Param Charset
*/
public void setCharset(string charset){
this.charset = charset;
}
/**
*バフサイズをリセットし、デフォルトで2048バイトになります。
* @paramサイズ
*/
public void setBufferSize(int size){
this.buff = new byte [size];
}
}
テストクラス
コードコピーは次のとおりです。
static void t4(){
telnetutil tu = new Telnetutil();
試す {
tu.Connect( "192.168.2.154");
System.out.println(tu.login( "root"、 "123456"));
//tu.show();
//system.out.println(tu.readstring());
//system.out.println(tu.exec("pwd "));
System.out.println(tu.exec( "echo /"123456789/">1.txt"));
System.out.println(tu.exec( "cat 1.txt"));
} catch(ioException e){
e.printstacktrace();
}
tu.close();
}
static void t1(){
telnetutil tu = new Telnetutil();
試す {
tu.Connect( "192.168.2.154");
System.out.println(tu.login( "sl1"、 "coffee8215"));
//tu.show();
//system.out.println(tu.readstring());
System.out.println(tu.exec( "pwd"));
} catch(ioException e){
e.printstacktrace();
}
tu.close();
}