我写了一个小脚本,试图从html页面获取远程主机的主机名。 该脚本可在cli上成功运行,但在浏览器上运行时似乎无法连接到远程主机并获取信息。 有人可以告诉我我想念什么吗?
#!/usr/bin/env python
import paramiko
import cgi
def ssh_connect(host,cmd):
try:
ssh_client =paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host, timeout=10)
stdin,stdout,stderr=ssh_client.exec_command(cmd)
output=(stdout.readlines())
return output
ssh_client.close()
except KeyboardInterrupt:
output="KeyboardInterrupt"
sys.exit(0)
except paramiko.AuthenticationException:
return "authenication failed"
remote_hostname=ssh_connect('10.1.2.2','hostname' )
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Test</title>'
print '</head>'
print '<body>'
print '<h2>the hostname on remote host is %s</h2>' %(host,remote_hostname)
print '</body>'
print '</html>'