1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import org.apache.commons.net.telnet.TelnetClient;
public class TestMain {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
static StringBuffer st= new StringBuffer();
// constructeur par defaut
public TestMain() {
}
public void connect(String server) {
try {
// Connect to the specified server
telnet.connect(server, 23);
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// Log the user on
readUntil("login: ");
//write("login");
write("test");
readUntil("password: ");
//write("password");
write("PApp@123");
//Advance to a prompt
//readUntil("#");
} catch (Exception e) {
e.printStackTrace();
}
}
public String readUntil(String pattern) {
try {
//System.out.println("Start readUntil");
char lastChar = pattern.charAt(pattern.length()-1);
//Thread.sleep(2000);
char ch = (char) in.read();
System.out.println("mca3 ch<" + ch + ">");
while (true) {
st.append(ch);
if (ch == lastChar) {
if (st.toString().endsWith(pattern)) {
System.out.println("mca §" + st.toString() + "§");
return st.toString();
}
}
ch = (char) in.read();
//System.out.println("mca <" + st.toString() + ">");
}
}
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 {
System.out.println("mca debut send commande");
System.out.println("mca2222 <" + st.toString() + ">");
readUntil("test>");
write(command);
System.out.println("mca333 <" + st.toString() + ">");
//Thread.sleep(2000);
System.out.println("mca fin send commande");
//System.out.println("mca2 <" + st.toString() + ">");
//return readUntil(" ");
//readUntil(">");
return null;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void disconnect() {
try {
telnet.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello world mostafa chab");
try {
String address1 = "me";
String ligne="";
TestMain telnet = new TestMain();
BufferedReader boq3 = new BufferedReader(new FileReader("E:/TstTelnet/input_file.txt"));
PrintWriter printWriter1 = new PrintWriter ("E:/TstTelnet/output_file.txt");
while ((ligne = boq3.readLine()) != null)
{
StringTokenizer sf = new StringTokenizer(ligne,",");
System.out.println("ligne : " + ligne);
sf.nextToken();
address1=sf.nextToken();
System.out.println("address1 : " + address1);
telnet.connect(address1);
printWriter1.println (st);
System.out.println("mca connection OK");
//telnet.sendCommand("show port");
telnet.sendCommand("dir");
printWriter1.println (st);
printWriter1.println ("Ne_ip_address :"+address1);
printWriter1.println (st);
st.delete(0, st.length());
telnet.disconnect();
}
printWriter1.close();
boq3.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} |
Partager