How To Simulate Telnet Session in Java Como para simular uma sessão telnet em Java
Often we come across procedures which need to be performed by issuing commands in a telnet session. Muitas vezes nos deparamos com procedimentos que devem ser realizadas por meio de comandos de uma sessão telnet. It is relatively easy to do it manually but is definitely not suitable for automation. É relativamente fácil fazê-lo manualmente, mas definitivamente não é adequado para automação. Let’s see how you can easily automate such tasks using Java software. Vamos ver como você pode facilmente automatizar essa tarefa usando software Java. Sample code included. Exemplo de código incluídos.
Here is the core code: Aqui está o núcleo do código:
// Conversation; Simulating telnet session with James server / / Conversas; Simulando sessão telnet com James servidor
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, SHUTDOWN);
readLines(r, 1); readLines (r, 1);
readLines() reads and discards specified number of lines (response). readLines () lê e devoluções determinado número de linhas (resposta).
Here are the key functions: Aqui estão as principais funções:
/** 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(); } / ** Ler e descartar conta as linhas de 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);)) / ** Escreve-se BufferedWriter w e flush * / public static void writeLine (BufferedWriter w, String out) throws IOException (w.write (out + CRLF, 0 , (A + CRLF). Comprimento ()); w.flush ();) You can download the code Você pode fazer o download do código here aqui . It is used to shutdown Apache James Mail Server. É usado para shutdown Apache James Mail Server. The utility is fully described along with the options in - O utilitário é descrito na íntegra, juntamente com as opções em -- Shutdown Apache James Mail Server - Java Utility Encerrar Apache James servidor de e-mail - Java Utility
Filed under Arquivado em Headline News Headline News , How To How To , Java Software Java Software , Tech Note Nota Tech | |
| |
RSS 2.0 RSS 2,0 | |
Email this Article E-mail este artigo
You may also like to read Você pode também gosta de ler |




April 17th, 2006 at 12:45 am 17 de abril de 2006 em 12:45 am
good bom
March 13th, 2008 at 10:59 pm 13 de março de 2008, 10:59 pm
Hey, Ei,
I have a question re the above code. Tenho uma pergunta novamente o referido código. 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. Eu tentei modificar o presente para o uso com um Cisco Catalyst 3500 swtich para voltar a utilizar o endereço MAC do conto "sh mac-endereço-quadro" comando. However, I’m having a problem where, using the above code, i need to specify the number of lines to read back. No entanto, estou com um problema, quando, utilizando o código acima, i necessidade de especificar o número de linhas para ler de volta. But, depending on the amount of clients physically collected into the switch, the number varies. Mas, dependendo da quantidade de clientes fisicamente recolhidos para a mudança, o número varia. I attempted to write a while((line = in.readLine()) != null) { .. Eu tentei escrever um while ((linha = in.readLine ())! = Null) (.. } block of code but tis fails where, once it reaches the end, it sits there as if expecting more. ) Bloco de código, mas não tis, onde, uma vez que ele chegue ao fim, ela se senta lá como se esperava mais. It never receives a NULL at the end. Ele nunca recebe um NULL no final.
In another attempt, i assumed the last line should contain a prompt. Em outra tentativa, i assumiu a última linha deve conter um aviso. However, this also failed. No entanto, este também fracassou. It seems readLine() discards prompts but should replace them with NULL. Parece readLine () desfaz prompts, mas deverá substituí-las com NULL. But this doesn’t seem to be true for me. Mas esse não parece ser verdadeiro para mim.
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? A minha pergunta é esta, há uma maneira de adaptar o seu código para que ele possa ler um número desconhecido de linhas e armazenar a saída?
Many Thanks, Muitos Obrigado,
A