-
login script - URGENT!!!
i am in desperate need to be able to log in to a telnet server. i am writing a program in C that acts as a telnet client, which will automatically login.
When i open the tcp connection, the login script appears.
if i write the username to teh socket then it gets to teh password prompt. However, if i just tell it to write the password to teh socket, it puts the password before the password prompt, and after the login.
i need to be able to tell when the word "Password:" is read from teh socket, so that when it does, it can send the password.
please can someone tell me how to do this.
thanks.
-
Is this homework or an assignment.
Basically no one is going to write the program for you. But people will help you when you are stuck.
What don't you get? Where are you going wrong? Show some code using code tags.
Also, if you haven't done so already I suggest you read the board guidelines.
If you have any questions please PM me.
Kermi3
Lead Moderator
-
code so far
i just need a bit of code that will check teh result of telnet_fastread to see if it matches the word "password:", and if it does, then i can continue with telnet_fastwrite...... at teh minute, they are all sending at teh same time, and i dont know how to wait for a certain word....
so far i have:
Code:
#define MY_IP_ADDRESS "192.168.14.101"
#define MY_NETMASK "255.255.255.0"
#define MY_GATEWAY "192.168.14.254"
#define MY_NAMESERVER "192.168.14.254"
#define SERIAL_PORT_SPEED 115200
//#undef TELNET_COOKED
#define TELNET_COOKED
#define SERVER_PORT 23
#define REMOTE_HOST "192.168.14.120"
#define CONNECT_TIMEOUT 10
void delay (long mdelay)
{
for (; mdelay>0; mdelay--);
}
void mdelay (long sd)
{
unsigned long t1;
t1=MS_TIMER;
for (t1=MS_TIMER; MS_TIMER<(sd+t1); );
}
#memmap xmem
#use "dcrtcp.lib"
#use "vserial.lib"
// The structure that holds a message and the destination
typedef struct {
char *login;
char *pass;
char *command;
} Message;
main()
{
tcp_Socket sock;
_TelnetCooker cooker;
char s[81];
char *p;
char *q;
char *r;
auto longword timeout;
Message messageArray[1];
auto int result;
brdInit();
sock_init();
messageArray[0].login = "linzee\n\r";
messageArray[0].pass = "Poppy\n\r";
messageArray[0].command = "perl linzee.pl\n\r";
tcp_open(&(sock), 0, inet_addr(REMOTE_HOST), SERVER_PORT, NULL);
timeout = SEC_TIMER + CONNECT_TIMEOUT;
while( !tcp_established(&sock) ) {
if( (long)(SEC_TIMER - timeout) >= 0 ) {
printf( "ERROR: connect: timeout");
exit(99);
}
if( ! tcp_tick( (sock_type *) &sock ) ) {
printf( "ERROR: connect: %s\n", sock.err_msg );
exit(99);
}
}
printf("Connected to: %s:%d\n",REMOTE_HOST,SERVER_PORT);
telnet_init(&cooker, &sock, TELNET_OPTION_GA|TELNET_OPTION_ECHO);
p = messageArray[0].login;
q = messageArray[0].pass;
r = messageArray[0].command;
telnet_fastwrite(&cooker, p, 7);
if (telnet_fastread(&cooker, s, 1 )>0){
telnet_fastwrite(&cooker, p, 7);
}
while (tcp_tick(&sock)) {
if (telnet_fastread(&cooker, s, 1 )>0) printf("%c", *s);
}
sock_close(&sock);
while (tcp_tick(&sock) != 0);
}
-
Code Tags
I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you ;)
For example:
Without code tags:
for(int i=0;i<5;i++)
{
cout << "No code tags are bad";
}
With Code Tags:
Code:
for(int i=0;i<5;i++)
{
cout << "This code is easy to read";
}
This is of course a basic example...more complicated code is even easier to read with code tags than without.
I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.
This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.
If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.
Good Luck with your program,
Kermi3
Lead Moderator
-
sorry, that was me testing things....
well, in the while loop afterwards, it is running telnet_fastread all teh time, so the login prompt comes up first, then it writes the login and then it reads the password prompt...
i guess my thing must be non blocking then, as if i write
code:
telnet_fastread
telnet_fastwrite
telnet_fastread
telnet_fastwrite
/code
then it still puts it as
login: linzee
Poppy
Password:
where Poppy is the password.
how do i make it blocking? what does that mean?
-
well, telnet_fastread and telnet~_fastwrite uses this:
Code:
_tcp_nodebug int sock_fastread( tcp_Socket *s, byte *dp, int len )
{
if( s->ip_type == TCP_PROTO )
return tcp_read(s, dp, len);
else
return -1;
}
_tcp_nodebug int sock_fastwrite( tcp_Socket *s, byte *dp, int len )
{
if (s->ip_type == TCP_PROTO)
return tcp_write(s, dp, len);
else
return -1;
}
which in turn use these:
Code:
_tcp_nodebug int tcp_write( tcp_Socket *s, byte *dp, int len )
{
auto int x;
LOCK_GLOBAL(TCPGlobalLock);
LOCK_SOCK(s);
// Check that we are in state which allows writing.
if (s->state & (tcp_StateCLOSED | tcp_StateLASTACK | tcp_StateFINWT1 |
tcp_StateFINWT2 | tcp_StateCLOSING | tcp_StateTIMEWT)) {
len = -1;
goto finish;
}
if(len < 0 ) len = 0;
if( len > (x = s->maxrdatalen - s->datalen) ) len = x;
if( len > 0 ) {
root2xmem( s->buffer + s->datalen, dp, len );
s->datalen += len;
s->datatimer = _SET_TIMEOUT( sock_data_timeout*1000L );
if( s->sock_mode & TCP_LOCAL )
s->sock_mode &= ~TCP_LOCAL;
else {
if( s->sock_mode & TCP_MODE_NONAGLE ) {
tcp_send( s, 40 );
} else {
/* transmit if no unacked data or got complete MTU worth */
if (!s->unacked ||
s->datalen - s->unacked >= s->mss &&
s->window - s->unacked >= s->mss)
tcp_send( s, 41 );
else
tcp_sendsoon( s, 42 );
}
}
}
finish:
UNLOCK_SOCK(s);
UNLOCK_GLOBAL(TCPGlobalLock);
return( len );
}
any help??