Using Expect to run C-Gate commands

Discussion in 'C-Gate Developers' started by ramon, Oct 18, 2005.

  1. ramon

    ramon

    Joined:
    Aug 3, 2004
    Messages:
    19
    Likes Received:
    0
    A simple way to create a poor man's Homegate is to use Expect/Tcl scripts... for Windows users, download the binaries from http://bmrc.berkeley.edu/people/chaffee/expectnt.html

    ... then you can automate commands to C-Gate by writing the appropriate scripts... for example the following switches off my desk lights by connecting to my c-gate service running on my linux server (bart)...

    # define c-gate server parameters
    set hostname "bart"
    set port "20023"
    # define cgate v2.3.25 (build 2057) telnet response
    set cgate_prompt "build 2057"
    spawn telnet $hostname $port
    expect -re "$cgate_prompt" {
    # send c-gate command - turn off lights
    exp_send "off 254/56/63\r"
    }
    expect "200 OK:"
    exp_send "exit\r"
    exp_send_user "Light Switched OFF"

    ... if you save the above as a file (say light_off.tcl), then pass into Expect on the command line you can automate the execution eg. create a Windows desktop icon to the runtime (tclsh80.exe) and pass in the path to the expect script eg. on my system this is -

    "C:\Program Files\Expect-5.21\bin\tclsh80.exe" "C:\Program Files\Expect-5.21\light_off.tcl"

    Choose your favorite icon, and away you go.

    ... another example, to set lights to 50% over 2 sec, with a bit of error handling...

    set hostname "bart"
    set port "20023"
    set cgate_prompt "build 2057"
    spawn telnet $hostname $port
    expect -re "$cgate_prompt" {
    exp_send "ramp 254/56/63 50% 2s\r"
    exp_send_user "sent c-gate command\n"
    } eof {
    exp_send_user "could not connect to $hostname\n"
    }
    expect "200 OK:"
    exp_send "exit\r"
    exp_send_user "Light Switched On 50%"
     
    Last edited by a moderator: Oct 18, 2005
    ramon, Oct 18, 2005
    #1
  2. ramon

    DaveQB

    Joined:
    Jul 1, 2010
    Messages:
    25
    Likes Received:
    0
    Location:
    DaveQB
    Thanks ramon
    Exactly the idea I need to inspire me.

    I might try this in python and add on a web UI to it and away we go....
     
    DaveQB, Dec 9, 2010
    #2
  3. ramon

    DaveQB

    Joined:
    Jul 1, 2010
    Messages:
    25
    Likes Received:
    0
    Location:
    DaveQB
    Just in case anyone was wondering the same, I have a basic python script working using the telnetlib module:

    Code:
    #!/usr/bin/python
    
    import telnetlib
    host = "cgatehost"
    port = "20023"
    telnet  = telnetlib.Telnet(host,port)
    stuff = telnet.read_until("\n")
    print stuff
    telnet.write("project list\n")
    stuff = telnet.read_until("\n")
    print stuff
    telnet.close()
    

    Something to get people started.
     
    DaveQB, Dec 9, 2010
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.