Link to home
Start Free TrialLog in
Avatar of MotoCrazy
MotoCrazyFlag for United States of America

asked on

Can you send batch file commands to a TelNet session?

I would like a batch file that logs into a simple telnet session, logs in, performs a couple commands, then terminates. How can this be done? My humble attempt started the TelNet session, but did not enter the rest of the commands until after I manually terminated the session.

Would appreciate the help.

ie.
====================
telnet ip.add.re.ss
[username]
[password]
[carriage return]
[command]
[command]
exit
Avatar of pablovr
pablovr
Flag of Mexico image

Does it have to be with telnet?

You might consider PSEXEC:
http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

"PsExec is a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software. PsExec's most powerful uses include launching interactive command-prompts on remote systems and remote-enabling tools like IpConfig that otherwise do not have the ability to show information about remote systems."

So, you could write a batch file, which runs commands using PSEXEC
No, not with the built-in telnet client.  However, the free client Putty includes the tool Plink which does support scripting.
Avatar of Qlemo
I personally use netcat in a slightly adapted version (delays in milliseconds instead of seconds) to do such things (see netcat.sourceforge.net/).
Avatar of MotoCrazy

ASKER

I sincerely appreciate all the answers, but unfortunately I had an emergency come up and have to leave work. I will not be able to try any of these solutions out until Monday. Just an FYI so you don't think I abandoned the question.

To [possibly] answer some questions, this is for automatically clocking in and out of Reynolds and Reynolds (car dealer database software). We use a WIN32 application called ERA that uses the telnet protocol for communicating with the server. My boss said no more overtime pay (or else...), but I run the shop and typically end up working 9-10 hours a day. I want to automate my time clock so I get perfect 8.0 hours every day, and no more getting yelled at for clocking out 30 minutes late, LOL.

Again, thank you and I will try these out on Monday.

Eric
Okay, sorry about the delay.

I downloaded both PsExec and PuTTY, but I am not familiar with either of these and am not sure how to do what I need them to do. If I were to list the commands individually, they would look like this (return key pressed after each line):

telnet x.x.x.x
era
CLOCK1
[RETURN]
[USER]
[PASS]
1
E
EXIT

Any ideas on how to accomplish this with any of the above programs?
Wow, had three people jump on the question, but where did you all go? LOL

I don't really have anything to add to the examples given in that Plink link, particularly the ones under the "Using Plink in batch files and scripts" heading.
No, PLink is definitely not part of the solution. You can't login automatically using telnet, only with SSH. netcat is able to do. You would put your commands into a textfile as you want them to be entered. let's call that cheat-the-clock.txt, and push that into the command:

nc -i 1 -t x.x.x.x < cheat-the-clock.txt

Because of the (necessary) delay of 1 second (-i 1), the command will perform slow. On  the other hand, that is more "human" behaviour ;-)
Olemo, what you are talking about seems like the best option so far, but I checked out the link (netcat.sourceforge.net), and only found Linux/Unix files. I am running M$ Winblows. Is there a version that would be compatible with XP/Vista?

Scratch that... found it here:
http://www.securityfocus.com/tools/139

Got busy today and just got home from work. I will try this out first thing Monday morning.

Thank you.
Okay... I created a TXT file, named "login.txt", with the following lines inside:
--------------------------------------------------------
era
CLOCK1

[EMP#]
[PASS]
1
E
E
EXIT
--------------------------------------------------------

And when I run NetCat as you specified above (nc -i 1 -t x.x.x.x < login.txt), I get an error, "no port[s] to connect to".

I tried specifying a port (nc -i 1 -t x.x.x.x:23 < login.txt), and I get an error, "x.x.x.x:23: forward host lookup failed: h-errno 11004: NO_DATA"

Any ideas?
nc -i 1 -t x.x.x.x 23 < login.txt
should do. The port must follow the IP address, with a space as delimiter.
Communication starts succesfully now, but how do you control time between commands? I need about a four second pause between "era" and "CLOCK1". I tried changing the line to:

nc -i 4 -t x.x.x.x 23 < login.txt

and it seems to pause four seconds before entering the "era" command, but the rest of the commands are entered immediately after. Is there a way to pause between each line? Not just before the first command?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for all of your attempts, Olemo, but still no good. It never starts the telnet session (that I can see) and freezes after:

C:\NC>start "NetCat Waiting" /min cmd /c "nc -L -p 60023 -t | nc -t x.x.x.x 23"
C:\NC>echo era   | nc 127.0.0.1 60023
[BLINKING CURSOR]

Had to "CTRL+BREAK" out of it.
Don't know how this was hidden from my attention, sorry. Here you go. The changes are that nc times out itself after 5 seconds (-w 5) after sending era.


start "NetCat Waiting" /min cmd /c "nc -L -p 60023 -t | nc -t x.x.x.x 23"
echo era | nc -t 127.0.0.1 60023 -w 5
nc -t 127.0.0.1 60023 -w 1 < login.txt
taskkill /IM nc.exe /f

Open in new window

Well, now it does SOMETHING, LOL, but it does not seem to be passing the commands to TelNet. When run, I get:

C:\NC>logout.bat
C:\NC>start "NetCat Waiting" /min cmd /c "nc -L -p 60023 -t | nc -t x.x.x.x 23"
C:\NC>echo era | nc -t 127.0.0.1 60023 -w 5
C:\NC>nc -t 127.0.0.1 60023 -w 1 0<logout.txt
C:\NC>taskkill /IM nc.exe /f
SUCCESS: The process "nc.exe" with PID 1304 has been terminated.
SUCCESS: The process "nc.exe" with PID 2976 has been terminated.
C:\NC>

I then logged into the system normally to check, and I was not clocked out. It does not even have a record of me logging into the time clock. Hmmm...

I think I may just give up on this one. Cheaters never prosper... eh? LOL
This might be a timing issue. Are you expecting some responses from the clocking server which you could check? If so, the nc started with the "start" cmd should show that responses. And you can deal with the -w switches, I might have set them too tight.
What I typed above was exactly as it appeared when run. None of the responses were shown, if there were any. I can try playing with -w switches a little, but I don't think that is the issue. I do not know what happened in the background, but what I "saw" didn't suggest anything happened at all, and the Reynolds system did not show that I clocked out.
I checked it against a telnet server - and that worked. I guess there are some anti-cheating issues, or the commands used are not exactly the expected ones.
Regardless if it works, I will give some points for the time you spent with me, but one more quick question... how would I send a blank command (carriage return). This may be the issue. I just have a blank line in the text file, but I am not sure if that will do the trick.

era   - First command
CLOCK1   - Second command
[RETURN]   - Third, need a simple ENTER. I have a blank line here.
NAME
NUMBER
1
E
Yes, a blank line should do.

I have been messing with this all morning, and I cannot get it to work. I tried changing the wait times, the order of the commands... I don't know what else to try. The server shows no records of my login attempts.

Is there any way to echo everything the TelNet session responds with? I am only seeing the commands from the batch file and cannot tell if anything is even happening.
Any response will be shown in the netcat window started with "NetCat Waiting".
Qlemo, all of your solutions got me THAT MUCH closer to doing what I want, but I have been tinkering with it for a while now, and I think the problem lies with the server-side code right after the "era" login. When I manually telnet into the server, I enter "era" as the username, a blank password, then it spits out mostly gibberish with a barely-recognizable "Username:" at the end. I know to type my username, password, then the normal keys I would press to clock in or out in the application. It is probably all formatting code, but I think that is causing the problem. Guess I have to follow the rules. =P

Thanks for all the help, though. You certainly know your stuff! =)