simple php/expect script works from command line but not from web


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple php/expect script works from command line but not from web
# 1  
Old 06-01-2011
simple php/expect script works from command line but not from web

I have a really basic expect script which I call from php. I works fine when I run the php from the shell, but from the web it appears as if the output buffer gets chopped and never gets all of the contents.

php script: (runexpect.php)

PHP Code:
<?php
shell_exec
("/opt/bell/apache2/htdocs/www/expecttest/connect.exp > /tmp/connect.exp.out 2>&1 ");
?>
expect script (connect.exp)
Code:
#!/usr/local/bin/expect -f

set timeout 20
spawn telnet COVE-C2851-CE1
expect -re ".*Username:.*" 
send -- "jacksona\r"

expect -re ".*Password:.*"
send -- "xxxxx\r"
expect -re ".*COVE-C2851-CE1#.*"
send -- "exit\r"
expect eof


Output from running it from the web

Quote:
spawn telnet COVE-C2851-CE1
Trying 167.70.219.133...
Connected to COVE-C2851-CE1.
Escape character is '^]'.

I seems to connect to the device fine, but the buffer only contains up to "Escape character is '^]'." and never contains the "Username:" prompt so the script eventually times out. but running php ./runexpect.php from the command line works like a charm.

I have tried everything, but no luck .. please help me.
# 2  
Old 06-01-2011
Telnet seems to connect but not get a prompt. Probably missing some ENV or the tty in the web case. Try comparing the kernel calls using truss/tusc/strace to see where it diverges. Make a truss/tusc/strace wrapper script for telnet.
# 3  
Old 06-01-2011
It is weird because it is actually connect to the device. I can see from the debug output of expect that is get as far as connecting, but never sees the "Username:" prompt in the buffer

here is the debug ...

expect: does "Trying 167.70.219.133...\r\nConnected to COVE-C2851-CE1.\r\nEscape character is '^]'.\r\n" (spawn_id exp3) match regular expression ".*Username:.*"? no



When I snoop what is happening on the router side, it is actually sending back the Username prompt, but expect never sees it in the buffer. The other weird thing is that is works fine from both the command line and web when I ssh to the device.
# 4  
Old 06-01-2011
Why not stick to ssh?
# 5  
Old 06-01-2011
The terminal may be acting in canonical mode, reading line by line. In which case the login prompt, if not followed by a newline, won't be read.

Try forcing a certain terminal mode before you spawn ssh like stty raw -echo

If at all possible of course, ssh would be better. telnet is mostly obsolete these days and ssh doesn't need expect-hacking to automate.
# 6  
Old 06-01-2011
I only turned on ssh for this box to test, but I am unfortunately many of our production devices are telnet. It appears that the expect buffer is not getting the "Username:" prompt eventhough the router is clearly sending it. I tried
"stty raw -echo but no luck.


# 7  
Old 06-01-2011
Try not using -re where there is no linefeed. Use simple expect.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string works on command-line but fails when run from shell script

I wish to replace "\\n" with a single white space. The below does the job on command-line: $ echo '/fin/app/scripts\\n/fin/app/01/sql' | sed -e 's#\\\\n# #g'; /fin/app/scripts /fin/app/01/sql However, when i have the same code to a shell script it is not able to get me the same output:... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. UNIX for Beginners Questions & Answers

Expect scripting issue, works interactively when doing commands in cli, does not work in script

Hi; problem may be obvious, simple but I have to say it is somehow not easy to locate the issue. I am doing some word extracting from multiline text. Interacting in CLI seems to work without issues. First step is to add multiline text to a variable. expect1.1> expect1.1> set... (2 Replies)
Discussion started by: aldowski
2 Replies

3. Shell Programming and Scripting

Works on command line but not in script

OSX 10.9 I am building a script that evaluates the difference between 2 files. Here is a command that does not work transparently. Running this command in Terminal yields great results; however when I put that line in a .sh script, I get the errors shown below. Am I doing something silly? ... (1 Reply)
Discussion started by: sudo
1 Replies

4. Shell Programming and Scripting

Expect script works on CLI, but not under Nagios

I'm having a problem scripting Expect to work as a Nagios check. The script runs fine from the cli, but not when run as a Nagios check. I turn on the debug output, and it looks like it's not seeing the whole buffer's worth of info, like it's paged interactively somehow. I'm new to Expect, but this... (1 Reply)
Discussion started by: dhargens
1 Replies

5. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

6. UNIX for Dummies Questions & Answers

Works on command line but not in script; vncserver on solaris 10

Hi guys. My first post, so be gentle... On my Solaris 10 machine vnc server is running. I need a command to extract most recent client session number (screen). So with: Code: bash-3.2# ps -ef | grep vnc | grep Xaut root 19805 19797 0 15:41:44 ? 0:01 Xvnc :4 -inetd -once... (5 Replies)
Discussion started by: cp6uja
5 Replies

7. UNIX for Dummies Questions & Answers

Works on command line but not in script

Hey guys. Hopefully this is an easy one but having reference similar problems on the web I still can't fix it. I am doing a recursive find and replace from a script. Of course I could just run the damn thing from the command line but it's bugging me now and want to get it working. grep -rl... (4 Replies)
Discussion started by: anthonyjstewart
4 Replies

8. Shell Programming and Scripting

This code works in the command line but not in a shl script

When I run this code from the command line works spinel.middlebury.edu:/u02/sct/banner/bandev2/middlebury/shl:DEV2$ ls ef* eftseq.dat spinel.middlebury.edu:/u02/sct/banner/bandev2/middlebury/shl:DEV2$ file_seq=$( < eftseq.dat) ... (1 Reply)
Discussion started by: rechever
1 Replies

9. Shell Programming and Scripting

Zgrep works at command line but not in script?

Hi all- I'm trying to search through some .gz log files to verify certain feeds have passed through our app. I have a small script that I wrote in hopes that I could automate the checking but haven't been able to get the zgrep to work. When I copy it to the command line directly it works... (2 Replies)
Discussion started by: Cailet
2 Replies

10. UNIX for Dummies Questions & Answers

script works on command line, not in cron job

Hey there, I'm a total newbie unix guy here and just picking this stuff up. Have a very small script I put together that works fine from the command line but not once I put it in a cron job. Searched and found this thread and am wondering it it has something to do with setting variables, though the... (7 Replies)
Discussion started by: JackTheTripper
7 Replies
Login or Register to Ask a Question