How To Simulate Telnet Session in Java Come per simulare una sessione Telnet in Java
Often we come across procedures which need to be performed by issuing commands in a telnet session. Spesso venire attraverso procedure che devono essere effettuati mediante l'emissione di comandi in una sessione telnet. It is relatively easy to do it manually but is definitely not suitable for automation. È relativamente facile per farlo manualmente, ma sicuramente non è adatto per l'automazione. Let’s see how you can easily automate such tasks using Java software. Vediamo come si può facilmente automatizzare tali compiti, utilizzando il software Java. Sample code included. Codice di esempio incluso.
Here is the core code: Qui è il cuore codice:
// Conversation; Simulating telnet session with James server / / Conversazione; Simulazione sessione telnet con il server James
readLines(r, 3); readLines (r, 3);
writeLine(w, login); WriteLine (w, login);
readLines(r, 1); readLines (r, 1);
writeLine(w, password); WriteLine (w, password);
readLines(r, 1); readLines (r, 1);
writeLine(w, SHUTDOWN); WriteLine (w, Arresto);
readLines(r, 1); readLines (r, 1);
readLines() reads and discards specified number of lines (response). readLines () legge e rigetti determinato numero di linee (risposta).
Here are the key functions: Ecco le funzioni principali:
/** Read and discard count lines from BufferedReader r */ public static void readLines(BufferedReader r, int count) throws IOException { for(int i = 0;i < count;i++) { String t = r.readLine(); if(DEBUG) System.out.println(t); } } /** Write out to BufferedWriter w and flush */ public static void writeLine(BufferedWriter w, String out) throws IOException { w.write(out + CRLF, 0, (out + CRLF).length()); w.flush(); } / ** Leggi e scartare count linee da BufferedReader r * / public static void readLines (BufferedReader r, int count) lancia IOException (for (int i = 0; i <count; i + +) (String t = r.readLine (); if (debug) System.out.println (t);)) / ** Scrivi a BufferedWriter w e filo * / public static void WriteLine (BufferedWriter w, String out) lancia IOException (w.write (su + CRLF, 0 , (Su + CRLF). Lunghezza ()); w.flush ();) You can download the code È possibile scaricare il codice here qui . It is used to shutdown Apache James Mail Server. E 'usato per spegnere Apache James Mail Server. The utility is fully described along with the options in - L'utilità è interamente descritto insieme con le opzioni -- Shutdown Apache James Mail Server - Java Utility Arresto di Apache James server di posta - Java Utility
Filed under Elencato sotto Headline News Headline News , How To Come , Java Software Software Java , Tech Note Nota tech | |
| |
RSS 2.0 RSS 2,0 | |
Email this Article Invia questo articolo
You may also like to read Si può anche leggere come |





April 17th, 2006 at 12:45 am 17 aprile 2006 alle 12:45 am
good buono
March 13th, 2008 at 10:59 pm 13 marzo 2008, 10:59 pm
Hey, Ehi,
I have a question re the above code. Ho una domanda nuovamente il codice sopra. I attempted to modify this for use with a Cisco Catalyst 3500 swtich to return the the mac address tale using the “sh mac-address-table” command. Ho cercato di modificare il presente per l'uso con un Cisco Catalyst 3500 swtich per restituire il l'indirizzo MAC utilizzando il racconto "sh mac-address-tabella". However, I’m having a problem where, using the above code, i need to specify the number of lines to read back. Tuttavia, Ho un problema quando, utilizzando il codice sopra, ho bisogno di specificare il numero di linee di leggere indietro. But, depending on the amount of clients physically collected into the switch, the number varies. , Ma, a seconda della quantità di clienti raccolti in fisicamente il passaggio, il numero varia. I attempted to write a while((line = in.readLine()) != null) { .. Ho cercato di scrivere un po 'di tempo ((linea = in.readLine ())! = Null) (.. } block of code but tis fails where, once it reaches the end, it sits there as if expecting more. ) Blocco di codice, ma non tis dove, una volta che raggiunge la fine, si siede come se ci aspettiamo di più. It never receives a NULL at the end. Non si è mai riceve un NULL alla fine.
In another attempt, i assumed the last line should contain a prompt. In un altro tentativo, i assunto l'ultima riga deve contenere un prompt dei comandi. However, this also failed. Tuttavia, anche questo non è riuscito. It seems readLine() discards prompts but should replace them with NULL. Sembra ReadLine () richiede i rigetti, ma dovrebbe sostituirli con NULL. But this doesn’t seem to be true for me. Ma questo non sembra essere vero per me.
My question is this, is there a way to adapt your code so that it can read an unknown number of lines and store the output? La mia domanda è questa, c'è un modo di adattare il tuo codice in modo che possa leggere un numero sconosciuto di linee e di memorizzare l'output?
Many Thanks, Molte grazie,
A Un