关于java实现telnet客户端的问题
1、需求:用java实现telnet到设备上后,执行一个交互式shell脚本;
脚本信息如下:a.sh
echo "pls input a"
read $a
echo "pls input b"
read $b
echo "pls input c"
read $c
要求获取shell脚本的全部输出信息;
telnet代码如下:运行后只能获取第一个打印信息,其余输出信息都获取不到;高手帮忙解答下,谢谢!
/**
* commons-net-2.0.jar是工程依赖包
*/
package telnet2;
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;
/**
* @descript NetTelenet.java
* @author sinclair
* @date Jun 10, 2010
*/
public class NetTelnet {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$';// 普通用户结束
public NetTelnet( String ip, int port, String user, String password ) {
try {
telnet.connect( ip, port );
in = telnet.getInputStream();
out = new PrintStream( telnet.getOutputStream() );
// 根据root用户设置结束符
this.prompt = user.equals( "root" ) ? '#' : '$';
login( user, password );
} catch ( Exception e ) {
e.printStackTrace();
}
}
/**
* 登录
*
* @param user
* @param password
*/
public void login( String user, String password ) {
readUntil( "login:" );
write( user );
readUntil( "Password:" );
write( password );
readUntil( prompt + " " );
}
/**
* 读取分析结果
*
* @param pattern
* @return
*/
public String readUntil( String pattern ) {
try {
char lastChar = pattern.charAt( pattern.length() - 1 );
StringBuffer sb = new StringBuffer();
char ch = ( char ) in.read();
while ( true ) {
sb.append( ch );
if (ch == lastChar) {
if (sb.toString().endsWith( pattern )) {
return sb.toString();
}
}
ch = ( char ) in.read();
}
} catch ( Exception e ) {
e.printStackTrace();
}
return null;
}
/**
* 写操作
*
* @param value
*/
public void write( String value ) {
try {
out.println( value );
out.flush();
} catch ( Exception e ) {
e.printStackTrace();
}
}
/**
* 向目标发送命令字符串
*
* @param command
* @return
*/
public String sendCommand( String command ) {
try {
write( command );
return readUntil( prompt + " " );
} catch ( Exception e ) {
e.printStackTrace();
}
return null;
}
/**
* 关闭连接
*/
public void disconnect() {
try {
telnet.disconnect();
} catch ( Exception e ) {
e.printStackTrace();
}
}
public static void main( String[] args ) {
try {
System.out.println( "启动Telnet..." );
String ip = "192.168.0.11";
int port = 23;
String user = "user";
String password = "111111";
NetTelnet telnet = new NetTelnet( ip, port, user, password );
telnet.sendCommand( "export LANG=en" );
String r1 = telnet.sendCommand( "a.sh" );
String r2 = telnet.sendCommand( "a" );
String r3 = telnet.sendCommand( "b" );
System.out.println( "显示结果" );
System.out.println( r1 );
System.out.println( r2 );
System.out.println( r3 );
telnet.disconnect();
} catch ( Exception e ) {
e.printStackTrace();
}
}
} --------------------编程问答--------------------
--------------------编程问答-------------------- 我的回复跑哪里去了.... --------------------编程问答-------------------- 谢谢,我回去试试看 --------------------编程问答-------------------- 恩 我的项目里是这样写的,完全没有问题,上面的你应该看的懂的
package com.sanss.telnet;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;
import com.sanss.basecontrol.model.Baseset;
import com.sanss.common.SysConfig;
public class NetTelnet {
public NetTelnet() {
}
// Telnet对象
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
// 提示符。每个服务器都不一样 请注意修改
private static String prompt = ">";
// telnet端口
private static String port = "23";
public NetTelnet(String ip, String user, String password, String port)
throws NumberFormatException, SocketException, IOException {
try {
telnet.connect(ip, Integer.parseInt(port));
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// 登录
readUntil("login: ");
write(user);
readUntil("Password: ");
write(password);
readUntil(prompt);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取分析结果
*
* @param pattern
* @return
* @throws IOException
*/
public String readUntil(String pattern) throws IOException {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
InputStreamReader br = new InputStreamReader(in);
char ch = (char) br.read();
while (true) {
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) br.read();
}
}
/**
* 写
*
* @param value
*/
public void write(String value) {
out.println(value);
out.flush();
}
/**
* 向目标发送命令字符串
*
* @param command
* @return
* @throws IOException
*/
public String sendCommand(String user, String command) throws IOException {
write(command);
return readUntil(user + prompt);
}
/**
* 关闭连接
*
* @throws IOException
*
*/
public void disconnect() throws IOException {
telnet.disconnect();
}
public static void main(String[] args) {
execute(null, "cd test");
}
public static boolean execute(Baseset baseset, String command) {
try {
NetTelnet telnet = new NetTelnet(baseset.getIp().trim(), baseset
.getUser().trim(), baseset.getPassword().trim(), port);
telnet.sendCommand(baseset.getUser().trim(), "rm result.txt ");
telnet
.sendCommand(baseset.getUser().trim(),
"apxrcv -text -ignwarn -txtelog result.txt < addscript.txt");
// apxrcv -text -ignwarn -txtelog addscript.txt < result.txt
// telnet.sendCommand(SysConfig.getInstance().getTelnetcommand());
telnet.disconnect();
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}
public String getPrompt() {
return prompt;
}
public void setPrompt(String prompt) {
this.prompt = prompt;
}
}
以前写过的一个里面有解释可以看看
补充:Java , Java SE