• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java trying to read Telnet banner (help appreciated)  RSS feed

 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use Java to read the banner from a telnet session. Currently, when I try to "talk" with the telnet machine I end up with nothing.
Please do not hesitate to email me with any comments or suggetions.
Thanks,
Landon Manning
yandonman@hotmail.com
In my method I do the following:
.
.
.
TelnetModule myTelnetModule = new TelnetModule()
myTelnetModule.connect("HostNameWithTelnetRunning");
myTelnetModule.getBanner()
.
.
.
Here's the rest of the code:

//
// Used to connect to the remote machine and initiate comunication.
// This method only takes care of establishing the control socket.
// No atempt is made by this method to login.
//
// @param hostName Name of the host to connect to.
//
// @return Returns true on success.
//
public boolean connect( String hostname )
{
boolean isSuccessful = false;

if( State == CONNECTED )
{
// Disconnect the current connection first
this.disconnect();
}
try
{
controlSocket = new Socket(hostname, telnetPort);
controlSocket.setSoTimeout(5000);
controlInputStream = new BufferedReader(new InputStreamReader(controlSocket.getInputStream()));
controlOutputStream = new BufferedWriter(new OutputStreamWriter(controlSocket.getOutputStream()));
State = CONNECTED;
isSuccessful = true;
banner = getMoreInput();

} catch (UnknownHostException e) {
if( logging != null )
logging.writeLog( LoggingType.INFORMATION, "Problem connecting - host unknown." );
} catch (IOException e) {
if( logging != null )
logging.writeLog( LoggingType.INFORMATION, "Problem connecting - IO problem." );
}
return isSuccessful;
}

/*
* Method: getMoreInput()
* Purpose: gets data from connection but limits the buffer and prevents hanging
* Parameters: None
* Return: returns the string from of what was read
* Exception: throws ConstantInputConnectionNotExpected when reads more data than
* MAX_BUFFER_SIZE
*/
public String getMoreInput() throws com.exceptions.ConstantInputConnectionNotExpected
{
int count = 0;
int ifCount = 0;
int charRead;
StringBuffer buffer = new StringBuffer();

try
{
while( (count < MAX_BUFFER_SIZE) && (ifCount < 10) )
{
charRead = (int) controlInputStream.read();
if ( charRead == 255 )
{
handleOption( controlInputStream,controlOutputStream );
ifCount++;
}
else
{
buffer.append( (char) charRead );
count++;
ifCount = 0;
}
}
}
catch( IOException e )
{
// occurs when we are done reading or some other IO error
}

// if banner is too large
if( count >= MAX_BUFFER_SIZE )
{
State = UNEXPECTED_STREAM;
disconnect();
throw new ConstantInputConnectionNotExpected();
}

return buffer.toString();
}
//The part of the code that "talks" with the telnet session
private void handleOption(BufferedReader myReader, BufferedWriter myWriter)
{
int command=0, option=0;
try
{
command = (int)myReader.read();
option = (int)myReader.read();
switch (command)
{
case 251:
case 252:
myWriter.write(255);
myWriter.write(254);
myWriter.write(option);
break;
case 253:
case 254:
myWriter.write(255);
myWriter.write(252);
myWriter.write(option);
break;
default:
break;
}
myWriter.flush();
} catch( Exception e )
{
if( logging != null )
logging.writeLog( LoggingType.ERROR, "TelnetModule: Error.", e );
}
}

Thanks (agian),
Landon Manning
yandonman@hotmail.com
 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works, except for one minor error. The ifCount variable keeps track of the number of consecutive INTERRUPT_AS_COMMANDs. It was exiting the function once it had more than 10 of them. Well, some connetions negotiate their options for more than 10 IACs.
I modified the code so that the handleOption method throws an IOException upon finding a IAC (255) in both the command and option reads on the stream. I use this condition (the two IAC reads) as the indicator that we are done negotiating with the telnet server.

Originally posted by landon manning:
I'm trying to use Java to read the banner from a telnet session. Currently, when I try to "talk" with the telnet machine I end up with nothing.
Please do not hesitate to email me with any comments or suggetions.
Thanks,
Landon Manning
yandonman@hotmail.com
In my method I do the following:
.
.
.
TelnetModule myTelnetModule = new TelnetModule()
myTelnetModule.connect("HostNameWithTelnetRunning");
myTelnetModule.getBanner()
.
.
.
Here's the rest of the code:

//
// Used to connect to the remote machine and initiate comunication.
// This method only takes care of establishing the control socket.
// No atempt is made by this method to login.
//
// @param hostName Name of the host to connect to.
//
// @return Returns true on success.
//
public boolean connect( String hostname )
{
boolean isSuccessful = false;

if( State == CONNECTED )
{
// Disconnect the current connection first
this.disconnect();
}
try
{
controlSocket = new Socket(hostname, telnetPort);
controlSocket.setSoTimeout(5000);
controlInputStream = new BufferedReader(new InputStreamReader(controlSocket.getInputStream()));
controlOutputStream = new BufferedWriter(new OutputStreamWriter(controlSocket.getOutputStream()));
State = CONNECTED;
isSuccessful = true;
banner = getMoreInput();

} catch (UnknownHostException e) {
if( logging != null )
logging.writeLog( LoggingType.INFORMATION, "Problem connecting - host unknown." );
} catch (IOException e) {
if( logging != null )
logging.writeLog( LoggingType.INFORMATION, "Problem connecting - IO problem." );
}
return isSuccessful;
}

/*
* Method: getMoreInput()
* Purpose: gets data from connection but limits the buffer and prevents hanging
* Parameters: None
* Return: returns the string from of what was read
* Exception: throws ConstantInputConnectionNotExpected when reads more data than
* MAX_BUFFER_SIZE
*/
public String getMoreInput() throws com.exceptions.ConstantInputConnectionNotExpected
{
int count = 0;
int ifCount = 0;
int charRead;
StringBuffer buffer = new StringBuffer();

try
{
while( (count < MAX_BUFFER_SIZE) && (ifCount < 10) )
{
charRead = (int) controlInputStream.read();
if ( charRead == 255 )
{
handleOption( controlInputStream,controlOutputStream );
ifCount++;
}
else
{
buffer.append( (char) charRead );
count++;
ifCount = 0;
}
}
}
catch( IOException e )
{
// occurs when we are done reading or some other IO error
}

// if banner is too large
if( count >= MAX_BUFFER_SIZE )
{
State = UNEXPECTED_STREAM;
disconnect();
throw new ConstantInputConnectionNotExpected();
}

return buffer.toString();
}
//The part of the code that "talks" with the telnet session
private void handleOption(BufferedReader myReader, BufferedWriter myWriter)
{
int command=0, option=0;
try
{
command = (int)myReader.read();
option = (int)myReader.read();
switch (command)
{
case 251:
case 252:
myWriter.write(255);
myWriter.write(254);
myWriter.write(option);
break;
case 253:
case 254:
myWriter.write(255);
myWriter.write(252);
myWriter.write(option);
break;
default:
break;
}
myWriter.flush();
} catch( Exception e )
{
if( logging != null )
logging.writeLog( LoggingType.ERROR, "TelnetModule: Error.", e );
}
}

Thanks (agian),
Landon Manning
yandonman@hotmail.com

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!