Results 1 to 3 of 3

Thread: screen and expect and python

  1. #1
    Join Date
    Jun 2011
    Beans
    10

    screen and expect and python

    Hi All,

    I am trying to write a bash script which launches a python application.

    Unfortunately the python application needs to be interactive.

    Also, I am running this python application in a screen.

    I am trying to send the required interactions using expect but I am failing hard.

    Here is my bash script:

    Code:
    sent=blah
    expexec=$(/usr/bin/expect -c "/usr/bin/python /opt/app.py expect \"Interaction:\" send \"$sent\r\" interact")
    
    /usr/bin/screen -L -dmS spawner /bin/sleep 30
    /bin/sleep 1
    /usr/bin/screen -S spawner -X screen screen -L -dR myDaemon
    /bin/sleep 1
    /usr/bin/screen -S myDaemon -X detach
    /usr/bin/screen -S myDaemon -X stuff "echo $expexec"
    /usr/bin/screen -S spawner -X stuff "exit\r"
    I would really appreciate it if someone could tell me what I am missing / doing wrong.

    Thanks,

    JG

  2. #2
    Join Date
    Mar 2010
    Location
    USA
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: screen and expect and python

    Let me see if I read this right. May be just style differences, but...
    Code:
    #!/bin/sh 
    # Debug expect & screen script.
    
    # You have to add quotes around a string to init your variable.
    sent="blah"
    # Debugged it by adding the next ine and seeing what it was initialized to...
    echo $sent
    
    # This was trying to execute the command instead of loading a variable with your
    # variable = string = commandline... showed executing with errors, but blank as
    # a variable... 
    # expexec=$(/usr/bin/expect -c "/usr/bin/python /opt/app.py expect \"Interaction:\" send \"$sent\r\" interact")
    
    # This is what I came up with:
    expexec="/usr/bin/expect -c \"/usr/bin/python /opt/app.py expect \"Interaction:\" send \"$sent\r\" interact\""
    # This was the debug line to see what it loaded as:
    echo $expexec
    
    
    /usr/bin/screen -L -dmS spawner /bin/sleep 30
    /bin/sleep 1
    /usr/bin/screen -S spawner -X screen screen -L -dR myDaemon
    /bin/sleep 1
    /usr/bin/screen -S myDaemon -X detach
    
    ## Can the Op explain this one? Is he tring to "echo" it or execute it? I'm thinking he needs to take the echo out of that next line...
    /usr/bin/screen -S myDaemon -X stuff "echo $expexec"
    
    /usr/bin/screen -S spawner -X stuff "exit\r"
    When I'm trying to work out (debug) a bash or python script... I'll put in debug code (echo or print statements) to see if my code is reacting like I'm expecting it too and to show me what is going on underneath. I can always comment it out or remove it when I get it going. What it showed me is that your variable were not loading... ad your expect commandline string varaible was trying to execute on the string init.

    Try the changes and see if that works. ...At least that part of it is working now. Didn't really look at the screen lines... I know some say bash and expect play weird together sometimes, so some, if using bash, call to an expect script... then return.

    Look in my comments. I have questions

    The application is a python app? Why not a python script (sys:exec) talking with each other? Just a thought.
    Last edited by MAFoElffen; May 1st, 2013 at 02:45 AM.
    Multi-Boot= Various flavours of Windows, Linux and Unix... Ubuntu user # 33563, Linux user # 533637
    Sticky: [all variants] Graphics Resolution- Upgrade /Blank Screen after reboot
    Boot Info Script courtesy of community member meierfra

  3. #3
    Join Date
    Jun 2011
    Beans
    10

    Re: screen and expect and python

    Quote Originally Posted by MAFoElffen View Post
    Let me see if I read this right. May be just style differences, but...
    Code:
    #!/bin/sh 
    # Debug expect & screen script.
    
    # You have to add quotes around a string to init your variable.
    sent="blah"
    # Debugged it by adding the next ine and seeing what it was initialized to...
    echo $sent
    
    # This was trying to execute the command instead of loading a variable with your
    # variable = string = commandline... showed executing with errors, but blank as
    # a variable... 
    # expexec=$(/usr/bin/expect -c "/usr/bin/python /opt/app.py expect \"Interaction:\" send \"$sent\r\" interact")
    
    # This is what I came up with:
    expexec="/usr/bin/expect -c \"/usr/bin/python /opt/app.py expect \"Interaction:\" send \"$sent\r\" interact\""
    # This was the debug line to see what it loaded as:
    echo $expexec
    
    
    /usr/bin/screen -L -dmS spawner /bin/sleep 30
    /bin/sleep 1
    /usr/bin/screen -S spawner -X screen screen -L -dR myDaemon
    /bin/sleep 1
    /usr/bin/screen -S myDaemon -X detach
    
    ## Can the Op explain this one? Is he tring to "echo" it or execute it? I'm thinking he needs to take the echo out of that next line...
    /usr/bin/screen -S myDaemon -X stuff "echo $expexec"
    
    /usr/bin/screen -S spawner -X stuff "exit\r"
    When I'm trying to work out (debug) a bash or python script... I'll put in debug code (echo or print statements) to see if my code is reacting like I'm expecting it too and to show me what is going on underneath. I can always comment it out or remove it when I get it going. What it showed me is that your variable were not loading... ad your expect commandline string varaible was trying to execute on the string init.

    Try the changes and see if that works. ...At least that part of it is working now. Didn't really look at the screen lines... I know some say bash and expect play weird together sometimes, so some, if using bash, call to an expect script... then return.

    Look in my comments. I have questions

    The application is a python app? Why not a python script (sys:exec) talking with each other? Just a thought.
    Thanks for the help mate. I admittedly did try your modifications, but they still weren't getting me there.

    I ended up finding some other expect examples (http://www.thegeekstuff.com/2010/10/expect-examples/) which inspired me to split this script out into 2 scripts.

    expect script:
    Code:
    #!/usr/bin/expect
    set timeout 60
    
    set sent [lindex $argv 0]
    
    spawn /usr/bin/python /opt/app.py
    
    expect "Interaction:"
    
    send "$sent\r";
    
    interact
    main script:
    Code:
    #!/bin/sh
    
    # variables
    sent=blah
    expectScript=/path/to/expect/script
    
    # bash screen detachment foo
    /usr/bin/screen -L -dmS spawner /bin/sleep 30
    /bin/sleep 1
    /usr/bin/screen -S spawner -X screen screen -L -dR myDaemon
    /bin/sleep 1
    /usr/bin/screen -S myDaemon -X detach
    
    # sending the command to the screen
    /usr/bin/screen -S myDaemon -X stuff "$expectScript $sent\r"
    
    # close the screen spawner
    /usr/bin/screen -S spawner -X stuff "exit\r"

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •