Sign in to follow this  
Followers 0
uspino

Help with simple Telnet script - I feel useless

7 posts in this topic

Here's my situation. I've written a not-very-elegant vbs script to change channels on my TiVo via Telnet. The script reads the argument from the command line and changes to that channel. Due to complicated reasons (my HTPC software cannot trigger a telnet script to change channels) I need to turn that into an exe file.

What I just need is to telnet my TiVo IP & port number (192.168.1.153 31339) and send the string "SETCH " plus the number introduced as command line argument.

I'm new to AutoIt and I can't even figure out how to run a telnet window from a script. I've read about TCPSend command but that looks way too complicated for me. Is there a simple way to do this? Any help would be very much appreciated.

This is the vbs script (please be gentle with my lack of programming skills):

'Use argument parameter as channel number

'

Dim ArgObj, var1

Set ArgObj = WScript.Arguments

Set WshShell = WScript.CreateObject("WScript.Shell")

var1 = ArgObj(0)

'

'

'Create the shell object

set oShell = CreateObject("WScript.Shell")

'

'Start up command prompt

oShell.run"cmd.exe"

WScript.Sleep 50

'

'Send keys to active window; change the IP address to that of your TiVo. Keep the port number.

oShell.SendKeys"telnet 192.168.1.153 31339"

'Emulate the enter key

oShell.SendKeys ("{Enter}")

WScript.Sleep 50

'

'Send the channel number to TiVo

oShell.SendKeys"SETCH "

oShell.SendKeys var1

oShell.SendKeys("{Enter}")

WScript.Sleep 50

'

'Exit and close window

Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}")

Set objProcess = objWMI.InstancesOf("Win32_process")

For each Process in objProcess

If (instr(1,lcase(Process.name),lcase("telnet.exe")) > 0) then

Process.terminate 0

End If

Next

oShell.SendKeys"exit{ENTER}"

oShell.SendKeys("{Enter}")

oShell.SendKeys"% "

Share this post


Link to post
Share on other sites



Hi Uspino and Welcome :x

I know nothing about Telnet into TiVo but the example given in the help file for TCPSend() function should work as-is (except you need to change the ip address and port number) as long as you don't have to login to the telnet session. The example will prompt for what you want to send to the server, but it's easy enough to modify for your purposes...

Share this post


Link to post
Share on other sites

Uspino, try the following:

#include <GUIConstantsEx.au3>

Opt("MustDeclareVars", 1)

Global $channel, $submit, $input, $chnl, $socket
Global $ip = "192.168.1.153", $port = "31339"

GUICreate("Telnet TiVO Channel Changer", 280, 100)
GUICtrlCreateLabel("Please input the channel number:", 20, 30)
$channel = GUICtrlCreateInput("", 230, 25, 40)
$submit  = GUICtrlCreateButton("OK", 110, 60, 60, 30)
GUISetState()

While 1
    
    $input = GUIGetMsg()
    
    if $input = $submit Then
        $chnl = GUICtrlRead($channel)
        if TCPStartup() Then
            if $socket = TCPConnect($ip, $port) Then
                if TCPSend($socket, "SETCH " & $chnl) = 0 Then
                    MsgBox(0, "Failure", "The channel could not be changed, please try again!")
                    ExitLoop
                EndIf
            TCPCloseSocket($socket)
            Else
                MsgBox(0, "Failure", "Could not connect to " & $ip & ":" & $port & "." & @CRLF & "Please try again.")
                ExitLoop
            EndIf
            TCPShutdown()
            MsgBox(0, "Sucess", "TCP command sent successfully." & @CRLF & "You should now be on the specified channel.")
        Else
            MsgBox(0, "Failure", "Could not start TCP.")
        EndIf
        ExitLoop
    EndIf
    
    if $input = $GUI_EVENT_CLOSE Then
        ExitLoop
    EndIf
WEnd
Exit

Please let me know if it works/doesn't work, so I will be able to provide further assistance if required.

shanet


[font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm

Share this post


Link to post
Share on other sites

Hi MrMitchell & shanet. Guys, you don’t know how much I appreciate your help. I’ve been able to find what I was looking for thanks to you both. I’ll explain…

Shanet, you script was terrific and worked fine, but it couldn’t work in my scenario. The HD-PVR software con trigger an external “exe” application and can add the channel number as a command line argument. Your script asks for the channel number, so it couldn’t be used unattended.

Ultimately, MrMitchell was right: follow the example and just go simple. So based on that advice and thanks to what I learned playing with shanet script, here’s what I did.

I wanted other people (TiVo users in the AVSForum and TheGreenButton communities) to use the script, so it couldn’t contain the internal LAN address of my own particular TiVo. What I did was this little application:

TCPStartup()
$fp = TCPConnect($CmdLine[1], $CmdLine[2])

Sleep(300)
TCPSend($fp, "SETCH " & $CmdLine[3] & @CRLF)
Sleep(300)
$ack = TCPRecv($fp, 40)

TrayTip("TiVo Channel Change", $ack, 10, 0)
Sleep(2000)
TCPShutdown()

I compiled it into a “TiVoChannelChanger.exe” file and I can use it like this:

C:\TiVoChannelChanger.exe 192.168.1.153 31339 600

It reads the first argument as IP address, the second as port number and the third as channel number. That way, anybody can use it. And magically… it works! I added the cool tray bubble to get some feedback (I had to add some delay to get it working - don’t really know why).

Again, thanks a lot for helping me. It’s my first application – I feel a like a proud geek who just invented the wheel – thanks to you.

Please take a look at the script and let me know if there’s something wrong with it.

Share this post


Link to post
Share on other sites

Hi MrMitchell & shanet. Guys, you don’t know how much I appreciate your help. I’ve been able to find what I was looking for thanks to you both. I’ll explain…

Shanet, you script was terrific and worked fine, but it couldn’t work in my scenario. The HD-PVR software con trigger an external “exe” application and can add the channel number as a command line argument. Your script asks for the channel number, so it couldn’t be used unattended.

Ultimately, MrMitchell was right: follow the example and just go simple. So based on that advice and thanks to what I learned playing with shanet script, here’s what I did.

It's always good to hear that someone always got something out of what you provided. Its also good to hear that you learned something from my script. Well done :P

I wanted other people (TiVo users in the AVSForum and TheGreenButton communities) to use the script, so it couldn’t contain the internal LAN address of my own particular TiVo. What I did was this little application:

TCPStartup()
$fp = TCPConnect($CmdLine[1], $CmdLine[2])

Sleep(300)      ;Were these the sleeps you said you had to include to make the script work?
TCPSend($fp, "SETCH " & $CmdLine[3] & @CRLF)
Sleep(300)      ;And this sleep?
$ack = TCPRecv($fp, 40)

TrayTip("TiVo Channel Change", $ack, 10, 0)
Sleep(2000)     ;And this one?
TCPShutdown()

I compiled it into a “TiVoChannelChanger.exe” file and I can use it like this:

C:\TiVoChannelChanger.exe 192.168.1.153 31339 600

It reads the first argument as IP address, the second as port number and the third as channel number. That way, anybody can use it. And magically… it works! I added the cool tray bubble to get some feedback (I had to add some delay to get it working - don’t really know why).

If you look at the code, I commented on it. I am just wondering if that's the delays you are talking about?

Also, anyone wishing to further speed up their script can put the following batch file in the same directory as the exe, and then they only need a channel number, as they can all pre-define their details in their batch files:

1. Open Notepad.

2. Type the following, replacing the appropriate data:

@echo off
cls
REM This will change the channel to whatever the command line parameter is by using pre-defined settings passed on to TiVoChannelChanger.exe in this directory.
REM %1 looks for the command line parameter.
TiVoChannelChanger.exe (IP address goes here) (port goes here) %1

3. Save it in your TiVoChannelChanger.exe directory as (whatever).cmd and launch the file from command prompt like so:

C:\> (whatever).cmd 1

assuming you wanted channel '1'.

By using a batch file, anyone can change their details. furthermore, if you included another piece of script, implemented however you want, you could give the end user a GUI to create this batch file!

This would be done like so (lets just move it to C:\TiVo):

#include <GUIConstantsEX.au3>

Opt("MustDeclareVars", 1)
Local $winhandle, $submit, $cancel, $ip, $port, $msg

$winhandle = GUICreate("Automate", 260, 300)
GUICtrlCreateLabel("By inputting the correct details a file" & @CRLF & "will be created that will allow you" & @CRLF & "to pass only the channel to the" & @CRLF & "created file instead of the IP address" & @CRLF & "port and channel.", 30, 20)
$submit = GUICtrlCreateButton("OK", 160, 240, 60)
$cancel = GUICtrlCreateButton("Cancel", 60, 240, 60)
$ip = GUICtrlCreateInput("IP Address", 30, 120, 200)
$port = GUICtrlCreateInput("Port Number", 30, 180, 200)
GUISetState()

While 1
    $msg = GUIGetMsg()
    
    if $msg = $submit Then
        $ip = GUICtrlRead($ip)
        $port = GUICtrlRead($port)
        Sleep(500)
        if FileWrite("C:\TiVo\Automate.cmd", "@echo off" & @CRLF & "cls" & @CRLF & "TiVoChannelChanger.exe "& $ip & " " & $port & " %1") Then
            MsgBox(0, "Success!", "The batch file was successfully created.")
        Else
            MsgBox(0, "Failure!", "The batch file was not successfullly created.")
        EndIf
        Sleep(500)
        ExitLoop
    EndIf
    if $msg = $cancel or $msg = $GUI_EVENT_CLOSE Then
        GUIDelete($winhandle)
        ExitLoop
    EndIf
WEnd

As I said, you can implement that how you wish, if you wish, edit it I dont mind, that's your script :shifty:

However, may I suggest you implement it with a command line parameter of /automate or similar that triggers it?

Again, thanks a lot for helping me. It’s my first application – I feel a like a proud geek who just invented the wheel – thanks to you.

Please take a look at the script and let me know if there’s something wrong with it.

It looks all good, however I do not have a tivo to try it on. So the question is for you. Does it work? Is there anything wrong with it?

Well done on your first script uspino.

I look forward to see you helping Microsoft deal with their problems soon :x

shanet


[font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm

Share this post


Link to post
Share on other sites

It looks all good, however I do not have a tivo to try it on. So the question is for you. Does it work? Is there anything wrong with it?

Well done on your first script uspino.

I look forward to see you helping Microsoft deal with their problems soon :x

shanet

shanet, it does work, which to me, amateur as I am, is surprising and strangely fulfilling. And I will indeed apply for a job at Microsoft once I get fired from my day job after getting hooked on AutoIt. I'll probably have a shot as a janitor there.

Anyway, thanks to your help and encouragement I've modified the script to read IP and port number from an INI file and to allow other TiVo remote button functions, not only channel change. I've also included my first If....Else routine, of which I'm really proud. I got rid of one of the delays buy I still need two of them: one to allow the TiVo to return a readable value (I can't explain why it takes a little time) and one to give the Windows tray bubble 2 seconds before disappearing.

Here's the script.

And again, big thanks.

$WORKINGDIR = @ScriptDir ; Reads INI file from same directory as EXE file
$var1 = IniRead($WORKINGDIR & "\TiVoRemote.ini", "TiVo LAN IP and Port Number", "IP", "IP Address Not Found in INI file") ; Reads IP from INI file
$var2 = IniRead($WORKINGDIR & "\TiVoRemote.ini", "TiVo LAN IP and Port Number", "Port", "Port Number NotFound in INI file") ; ; Reads Port from INI file


TCPStartup() ; Opens Telnet Connection
$fp = TCPConnect($var1, $var2) ; Telnets TiVo at provided IP and Port Number

TCPSend($fp, $CmdLine[1] & " " & $CmdLine[2] & @CRLF) ; Sends Commands to TiVo
Sleep(390) ; Allows time to TiVo to return Local and Remote Channel Status
$ack = TCPRecv($fp, 40)

If $CmdLine[1] = "SETCH" Then ; Provides Windows Tray Bubble Feedback only on channel chenge
        TrayTip("TiVo Channel Change", $ack, 10, 1)
        Sleep(2000) ; Gives tray bubble 2 seconds to disappear
        TCPShutdown()
    Else
        TCPShutdown()
EndIf
Exit

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

shanet, it does work, which to me, amateur as I am, is surprising and strangely fulfilling. And I will indeed apply for a job at Microsoft once I get fired from my day job after getting hooked on AutoIt. I'll probably have a shot as a janitor there.

Very good then :x

Anyway, thanks to your help and encouragement I've modified the script to read IP and port number from an INI file and to allow other TiVo remote button functions, not only channel change. I've also included my first If....Else routine, of which I'm really proud. I got rid of one of the delays buy I still need two of them: one to allow the TiVo to return a readable value (I can't explain why it takes a little time) and one to give the Windows tray bubble 2 seconds before disappearing.

You are welcome. It is good to see you up and running now. Now that I think about it, an ini file would be better than a batch file. Well done! Also, again well done for including different functions, if I were you I would put them in a new function(in the program... if you haven't already-search functions in the help file).

As far as the first sleep to return a value, the reason it requires time to return the value is because it needs send the data to your router, which needs to find which device to give it to, which then has to recieve the data, compute the data, compute an output and send that output back to the router which finds who to send that data to and sends it. Your program would do this without the sleep:

I have given you the data now give me a response. No response lets find out what is next without a value. Then, if/when that value is required, it will return 0 (false/fail).

That sleep makes it hang before asking for input, so the data can be sent back and recieved by the program.

Here's the script.

And again, big thanks.

$WORKINGDIR = @ScriptDir ; Reads INI file from same directory as EXE file
$var1 = IniRead($WORKINGDIR & "\TiVoRemote.ini", "TiVo LAN IP and Port Number", "IP", "IP Address Not Found in INI file") ; Reads IP from INI file
$var2 = IniRead($WORKINGDIR & "\TiVoRemote.ini", "TiVo LAN IP and Port Number", "Port", "Port Number NotFound in INI file") ; ; Reads Port from INI file


TCPStartup() ; Opens Telnet Connection
$fp = TCPConnect($var1, $var2) ; Telnets TiVo at provided IP and Port Number

TCPSend($fp, $CmdLine[1] & " " & $CmdLine[2] & @CRLF) ; Sends Commands to TiVo
Sleep(390) ; Allows time to TiVo to return Local and Remote Channel Status
$ack = TCPRecv($fp, 40)

If $CmdLine[1] = "SETCH" Then ; Provides Windows Tray Bubble Feedback only on channel chenge
        TrayTip("TiVo Channel Change", $ack, 10, 1)
        Sleep(2000) ; Gives tray bubble 2 seconds to disappear
        TCPShutdown()
    Else
        TCPShutdown()
EndIf
Exit

If I may be a pain in the ass, can I get you to change var1 and var2 for your own benefit? I know that var1 is the IP address and var2 the port, but to find that out I need to:

think what you would typically use first;

or look further on in the code.

Good programming practice states that you should ALWAYS give your variables names that describe what they do. I will attempt an explanation to help you understand this better:

If you had 10 cars, car1, car2...car10, and put that in a program that would then ask which one to display the parts required to build one for, it would be a lot easier when programming, and when looking back on the code 10 years later if instead you gave them better names, such as falcon, commodore, corolla, etc etc instead of car1, car2 etc etc.

It also helps other people understand your code and what you are doing with the data.

I am very sure you understand now how important it is to give variables good names?

However as for the rest of it, very well done and I hope to meet you again on the forums some time.

Anyway thats enough bitching from me.

Good Luck with the rest of this project, and if it is now finished, with any other projects you may have.

shanet.

Edited by shanet

[font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS:
%programfiles%/AutoIt3/autoit3.chm

Share this post


Link to post
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
Sign in to follow this  
Followers 0