Discussion:
[rancid] PHP creating a telnet session via Expect
c***@aol.com
2008-06-19 18:44:14 UTC
Permalink
I have a webpage that is in HTML and PHP.  In PHP I have tried using exec, system, shell_exec and passthru functions to call an Expect Script file (temp.exp).  This expects file spawns a telnet session that uses "expect/send" commands to retrieve information from an environmental unit (not a normal server).  This data is placed in a file and then loaded to the webpage.  

The problem is if I call "./temp.exp" it runs great, but when I call the same program from the webpage exec("./temp.exp") using PHP (because it retrieves the data is displayed in the webpage), it will connect but then stops responding.  It never returns the password prompt to achieve a yes to move on.  

I inserted “exp_internal 1” in the expect file to produce the diagnostic results.  

When temp.exp is ran manually
spawn telnet 111.111.11.111
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {18790}
expect: does "" (spawn_id exp5) match glob pattern "Password:"? no
Trying 111.111.11.111...
Connected to 111.111.11.111.
Escape character is '^]'.
Password:
expect: does "Trying 111.111.11.111...\r\nConnected to 111.111.11.111.\r\nEscape character is '^]'.\r\nPassword: " (spawn_id exp5) match glob pattern "Password:"? yes
expect: set expect_out(0,string) "Password:"
expect: set expect_out(spawn_id) "exp5"


When temp.exp is called from the webpage
spawn telnet 111.111.11.111
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {19909}
expect: does "" (spawn_id exp19) match glob pattern "Password:"? no
Trying 111.111.11.111...
Connected to 111.111.11.111.
Escape character is '^]'.
expect: does "Trying 111.111.11.111...\r\nConnected to 111.111.11.111.\r\nEscape character is '^]'.\r\n" (spawn_id exp19) match glob pattern "Password:"? no
expect: timed out

I have checked/changed the Apache user, the permissions and ownership of the files are fine.   I have put debug on the expect and the telnet sessions.  I have specified the telnet used and the port.  I have upgraded my Tcl/Tk.   The link http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2005-06/0454.html is similar but I am not getting the "setsocket(SO_DEBUG):Permission denied" error and that solution did not work for me.   

 

System info

# find / -name telnet

/usr/bin/telnet

/usr/ucb/telnet

# expect -v

expect version 5.40.0

 

Below are versions that I have tried.

 

The script 1

#!/usr/local/bin/expect

exp_internal -f /opt/xampp/htdocs/temp/diag.txt 1

spawn -nottycopy /bin/telnet -d 111.11.11.111 23

sleep 3

expect "Password:"

send "OMNI\r"

expect "X. Exit (end connection)"

send "p\r"

expect ">"

log_file -noappend -a "/opt/xampp/htdocs/temp/data.txt"

 

#Sensor Name     POS3      Temp Sensor     1

send "GET 3052.5.1.1.1.1.4.3.1.1\r"

sleep 3

expect "1.3.6.1.4.1.3052.5.1.1.1.1.4.3.1.1"

 

#Read Out        POS3      Temp Sensor     1

send "GET 3052.5.1.1.1.1.6.3.1.1\r"

sleep 3

expect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.1.1"

 

#Read Out        POS3      Humidity Sensor 2

send "GET 3052.5.1.1.1.1.6.3.3.1\r"

sleep 3

expect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.3.1"

 

log_file

send "BYE\r"

exit 0

 

 

 

 

The script 2

#!/usr/local/bin/expect -f

exp_internal -f /opt/xampp/htdocs/temp/diag.txt 1

set force_conservative 1 ;# set to 1 to force conservative mode even if

                          ;# script wasn't run conservatively originally

if {$force_conservative} {

        set send_slow {1 .1}

        proc send {ignore arg} {

                sleep .1

                exp_send -s -- $arg

        }

}

set timeout 3

spawn $env(SHELL)

match_max 100000

send -- "telnet 111.11.11.111\r"

expect "Password: "

send -- "OMNI\r"

expect "X. Exit (end connection)"

send -- "p\r"

expect ">"

log_file -noappend -a "/opt/xampp/htdocs/temp/data.txt"

 

#Sensor Name     POS3      Temp Sensor     1

send -- "GET 3052.5.1.1.1.1.4.3.1.1\r"

sleep 3

expect "1.3.6.1.4.1.3052.5.1.1.1.1.4.3.1.1"

 

#Read Out        POS3      Temp Sensor     1

send -- "GET 3052.5.1.1.1.1.6.3.1.1\r"

sleep 3

expect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.1.1"

 

#Read Out        POS3      Humidity Sensor 2

send -- "GET 3052.5.1.1.1.1.6.3.3.1\r"

sleep 3

expect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.3.1"

 

log_file

send -- "BYE\r"

exit 0
Lance Vermilion
2008-06-19 19:27:40 UTC
Permalink
I would look at what term you are using and how that differs from PHP to
expect. Also create a basic php script to execute the expect script and see
if it works through cli. if that works then check through apache. if it
fails through apache it might be because of a term issue.
I have a webpage that is in HTML and PHP. In PHP I have tried using exec,
system, shell_exec and passthru functions to call an Expect Script file
(temp.exp). This expects file spawns a telnet session that uses
"expect/send" commands to retrieve information from an environmental unit
(not a normal server). This data is placed in a file and then loaded to the
webpage.
The problem is if I call "./temp.exp" it runs great, but when I call the
same program from the webpage exec("./temp.exp") using PHP (because it
retrieves the data is displayed in the webpage), it will connect but then
stops responding. It never returns the password prompt to achieve a yes to
move on.
I inserted "exp_internal 1" in the expect file to produce the diagnostic
results.
*When temp.exp is ran manually*
spawn telnet 111.111.11.111
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {18790}
expect: does "" (spawn_id exp5) match glob pattern "Password:"? no
Trying 111.111.11.111...
Connected to 111.111.11.111.
Escape character is '^]'.
expect: does "Trying 111.111.11.111...\r\nConnected to
111.111.11.111.\r\nEscape character is '^]'.\r\n*Password: " (spawn_id
exp5) match glob pattern "Password:"? yes*
expect: set expect_out(0,string) "Password:"
expect: set expect_out(spawn_id) "exp5"
*When temp.exp is called from the webpage*
spawn telnet 111.111.11.111
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {19909}
expect: does "" (spawn_id exp19) match glob pattern "Password:"? no
Trying 111.111.11.111...
Connected to 111.111.11.111.
Escape character is '^]'.
expect: does "Trying 111.111.11.111...\r\nConnected to
111.111.11.111.\r\nEscape character is '^]'.\r\n*" (spawn_id exp19) match
glob pattern "Password:"? no*
expect: timed out
I have checked/changed the Apache user, the permissions and ownership of
the files are fine. I have put debug on the expect and the telnet
sessions. I have specified the telnet used and the port. I have upgraded
my Tcl/Tk. The link
http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2005-06/0454.html is
similar but I am not getting the "setsocket(SO_DEBUG):Permission denied"
error and that solution did not work for me.
*System info*
# find / -name telnet
/usr/bin/telnet
/usr/ucb/telnet
# expect -v
expect version 5.40.0
Below are versions that I have tried.
*The script 1*
#!/usr/local/bin/expect
exp_internal -f /opt/xampp/htdocs/temp/diag.txt 1
spawn -nottycopy /bin/telnet -d 111.11.11.111 23
sleep 3
expect "Password:"
send "OMNI\r"
expect "X. Exit (end connection)"
send "p\r"
expect ">"
log_file -noappend -a "/opt/xampp/htdocs/temp/data.txt"
#Sensor Name POS3 Temp Sensor 1
send "GET 3052.5.1.1.1.1.4.3.1.1\r"
sleep 3
expect "1.3.6.1.4.1.3052.5.1.1.1.1.4.3.1.1"
#Read Out POS3 Temp Sensor 1
send "GET 3052.5.1.1.1.1.6.3.1.1\r"
sleep 3
expect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.1.1"
#Read Out POS3 Humidity Sensor 2
send "GET 3052.5.1.1.1.1.6.3.3.1\r"
sleep 3
expect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.3.1"
log_file
send "BYE\r"
exit 0
*The script 2*
#!/usr/local/bin/expect -f
exp_internal -f /opt/xampp/htdocs/temp/diag.txt 1
set force_conservative 1 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
set timeout 3
spawn $env(SHELL)
match_max 100000
send -- "telnet 111.11.11.111\r"
expect "Password: "
send -- "OMNI\r"
expect "X. Exit (end connection)"
send -- "p\r"
expect ">"
log_file -noappend -a "/opt/xampp/htdocs/temp/data.txt"
#Sensor Name POS3 Temp Sensor 1
send -- "GET 3052.5.1.1.1.1.4.3.1.1\r"
sleep 3
expect "1.3.6.1.4.1.3052.5.1.1.1.1.4.3.1.1"
#Read Out POS3 Temp Sensor 1
send -- "GET 3052.5.1.1.1.1.6.3.1.1\r"
sleep 3
expect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.1.1"
#Read Out POS3 Humidity Sensor 2
send -- "GET 3052.5.1.1.1.1.6.3.3.1\r"
sleep 3
expect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.3.1"
log_file
send -- "BYE\r"
exit 0
------------------------------
Get the Moviefone Toolbar<http://toolbar.aol.com/moviefone/download.html?ncid=aolcmp00050000000011>.
Showtimes, theaters, movie news, & more!
_______________________________________________
Rancid-discuss mailing list
http://www.shrubbery.net/mailman/listinfo.cgi/rancid-discuss
Loading...