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 170 171 172
| import com.jscape.inet.telnet.*;
import java.io.*;
public class TelnetExample extends TelnetAdapter {
private Telnet telnet = null;
//private Telnet passw = null;
private OutputStream output = null;
private static BufferedReader reader = null;
private boolean connected = false;
public TelnetExample(String hostname, String password) throws IOException, TelnetException {
String input = null;
// create new Telnet instance
telnet = new Telnet(hostname);
// register this class as TelnetListener
telnet.addTelnetListener(this);
// establish Telnet connection
telnet.connect();
connected = true;
// get output stream
output = telnet.getOutputStream();
// sends all data entered at console to Telnet server
while ((input = reader.readLine()) != null) {
if (connected) {
((TelnetOutputStream) output).println(input);
} else {
break;
}
}
}
/**
* Invoked when Telnet socket is connected.
* @see TelnetConnectedEvent
* @see Telnet#connect
*/
public void connected(TelnetConnectedEvent event) {
System.out.println("Connected");
}
/**
* Invoked when Telnet socket is disconnected. Disconnect can
* occur in many circumstances including IOException during socket read/write.
* @see TelnetDisconnectedEvent
* @see Telnet#disconnect
*/
public void disconnected(TelnetDisconnectedEvent event) {
connected = false;
System.out.print("Disconnected. Press enter key to quit.");
}
/**
* Invoked when Telnet server requests that the Telnet client begin performing specified <code>TelnetOption</code>.
* @param event a <code>DoOptionEvent</code>
* @see DoOptionEvent
* @see TelnetOption
*/
public void doOption(DoOptionEvent event) {
// refuse any options requested by Telnet server
telnet.sendWontOption(event.getOption());
}
/**
* Invoked when Telnet server offers to begin performing specified <code>TelnetOption</code>.
* @param event a <code>WillOptionEvent</code>
* @see WillOptionEvent
* @see TelnetOption
*/
public void willOption(WillOptionEvent event) {
// refuse any options offered by Telnet server
telnet.sendDontOption(event.getOption());
}
/**
* Invoked when data is received from Telnet server.
* @param event a <code>TelnetDataReceivedEvent</code>
* @see TelnetDataReceivedEvent
*/
public void dataReceived(TelnetDataReceivedEvent event) {
// print data recevied from Telnet server to console
System.out.print(event.getData());
}
/**
* Main method for launching program
* @param args program arguments
*/
public static void main(String[] args) {
String chaine="";
String fichier ="presence.txt";
String cherche = "presence : on";
String cherche2 = "resence : off";
Integer index;
Integer index2;
Integer i;
for (i=0;i<1;i++)
{
//lecture du fichier texte
try{
InputStream ips=new FileInputStream(fichier);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null){
System.out.println(ligne);
chaine+=ligne+"\n";
index=ligne.indexOf(cherche);
//System.out.println(index);
index2=ligne.indexOf(cherche2);
//System.out.println("indexOf(presence : on) = " +
//ligne.indexOf("presence : on"));
//System.out.println("indexOf(presence : off) = " +
//ligne.indexOf("resence : off"));
if (index == 0) {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
// prompt user for Telnet server hostname
//System.out.print("Enter Telnet server hostname (e.g. 10.0.0.1): ");
String hostname = "192.168.100.254";
String password = "cisco";
System.out.println(password);
// create new TelnetExample instance
TelnetExample example = new TelnetExample(hostname, password);
//TelnetExample pass = new TelnetExample(hostname);
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
//else {System.out.println("je ne suis pas la"); }
if (index2 == 1) {System.out.println("je ne suis pas la"); break;}
}
br.close();
}
catch (Exception e){
System.out.println(e.toString());
}
}
}
} |
Partager