Home arrow static arrow Java Programming [Archive] - Telnet logging in - incorrect login
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Telnet logging in - incorrect login
This topic has 1 reply on 1 page.

Posts:2
Registered: 2/27/04
Telnet logging in - incorrect login  
Jul 8, 2004 10:17 AM



 
Hello All,

I have an app that will log in to a unix box through telnet and run a unix command ("ls -la") and return me the output to an arraylist. This part is all working great, and of course there is a but. If a use logs in with the wrong password I get back a Login incorrect and then a login: prompt. Below is my code if any one could point me in the right direction that would be great.

private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '>';

public ArrayList TelnetConnection( String server, String user, String password, String path ) {

ArrayList output = new ArrayList();
String yy = null;
String tt = null;
try {
// Connect to the specified server
telnet.connect( server, 23 );

} catch ( IOException ae ) {
output = null;
System.out.println("In the connect catch****************");
return output;
}
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream( telnet.getOutputStream() );
try {
// Log the user on
readUntil( "login: " );
write( user );
readUntil( "Password: " );
write( password );
} catch (Exception ee) {
try{
telnet.disconnect();
}catch (IOException are) {
are.printStackTrace();
}
ee.printStackTrace();
}
*********************************************************************************
here is where I was trying to test for the login incorrect
**********************************************************************************

/* trying to catch to see if the end user did not use the correct password
* the will be a login incorrect. So you need to catch this information.
*/
String loginok = inCorrectLogin( "Login incorrect" + "
n" + "login: " );

if (loginok.equalsIgnoreCase("true")) {
output = null;
System.out.println("line 56 if statement****************");
try {
telnet.disconnect();
} catch (IOException aer) {
aer.printStackTrace();
}
return output;
} else {
// Advance to a prompt
readUntil( prompt + " " );
}

return output;
}

public ArrayList sendUnixCommand( String command ) {

ArrayList dirlist = new ArrayList();

try {
write( command );

try {

// Reader r = new BufferedReader(new InputStreamReader(telnet.getInputStream()));
StreamTokenizer st = new StreamTokenizer(in);

st.wordChars('_', '_');
st.wordChars(' ', ' ');
st.wordChars('-', '-');
st.wordChars('-', '-');
st.wordChars(':', ':');
st.wordChars('*', '*');

while ( st.nextToken() != StreamTokenizer.TT_EOF ) {
if ( st.ttype == StreamTokenizer.TT_WORD) {
if (st.sval.equalsIgnoreCase("backyard")) {
System.out.println("in backyard if statement");
return dirlist;
} else {
System.out.println( "A word: " + st.sval );
dirlist.add(st.sval);
}
} else if ( st.ttype == StreamTokenizer.TT_EOF ) {
System.out.println( "EOL" );
}else {
System.out.println( "Other: " + (char) st.ttype );
}
}
} catch( Exception e ) {
telnet.disconnect();
System.out.println("check point bla 2");
e.printStackTrace();
}
} catch( Exception e ) {
System.out.println("check point in a catch bla 3");
e.printStackTrace();
try {
telnet.disconnect();
}catch (IOException ae) {
ae.printStackTrace();
}

}

return dirlist;
}

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 );
System.out.println("");
sb.append( ch );
if( ch == lastChar ) {
if( sb.toString().endsWith( pattern ) ) {
return sb.toString();
}
}
ch = ( char )in.read();
}
}
catch( Exception e ) {
System.out.println("in the read until catch ***************");
e.printStackTrace();
}
return null;
}

public String inCorrectLogin( String patterna ) {
try {
char lastChara = patterna.charAt( patterna.length() - 1 );
StringBuffer sba = new StringBuffer();
boolean founda = false;
char cha = ( char )in.read();
while( true ) {
System.out.print( cha );
//System.out.println("");
sba.append( cha );
if( cha == lastChara ) {
if( sba.toString().endsWith( patterna ) ) {
return "true";
}
}
cha = ( char )in.read();
}
}
catch( Exception e ) {
System.out.println("in the read until catch ***************");
e.printStackTrace();
}
return "False";
}

public void write( String value ) {
try {
out.println( value );
out.flush();
System.out.println( value );
}
catch( Exception e ) {
System.out.println("in the write method catch***********");
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();
}
}

}

And I'm sure by my code you can tell that I'm a serious rookie at Java. Thanks again for your help or input.

-Dale

 

Posts:27,518
Registered: 11/3/97
Re: Telnet logging in - incorrect login  
Jul 8, 2004 12:27 PM (reply 1 of 1)



 
And....what is the question?

If the login in fails then presumably you want to handle it as an error condition? If so you have to parse the output looking for the failure.

Given that you are not handling the protocol yourself then that means you need to look at the class that is handling this for you. And that is not part of the standard Java API. That class might handle it but you didn't specify the source of that class so it is hard to figure out where you got it from. And if it is your own class then nothing anyone can do here will help you to figure out what you should be doing.

I suspect you don't quite understand telenet either. Telnet just takes commands and gives responses. Login is a command just like any other. If it fails it isn't going to generate an exception (or it shouldn't any more so than any other command.) That doesn't mean that an exception can't occur during it, but an exception can occur during any command, for example if the telnet server is taken down.

And depending on what you are doing you might also want to keep in mind that telnet commands are not necessarily standard. Thus a sequence of commands to a telnet on windows might not work on a linux telnet. It might not even work on a different windows telnet server.
 
This topic has 1 reply on 1 page.