【问题标题】:How to check programmatically whether apache tomcat is running or not in python?如何以编程方式检查 apache tomcat 是否在 python 中运行?
【发布时间】:2017-09-25 09:47:16
【问题描述】:

有没有什么方法可以在 python 中从另一台机器检查 apache tomcat 服务器的状态?

例如,RESTful API 正在一台机器上运行,该机器的 apache tomcat 服务器位于 8086 端口。

现在,我想要的是在 python 中以编程方式从我的电脑检查指定机器的 apache tomcat 服务的状态。

是的,我可以通过在 python 中使用os.system ping 来检查机器是否正在运行。

import os
hostname = "xxx.xxx.xxx.xxx" #example
response = os.system("ping -c 1 " + hostname)

#and then check the response...
if response == 0:
    print hostname, 'is up!'
else:
    print hostname, 'is down!'

但是,我感兴趣的是,是否可以在 python 中以编程方式检查服务的状态 (real_ip:8086/BotAPI) 而不是机器的活跃度?

更新:使用 paramiko 我得到了以下回溯

channel.exec_command('ps -ef| grep tomcat')
Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
channel.exec_command('ps -ef| grep tomcat')
File "E:\python\lib\site-packages\paramiko\channel.py", line 63, in _check
return func(self, *args, **kwds)
File "E:\python\lib\site-packages\paramiko\channel.py", line 241, in exec_command
self._wait_for_event()
File "E:\python\lib\site-packages\paramiko\channel.py", line 1197, in _wait_for_event
raise e
paramiko.ssh_exception.SSHException: Channel closed.

【问题讨论】:

  • 你能ssh进入tomcat机器吗?
  • 是的。我可以远程登录到tomcat机器。

标签: python tomcat


【解决方案1】:

你可以使用fabric或paramiko远程连接到你的tomcat机器,然后检查进程是否正在运行

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('hostname', username='user', password='password')

transport = ssh.get_transport()
channel = transport.open_session()
channel.exec_command('ps -ef | grep tomcat')

status = channel.recv_exit_status()

【讨论】:

  • 它可以在 Windows 中工作吗?我的 tomcat 机器在 windows 10 中。
  • w10?你有那个 w10 外壳吗?像这样stackoverflow.com/questions/40057339/…
  • 不,它直接在netbeans中运行。
  • 好吧,你应该检查你的防火墙和用户权限
  • ssh.coneect 有效。但现在 channel.exec_command 不工作...... @lapinkoira
猜你喜欢
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 2011-01-31
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
相关资源
最近更新 更多