用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