Jump to content

Need Help Scheduling a Telnet Session


Recommended Posts

Good evening.

I am looking for best practice for scheduling a telnet session [or similar].

What I am trying to accomplish:

I want to have a scheduled task that will connect to a remote unix system via telnet, login, enter some commands, etc.

I was able to download autoitv3, and I did set this up.

The autoit that I set up launched a command prompt, runs telnet, and then does a few things.

This was fine and dandy.

The challenge occurred when I attempted to schedule this. (I know that I am not the first to experience this problem.)

So, I searched through the documentation, and I see that there are issues with running autoit without having a session on the machine. I figure that I can address this tomorrow morning by creating a dedicated admin station (as I now have ideas of automating all sorts of things, of course...)

Anyway, it still remains that I have the problem that the documentation says that ControlSend (the alternative to Send, which I was originally using) doesn't work that well with the command prompt. Because of this, I am thinking that I need to use a totally different program, such as hyperterminal, or terraterm, or something like that.

In this case, I need to know what telnet program is recommended for using with autoit as a scheduled task? Since I'm not rich, I need a freeware program, if at all possible. Also, if anyone has specific recommendations on how they have conquered a similar issue in the past, this would be optimal.

Like I said before, I can live with the session requirement, but I really need a "recommended" telnet client to use. Hopefully, a telnet client that someone else has used with autoit before, via schedule task.

Thanks for your help.

Please realize that your help can save lots of time, as we currently have to do this process manually, and the vendor doesn't have an automated way to do it (yet). [basically, the process is running some apps on the remote unix system, to start some drivers. Due to some limitations of the user profiles, I am unable to successfully automate the tasks in unix as a script. And, I did originally try to do this in Unix ... but failed .... I even looked into expect, but I had such utter difficulty even getting that installed [not enough unix exp], and after installation, didn't even know what to do with it ... that I came back to the windows side, and I was at least able to get the script to run in Windows.)

Heck, even with the addition of the tools blat and ftp (which I've used extensively in the past) I was able to create files on the 'nix system, and download them, and concatenate for some type of report on what the script does.

Unfortunately, I want to do this as a schedule tasks, and I'm up against the command prompt vs. send/controlsend vs. schedule tasks problem.

If adding an example of the script and batch files would assist you in helping me, please let me know (would take some time, as I'd have to sanitize them a bit first.)

At this time, I think my primary request is that I need a recommended telnet app, due to the command prompt/control send issue in the documentation.

Edited by instant000
Link to comment
Share on other sites

Hello,

I was experiencing the same problem, i made a relayserver which avoids this problem.

Take a look at this;

Normally your script is trying to manipulate data in the application, in this case,.. a telnet application (console).

You are tryting to do this;

Script (gives input) > console, DOS or TELNET (sends this to server) > Server processes the information and might send something back.

What i'm doing to avoid the console manipulation (coz it doesn't work like a charm, to be honest.) is the following.

Console, DOS or TELNET (connects to script) > Script manipulates data and connects to server to send data > server processes the information and might send something back.

I understand you need to schedule it, so you will not need the telnet client, just build your own client which can send commands at a specific time.

#Include <INet.Au3>
TcpStartUp ()
$IP = @IpAddress1 ;scriptserver (telnet client normally connects to this IF NOT SCHEDULED.)
$Port = '23' ;server
$Server = TcpListen ($IP, $Port);server

If @Error Or $Server = '-1' Then Exit MsgBox ('16','Error','Cannot start the server.')

$remoteserver = tcpconnect(TCPNameToIP("mynice.telnetserver.local"), "23") ;script functions as client to the remote unix server.


Do 
   $Client = TcpAccept ($Server);wait for telnet or DOSconsole client from localhost to connect.
   Sleep ('100')
Until $Client <> '-1'

While 1
 $Recv = TcpRecv ($Client, '1000') ;1000 is maxlength
 $Recv1 = TCPRecv($remoteserver, "1000")
 Sleep ('100');


 consolewrite($recv1);What the server gives us back.

  ;example indata information ;

  if stringinstr($recv1, "Hello, welcome to my nice remote unix server with telnetlogin, press enter to continue") > 0 then
    tcpsend ($remoteserver, @crlf) ;send enter coz the telnet server asks for that.
  endif



  tcpsend($remoteserver, $Recv); send info from client to server so connection remains "keepalive"
  tcpsend($client,$recv1); send info from server to client.

WEnd

With this technique (as relayserver) you can just function between the client and server app. You can manipulate alot. Be aware you cannot connect to the script with 127.0.0.1, it functions on LAN addresses like 192.168.1.20. This example script needs some modification if you want to get it to work with your idea's so if you're not too new to autoit you'll figure out how to do this.

Edited by notsure
Link to comment
Share on other sites

Hello,

I was experiencing the same problem, i made a relayserver which avoids this problem.

Take a look at this;

Normally your script is trying to manipulate data in the application, in this case,.. a telnet application (console).

You are tryting to do this;

Script (gives input) > console, DOS or TELNET (sends this to server) > Server processes the information and might send something back.

What i'm doing to avoid the console manipulation (coz it doesn't work like a charm, to be honest.) is the following.

Console, DOS or TELNET (connects to script) > Script manipulates data and connects to server to send data > server processes the information and might send something back.

I understand you need to schedule it, so you will not need the telnet client, just build your own client which can send commands at a specific time.

#Include <INet.Au3>
TcpStartUp ()
$IP = @IpAddress1 ;scriptserver (telnet client normally connects to this IF NOT SCHEDULED.)
$Port = '23' ;server
$Server = TcpListen ($IP, $Port);server

If @Error Or $Server = '-1' Then Exit MsgBox ('16','Error','Cannot start the server.')

$remoteserver = tcpconnect(TCPNameToIP("mynice.telnetserver.local"), "23") ;script functions as client to the remote unix server.


Do 
   $Client = TcpAccept ($Server);wait for telnet or DOSconsole client from localhost to connect.
   Sleep ('100')
Until $Client <> '-1'

While 1
 $Recv = TcpRecv ($Client, '1000') ;1000 is maxlength
 $Recv1 = TCPRecv($remoteserver, "1000")
 Sleep ('100');


 consolewrite($recv1);What the server gives us back.

  ;example indata information ;

  if stringinstr($recv1, "Hello, welcome to my nice remote unix server with telnetlogin, press enter to continue") > 0 then
    tcpsend ($remoteserver, @crlf) ;send enter coz the telnet server asks for that.
  endif



  tcpsend($remoteserver, $Recv); send info from client to server so connection remains "keepalive"
  tcpsend($client,$recv1); send info from server to client.

WEnd

With this technique (as relayserver) you can just function between the client and server app. You can manipulate alot. Be aware you cannot connect to the script with 127.0.0.1, it functions on LAN addresses like 192.168.1.20. This example script needs some modification if you want to get it to work with your idea's so if you're not too new to autoit you'll figure out how to do this.

This looks impressive.

So, potentially, I can do this ...

(1) Make a connection to telnet server

(2) Based on the on-screen prompts, I can then login

(3) based on the next prompt, I can enter a password

(4) Based on the MOTD/login screen, I can then enter all of my commands sequentially, taking advantage of the unix command buffering.

Can I still use the normal Wait() syntax within this? Or do I need to do something else?

Thanks for all of your help, please answer the question about the Wait() syntax. I can work at this today, and can attempt scheduling it to run tomorrow AM (since the app is used throughout the day, I have to comment out certain parts of the script.)

Link to comment
Share on other sites

This looks impressive.

So, potentially, I can do this ...

(1) Make a connection to telnet server

(2) Based on the on-screen prompts, I can then login

(3) based on the next prompt, I can enter a password

(4) Based on the MOTD/login screen, I can then enter all of my commands sequentially, taking advantage of the unix command buffering.

Can I still use the normal Wait() syntax within this? Or do I need to do something else?

Thanks for all of your help, please answer the question about the Wait() syntax. I can work at this today, and can attempt scheduling it to run tomorrow AM (since the app is used throughout the day, I have to comment out certain parts of the script.)

You mean the "Sleep()" command? What are you trying to accomplish? This construction will loop forever and waits for data to come in at TCPRECV.

You can use "sleep()" for whatever reason you want to use it in this code but i can't see the advantages of using those? Just wait for the unixserver to respond, capture the response and anticipate on this serverresponse.

Please explain more what you're trying to do.

Regards,

Notsure.

Link to comment
Share on other sites

This looks impressive.

So, potentially, I can do this ...

(1) Make a connection to telnet server

(2) Based on the on-screen prompts, I can then login

(3) based on the next prompt, I can enter a password

(4) Based on the MOTD/login screen, I can then enter all of my commands sequentially, taking advantage of the unix command buffering.

Can I still use the normal Wait() syntax within this? Or do I need to do something else?

Thanks for all of your help, please answer the question about the Wait() syntax. I can work at this today, and can attempt scheduling it to run tomorrow AM (since the app is used throughout the day, I have to comment out certain parts of the script.)

My apologies. I reread your script. It plainly has a sleep() statement in there.

I apologize for the silly question.

I was unable to edit my post (is this a browser limitation, or a forum rule that I failed to comprehend?)

Link to comment
Share on other sites

My apologies. I reread your script. It plainly has a sleep() statement in there.

I apologize for the silly question.

I was unable to edit my post (is this a browser limitation, or a forum rule that I failed to comprehend?)

The main reason why i put sleeps in there is to slowdown the loop and spare CPUusage. Your script shouldn't rely on SLEEP's, thats kinda "ugly" :) TCPSEND's should be based on incoming data rather than sleeps.

Your post should be editable tho.

Let me know if the script is going to work this way. To schedule it, you could just make a GUI with 1 textbox like this;

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form2 = GUICreate("Schedulescript", 277, 72, 302, 218)
$txtTime = GUICtrlCreateInput("", 144, 32, 121, 21)
$Label1 = GUICtrlCreateLabel("Time to execute script;", 24, 32, 111, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

EndSwitch

if @hour & ":" & @min = guictrlread ($txtTime) Then
    _g0g0g0()
        guictrlsetdata($txttime, "")
EndIf
sleep(100)

WEnd


func _g0g0g0()


    msgbox(64, "!!!1", "Omg all your nice functions here :O")

EndFunc
Edited by notsure
Link to comment
Share on other sites

The main reason why i put sleeps in there is to slowdown the loop and spare CPUusage. Your script shouldn't rely on SLEEP's, thats kinda "ugly" :) TCPSEND's should be based on incoming data rather than sleeps.

Your post should be editable tho.

Let me know if the script is going to work this way. To schedule it, you could just make a GUI with 1 textbox like this;

Sorry, I wanted the sleeps for troubleshooting purposes. I wanted to follow along with what the script was doing visually. (So far, I run the autoit script, by calling it from a larger batch file ... and I currently have a "pause" at the end of the batch file, but if I add "sleep" strategically within the batch file, I can slow it down enough to read everything that occurs, as it occurs.)

Then, if everything visual checked out, I'd schedule it, and then check on the resultant log files to see if everything worked like it is supposed to.

If everything checks out, then of course, I could remove the sleep statements later. ... the primary purpose is to slow it down enough for me to read what occurs.

Please tell me that there is some robust logging feature built into autoit that voids my having to watch over it in this manner?

Thanks!

NOTE: I figured out the editing restriction, I was trying to edit the post after you had replied to it. (I Hadn't refreshed my screen.)

Edited by instant000
Link to comment
Share on other sites

OK, now I see that I'm still lost.

I thought that it was simple, and I only had to make minor modifications.

However, I tried to modify it, and it didn't work for me, at all.

Let me show you what I attempted, and please tell me why it did not work.

I thought this is what would happen:

1. telnet XXservername.XXdomainname.local

2. wait for the string: "login:"

3. Send: XXusername

4. Send: Enter

But, instead, I run this, and get "cannot start server"

I am missing something. Do I have some understanding backwards?

#Include <INet.Au3>
TcpStartUp ()
$IP = @IpAddress1 ;scriptserver (telnet client normally connects to this IF NOT SCHEDULED.)
$Port = '23' ;server
$Server = TcpListen ($IP, $Port);server

If @Error Or $Server = '-1' Then Exit MsgBox ('16','Error','Cannot start the server.')

$remoteserver = tcpconnect(TCPNameToIP("XXservername.XXdomain.local"), "23") ;script functions as client to the remote unix server.


Do 
   $Client = TcpAccept ($Server);wait for telnet or DOSconsole client from localhost to connect.
   Sleep ('100')
Until $Client <> '-1'

While 1
 $Recv = TcpRecv ($Client, '1000') ;1000 is maxlength
 $Recv1 = TCPRecv($remoteserver, "1000")
 Sleep ('100');


 consolewrite($recv1);What the server gives us back.

  ;example indata information ;

  if stringinstr($recv1, "login:") > 0 then
    tcpsend ($remoteserver, "XXuser"); send username
    tcpsend ($remoteserver, @crlf) ;send enter coz the telnet server asks for that.
  endif



  tcpsend($remoteserver, $Recv); send info from client to server so connection remains "keepalive"
  tcpsend($client,$recv1); send info from server to client.

WEnd

Hrm, I now get "cannot start the server." (maybe I hadn't saved before)

I am missing something simple here ... but I don't know what.

Edited by instant000
Link to comment
Share on other sites

Ok. Try this:

Start the script. Go into dos and type :

telnet [YOUR_LOCAL_IPADDRESS] (example : 192.168.1.20)

If your domain is filled in properly here: $remoteserver = tcpconnect(TCPNameToIP("XXservername.XXdomain.local"), "23") the script will connect to that remote telnet server.

In the autoit console will be logged what you receive from the server. Consolewrite($recv1) takes care of that. If you do not see the outputwindow in SCITE, go to SCITE and press F8.

Carefully watch what the server says and respond in a proper way to that.

Post some output which the server gives you and paste it here plz.

Edit: put a TCPshutdown() in your script at the end to close the connection.

Edited by notsure
Link to comment
Share on other sites

Ok. Try this:

Start the script. Go into dos and type :

telnet [YOUR_LOCAL_IPADDRESS] (example : 192.168.1.20)

If your domain is filled in properly here: $remoteserver = tcpconnect(TCPNameToIP("XXservername.XXdomain.local"), "23") the script will connect to that remote telnet server.

In the autoit console will be logged what you receive from the server. Consolewrite($recv1) takes care of that. If you do not see the outputwindow in SCITE, go to SCITE and press F8.

Carefully watch what the server says and respond in a proper way to that.

Post some output which the server gives you and paste it here plz.

Edit: put a TCPshutdown() in your script at the end to close the connection.

Woah! That worked. (I literally said *woah*. The code didn't make sense until it actually worked! I somehow thought it wanted me to telnet to my local system, but it didn't make sense in my head, as I wanted to telnet elsewhere.

But, it's working, so how can I complain now?

Anyway, I did as you instructed, and it worked! Yay!

Now, the task of filling out the rest of the script.

So, I'm assuming that if I want to script this out, I'm required to do the following:

(1) create a batch file

(2) call this .au3 script first

(3) then call telnet <servername>

(4) at that point, the running script (which, is actually a running telnet relay, does all of the telnet magic for me)

(5) after the script completes, I can continue my batch job at the command following the telnet?

Will this be properly handled? Do you know what I'm referring to?

Link to comment
Share on other sites

Woah! That worked. (I literally said *woah*. The code didn't make sense until it actually worked! I somehow thought it wanted me to telnet to my local system, but it didn't make sense in my head, as I wanted to telnet elsewhere.

But, it's working, so how can I complain now?

Anyway, I did as you instructed, and it worked! Yay!

Now, the task of filling out the rest of the script.

So, I'm assuming that if I want to script this out, I'm required to do the following:

(1) create a batch file

(2) call this .au3 script first

(3) then call telnet <servername>

(4) at that point, the running script (which, is actually a running telnet relay, does all of the telnet magic for me)

(5) after the script completes, I can continue my batch job at the command following the telnet?

Will this be properly handled? Do you know what I'm referring to?

OK, I'm having an issue with getting this to work properly.

How can I call this script and then start the actual telnet without being at the keyboard?

Do I do it with multiple tasks? (This is the simplest way I can imagine it right now, but I want to make sure it can't be done another way.)

Schedule task 1: call the telnetrelay.au3 @ 11:30:00 hours

Schedule task 2: call the telnet <192.168.1.10> @ 11:30:45 hours ; 45 seconds later

Is there any method to do this "all in one"?

It appears that the telnet relay has to be running, prior to making the telnet connection to local machine, and as it appears to suspend until it has completed all of its operations, I can't launch this and then the telnet command from within the same batch file.

Am I correct?

OK, This didn't work as expected for me. This is what I did:

Everything else appears irrelevant at this point, as this is the part that is hanging me up.

(1) task1.bat:

%pstooldir%\%process% /accepteula -i -s %autoitprog% "%scriptdir%\%autoitscript%"

(2) task2.bat:

telnet %ipaddress%

Here is the autoit script I'm using:

#Include <INet.Au3>
TcpStartUp ()
$IP = @IpAddress1 ;scriptserver (telnet client normally connects to this IF NOT SCHEDULED.)
$Port = '23' ;server
$Server = TcpListen ($IP, $Port);server

If @Error Or $Server = '-1' Then Exit MsgBox ('16','Error','Cannot start the server.')

$remoteserver = tcpconnect(TCPNameToIP("XXServer.XXDomain.local"), "23") ;script functions as client to the remote unix server.


Do
   $Client = TcpAccept ($Server);wait for telnet or DOSconsole client from localhost to connect.
   Sleep ('100')
Until $Client <> '-1'

While 1
 $Recv = TcpRecv ($Client, '1000') ;1000 is maxlength
 $Recv1 = TCPRecv($remoteserver, "1000")
 Sleep ('100');


 consolewrite($recv1);What the server gives us back.

  ;example indata information ;

  if stringinstr($recv1, "login:") > 0 then
    tcpsend ($remoteserver, "XXUserName"); send username
    tcpsend ($remoteserver, @crlf) ;send enter coz the telnet server asks for that.
  endif
if stringinstr($recv1, "password:") > 0 Then
    tcpsend ($remoteserver, "XXPassword");send password
    tcpsend ($remoteserver, @crlf) ;send enter to finish sending the password.
    TCPShutdown()
EndIf


  tcpsend($remoteserver, $Recv); send info from client to server so connection remains "keepalive"
  tcpsend($client,$recv1); send info from server to client.

WEnd

First, I launch task 1.

Then, I launch task 2.

The running relay server does run as expected, and does put the information into the window launched by task 2, but it doesn't exit.

Edited by instant000
Link to comment
Share on other sites

I fixed the problem.... kind of, I was able to schedule a batch file that included tst10.exe.

This utility is called "telnet scripting tool" "tst10.exe". It's available in various places, and since it's not an autoit solution, I know it's not appropriate, but since it solved my problem, I'm just letting others know.

It can be launched from comamnd line, and you can call the batch that uses it from the command line. (and the key, is that you can schedule it without being logged in!)

It doesn't appear capable of using variables as its input, so that may cause some pain for some people, but it solved an issue that I had for the past few months, where I needed to do a manual task every Monday morning, and now I do not! That's what computers are for, right?

I found this after the fact, in looking up some documentation for it, but here is a link that explains it a bit more:

http://jerrymannel.com/blog/2008/11/11/telnet-scripting-tool-aka-tst10exe/

Edited by instant000
Link to comment
Share on other sites

Sorry i havent been following this forum for couple of days coz im working on a script myself.

Good to see you fixed the issue yourself :)

Edit:

The reason why it works to telnet to yourself is coz the script (on your own PC) is accepting that connection and immediately connects to the "real" server. Its just functioning as relayserver. Your telnet client connects to the script and the script connects to the server. Just so you know :)

Edited by notsure
Link to comment
Share on other sites

Sorry i havent been following this forum for couple of days coz im working on a script myself.

Good to see you fixed the issue yourself :)

Edit:

The reason why it works to telnet to yourself is coz the script (on your own PC) is accepting that connection and immediately connects to the "real" server. Its just functioning as relayserver. Your telnet client connects to the script and the script connects to the server. Just so you know :)

OK, thanks, yes, I gathered that much after it worked. Your explanations which you posted originally said as much, but I didn't understand until I saw to launch things in the correct order.

My ONLY problem was that I could not make the launched telnet session exit automatically.

How do I END the session after sending the commands that I want?

I attached what I was using to an earlier post.

I attached the autoit script that I was using.

Link to comment
Share on other sites

  • 3 weeks later...

OK, thanks, yes, I gathered that much after it worked. Your explanations which you posted originally said as much, but I didn't understand until I saw to launch things in the correct order.

My ONLY problem was that I could not make the launched telnet session exit automatically.

How do I END the session after sending the commands that I want?

I attached what I was using to an earlier post.

I attached the autoit script that I was using.

TCPclose($socket)

..

Or send commands which will make the program exit. I suppose you have a "proper way" to end the session how men would do it without the script.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...