- import org.apache.commons.net.telnet.*;
- import java.io.*;
- public class TelnetSample {
- private TelnetClient telnet = new TelnetClient();
- private InputStream in;
- private PrintStream out;
- private char prompt = ';
- public TelnetSample(String server, String user, String password) {
- try {
- telnet.connect(server, 23);
- in = telnet.getInputStream();
- out = new PrintStream(telnet.getOutputStream());
- readUntil("ogin: ");
- write(user);
- readUntil("assword: ");
- write(password);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void su(String password) {
- try {
- write("su");
- readUntil("Password: ");
- write(password);
- prompt = '#';
- readUntil(prompt + " ");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String readUntil(String pattern) {
- try {
- char lastChar = pattern.charAt(pattern.length() - 1);
- StringBuffer sb = new StringBuffer();
- boolean found = false;
- char ch = (char) in.read();
- while (true) {
- System.out.print(ch);
- 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;
- }
- public void write(String value) {
- try {
- out.println(value);
- out.flush();
- System.out.println(value);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- 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 {
- TelnetSample telnet = new TelnetSample("192.1***.0.***", "username","password");
- telnet.sendCommand("dir ");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
------------------------------------------------------------------------------------------------------------------------
建立PC与设备的连接,之后发送命令
Runtime.getRuntime().exec("adb forward tcp:1080 tcp:1080");
Runtime.getRuntime().exec("adb shell monkey --port 1080");
Socket socket = new Socket();
socket.connect(new InetSocketAddress("127.0.0.1", 1080));
BufferedWriter
out =
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
out.write("cmd");
out.newLine();
out.flush();