不愿意做出改变 ≈ 坐吃等死!!

python远程执行linux命令

上一篇 / 下一篇  2016-05-30 17:29:03 / 个人分类:python

# coding:utf-8
importparamiko
defstart_up(host,user,pwd,path):
"""启动服务"""
# print host, user, pwd, path

# paramiko.util.log_to_file('paramiko_startup.log')
ssh = paramiko.SSHClient()
# ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host,22,user,pwd,timeout=10)
#找出当前linux服务器的java目录
find_java ="echo $JAVA_HOME"
stdin,stdout,stderr = ssh.exec_command(find_java)
java_home = stdout.readlines()[0]
command ="sudo sh {1}bin/startup.sh".format(java_home,path)
printcommand
(stdin,stdout,stderr) = ssh.exec_command(command=command)
stdin.write('Y')
ut = stdout.readlines()
foriinout:
printi
ssh.close()
定义一个方式远程启动tomcat,但是报错
报错如下
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

怎么办 ? 
问里群里大神,需要在执行shell的时候加入JAVA_HOME路径。
于是获取JAVA_HOME路径,于是就有了上面的代码,但是还是不行,因为这样还是获取不到java路径


没办法,问题困扰很久,改用其他模块

# coding:utf-8
from fabric.api import run, env, hosts

def start_up_fabric(host, user, pwd, path):
    """启动服务"""
    host_ = host + ':22'
    env.hosts = [host_]
    env.user = user
    env.password = pwd
    # utput = run('echo $JAVA_HOME')
    # print output
    command = 'sh {0}bin/startup.sh'.format(path)
    run(command)


最开始使用的是env.hosts = [host,]
依然有问题, 因为其他方法调用这个方法的时候 仍然需要手动输入host

继续查 继续修改 于是问题终于搞定
下面的代码 可以远程执行shell 启动tomcat不需要指定java/jre路径 而且不会再需要手动输入host

# coding:utf-8
from fabric.api import run, env, hosts

def start_up_fabric(host, user, pwd, path):
    """启动服务"""
    host_ = host + ':22'
    env.host_string = host_
    env.user = user
    env.password = pwd
    # utput = run('echo $JAVA_HOME')
    # print output
    command = 'sh {0}bin/startup.sh'.format(path)
    run(command,pty=False)



TAG: Linux linux 远程启动tomcat 远程执行shell Python python

 

评分:0

我来说两句

Open Toolbar