• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4455
  • Last Modified:

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
0
MotoCrazy
Asked:
MotoCrazy
  • 12
  • 9
  • 2
  • +1
1 Solution
 
pablovrCommented:
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
0
 
Shift-3Commented:
No, not with the built-in telnet client.  However, the free client Putty includes the tool Plink which does support scripting.
0
 
QlemoC++ DeveloperCommented:
I personally use netcat in a slightly adapted version (delays in milliseconds instead of seconds) to do such things (see netcat.sourceforge.net/).
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
MotoCrazyAuthor Commented:
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
0
 
MotoCrazyAuthor Commented:
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?
0
 
MotoCrazyAuthor Commented:
Wow, had three people jump on the question, but where did you all go? LOL

0
 
Shift-3Commented:
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.
0
 
QlemoC++ DeveloperCommented:
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 ;-)
0
 
MotoCrazyAuthor Commented:
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.
0
 
MotoCrazyAuthor Commented:
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?
0
 
QlemoC++ DeveloperCommented:
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.
0
 
MotoCrazyAuthor Commented:
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?
0
 
QlemoC++ DeveloperCommented:
Not any easy way which I knew of. I can think only of one approach while using netcat: netcat in Listen mode, as a pipe proxy. It's tricky, but it might work.

First, the netcat will start in Listen mode, as a background process. It listens to local port 60023, and piping whatever comes over that port to the target. Because of -L (instead of -l), netcat remains in listen mode after a connection (on 60023) is closed - why need this will follow.
Then era is pushed to the listening netcat, which pushes it again to the target. The connection to 60023 is closed, but because of -L, the "server" process does not stop.
This gives us the chance to wait for 4 seconds (ping -n 5), and paste the rest of data to the target via the listening netcat.
The listening netcat does not terminate by itself, so we have to kill it after work is done.


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

Open in new window

0
 
MotoCrazyAuthor Commented:
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.
0
 
QlemoC++ DeveloperCommented:
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

0
 
MotoCrazyAuthor Commented:
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
0
 
QlemoC++ DeveloperCommented:
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.
0
 
MotoCrazyAuthor Commented:
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.
0
 
QlemoC++ DeveloperCommented:
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.
0
 
MotoCrazyAuthor Commented:
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
0
 
QlemoC++ DeveloperCommented:
Yes, a blank line should do.

0
 
MotoCrazyAuthor Commented:
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.
0
 
QlemoC++ DeveloperCommented:
Any response will be shown in the netcat window started with "NetCat Waiting".
0
 
MotoCrazyAuthor Commented:
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! =)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 12
  • 9
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now