简体   繁体   English

从Apache httpd运行pexpect

[英]Running pexpect from apache httpd

I am generating HTML page from Python. 我正在从Python生成HTML页面。 There is also logic for spawning a SSH session using pexpect and fetching command output inside same Python code. 还有一种逻辑,可以使用pexpect生成SSH会话并在同一Python代码中获取命令输出。 But when I run Python from Apache httpd server, it is giving me 500 internal server error . 但是,当我从Apache httpd服务器运行Python时,它给了我500 internal server error But executing Python code separately is working fine. 但是单独执行Python代码效果很好。

Not sure if issue is in Python or Apache? 不确定是Python还是Apache?

Code is below and I have added the exception for debugging purpose. 代码在下面,我为调试目的添加了异常。 Exception shows 异常显示

Exception seen in Web page :
Error! pty.fork() failed: out of pty devices name 
'child' is not defined name 
'child' is not defined name 
'child' is not defined name 
'child' is not defined name 
'child' is not defined name 
'child' is not defined name 
'child' is not defined name

Code is below #############################################################

import pexpect
import sys
import time
import cgi, cgitb
import getpass
print "Content-Type: text/html\n\n"

try:
        child = pexpect.spawn('ssh -t admin@192.***.***.*** login root')
except Exception, e:
        print e
try:
        child.expect('(?i)password')
except Exception, e:
        print e
try:
        child.sendline('password')
except Exception, e:
        print e
try:
        child.expect('(?i)Password:')
except Exception, e:
        print e
try:
        child.sendline('password')
except Exception, e:
        print e
try:
        child.expect('-bash# ')
except Exception, e:
        print e
try:
        child.sendline('ls -al')
except Exception, e:
        print e
try:
        child.expect('-bash# ')
except Exception, e:
        print e
output = child.before
print "Content-Type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>Hello </title>"
print "</head>"
print "<body>"
print "<h1>",output,"</h1>"
print "</body>"
print "</html>"

The child variable is defined in the scope of the first try block. 子变量在第一个try块的范围内定义。 When it goes out of the scope of the first try block it becomes unknown to the interpreter. 当它超出第一个try块的范围时,解释器将不知道它。 You could fix this by merging all your try blocks into one. 您可以通过将所有try块合并为一个来解决此问题。 Which is enough. 够了。

Try with this snippet: 尝试使用以下代码段:

#!/usr/bin/env python

import pexpect
import sys
import time
import cgi, cgitb
import getpass


output = ""
try:
        child = pexpect.spawn('ssh -t admin@192.***.***.*** login root')
        child.expect('(?i)password')
        child.sendline('password')
        child.expect('(?i)Password:')
        child.sendline('password')
        child.expect('-bash# ')
        child.sendline('ls -al')
        child.expect('-bash# ')
        output = child.before
except Exception, e:
    print e

print "Content-Type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>Hello </title>"
print "</head>"
print "<body>"
print "<h1>",output,"</h1>"
print "</body>"
print "</html>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM