用paramiko远程开启tomcat失败

本文介绍了一种通过SSH使用Paramiko远程启动Tomcat的方法。针对环境变量未定义导致启动失败的问题,文中提供了修改后的代码示例,在命令中增加了加载环境变量的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用paramiko远程开启tomcat失败

在网上查找资料后,使用如下代码通过ssh远程连接到服务器,并使用exec_command()执行命令

import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,username='root',password='####')
stdin,stdout,stderr=ssh.exec_command('/tomcat/bin/startup.sh')
result=stdout.readlines()
print result

******result返回报错
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program******

根据报错可以发现,缺少环境变量,于是在command中添加一句source /etc/profile读入环境变量的数据
*

修改后的代码

*


import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host',username='root',password='passwd')
stdin,stdout,stderr=ssh.exec_command('source /etc/profile;/opt/project/Tomcat7.0.22-8060-pc/bin/startup.sh')
result=stdout.readlines()
print result
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值