Calling an Expect script from a Webpage using PHP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling an Expect script from a Webpage using PHP
# 1  
Old 06-19-2008
Calling an Expect script from a Webpage using PHP

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 Expect 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 comp.unix.shell: Re: Calling Expect from PHP is similar but I am not getting the "setsocket(SO_DEBUG):Permission denied" error and that solution did not work for me.

I have also checked the environment varibles for the webpage against command line. Command line 'env' included variables $LD_LIBRARY_PATH, $PERL5LIB, and $XAMPP_ROOT where the webpage did not. For grins and giggles, I exported these values before calling temp.exp on the webpage; it made no difference.


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
xpect "1.3.6.1.4.1.3052.5.1.1.1.1.6.3.3.1"

log_file
send -- "BYE\r"
exit 0
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Password check in bash script calling on expect

password check in bash script calling on expect Background: I have to copy a file from one server, to over 100 servers in a test environment. once the file is copied, it requires to have the permissions on the file changed/verified. These are all linux servers. most of them have the same... (1 Reply)
Discussion started by: 2legit2quit
1 Replies

2. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

3. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

4. Shell Programming and Scripting

Calling a function which uses expect from a shells script

Hi all, This is the first time i am using expect. I am trying to call a function with in the shell script. The function will shh to a new server and will pass the password using expect and send. I need help in calling the fuction i am getting follaowing errors... here the script ... (8 Replies)
Discussion started by: firestar
8 Replies

5. Shell Programming and Scripting

Calling Expect Script - Telnet

Hi All, I have an Expect script which logs into Cisco switch, performs a show interface command. I want to read a file of ip addresses which will be passed to the expect script. The script to read the file works, the expect script works on it's own but when i call the 'expect' script from the... (12 Replies)
Discussion started by: trinak96
12 Replies

6. Shell Programming and Scripting

Calling Shell Script from Expect...

Hi there, I need some help regarding the execution of shell script from expect as the method I am trying is giving me error. I wrote an shell program which takes two arguments to telnet to a device and saves the output in a file. Following is the script.... (0 Replies)
Discussion started by: cyberparanoid
0 Replies

7. Shell Programming and Scripting

ssh is not working while calling through expect shell script

Hi, Please share you experience and way out on below error:--> #!/bin/bash -xv FILE=login.txt + FILE=login.txt CONNECT=sshlogin.exp + CONNECT=sshlogin.exp SERVERNAME=$1 + SERVERNAME=192.168.12.1 MyServer="" + MyServer= MyUser="" + MyUser= MyPassword="" + MyPassword= exec 3<&0 +... (6 Replies)
Discussion started by: manish_1678
6 Replies

8. Shell Programming and Scripting

cant get a counter to work in bash scipt, this is calling expect script

I have looked high and low, tryed lots of diffrent things but cant get a simple counter to work right. what i need is to increase a count ever time it finishes the test, pass or fail. example TEST PASS 1, NEXT TEST PASS 2, I curently have set foo o while {$foo <=5} { incr foo puts... (1 Reply)
Discussion started by: melvin
1 Replies

9. Shell Programming and Scripting

calling expect script in ksh is failing via cron

I'm calling an expect script via a ksh script in cron and it is failing. The script runs fine if i run it manually. Does anyone know if it is an issue with compatibilty and if there is a way around it? (2 Replies)
Discussion started by: bhatia
2 Replies

10. Shell Programming and Scripting

Calling Expect script in Perl...

I call a EXPECT script from my perl script with machine IP and a FIle. The script logins to the machine and exports the value. The values to be exported or stored in a file. I have close to 10 machines and I have created 10 files and pass the corresponding files in command line, Now I could like... (4 Replies)
Discussion started by: ramkriz
4 Replies
Login or Register to Ask a Question