java使用ssh远程操作linux 提交spark jar

maven依赖
<!--Java ssh-2 --><dependency>    <groupId>ch.ethz.ganymed</groupId>    <artifactId>ganymed-ssh2</artifactId>    <version>262</version></dependency>
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

public class RemoteShellTool {
    private Connection conn;
    private String ipAddr;
    private String charset = Charset.defaultCharset().toString();
    private String userName;
    private String password;

    public RemoteShellTool( String ipAddr, String userName, String password, String charset) {
        this.ipAddr = ipAddr;
        this.userName = userName;
        this.password = password;
        if(charset !=null){
            this.charset = charset;
        }
    }
    public boolean login() throws IOException {
        conn = new Connection(ipAddr);
        //连接
        conn.connect();
        //认证
        return  conn.authenticateWithPassword(userName,password);
    }

    public String exec(String cmds) {
        InputStream in = null;
        String result = "";
        try {
            if(this.login()){
                //打开一个会话
                Session session = conn.openSession();
                session.execCommand(cmds);
                in = session.getStdout();
                result = this.processStdout(in,this.charset);
                conn.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public String processStdout(InputStream in, String charset) {
        byte[]  buffer = new byte[1024];
        StringBuffer sb = new StringBuffer();
        try {
            while (in.read(buffer)!=-1){
                sb.append(new String(buffer,charset));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  sb.toString();
    }

    public static void main(String[] args) {//将jar包命名为task任务,根据传入taskid 找到对应的jar包 调用接口提交
//      args[0]="{1}";
        RemoteShellTool rst = new RemoteShellTool("bigdata.server1","hejunhong","123456","utf-8");
        System.out.println(rst.exec("/opt/cdh5/hadoop-2.5.0-cdh5.3.6/sbin/hadoop-daemon.sh start datanode"));
        System.out.println(rst.exec("ls /opt/cdh5/"));
//String submit="/opt/cdh5/spark-2.2.1-bin-2.6.0-cdh5.14.2/bin/spark-submit --master yarn --deloy-modle cluster" +
//        "--class spark.jar  hdfs://  "+args[0]+ " --conf  spark.ssss:20";
    }
}

原文地址:https://www.cnblogs.com/hejunhong/p/10343513.html

时间: 2024-08-28 07:06:34

java使用ssh远程操作linux 提交spark jar的相关文章

java操作linux 提交spark jar

maven依赖 <!--Java ssh-2 --><dependency> <groupId>ch.ethz.ganymed</groupId> <artifactId>ganymed-ssh2</artifactId> <version>262</version></dependency> import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.S

远程操作linux的利器:putty和psftp

通过SSH远程操作Linux的好工具:putty(远程操作linux)  psftp(远程上传下载) 占用空间小,操作方便,不需安装配置即可使用,官方网站(http://www.chiark.greenend.org.uk/~sgtatham/putty/ )可提供相应软件的下载,免费的哦 到 Unix-Center.net/Unix体验中心注册个帐后就可 利用putty登陆 学学unix了 : psftp工具相关命令使用(转载于http://live.haliluya.org/weblog/2

java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息

1.java使用Jsch实现远程操作linux服务器进行文件上传.下载,删除和显示目录信息. 参考链接:https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html https://www.cnblogs.com/longyg/archive/2012/06/25/2561332.html https://www.cnblogs.com/qdwyg2013/p/5650764.html#top 引入jar包的maven依赖如下所示:

java应用程序远程登录linux并执行其命令(ssh jar包)

http://www.ganymed.ethz.ch/ssh2/在这个网址下载一个调用ssh和scp命令的jar包. 然后,就可以写程序了.将上面的jar包导入MyEclipse,下面是一个类的实例代码. package hh; import java.io.BufferedReader;     import java.io.IOException;     import java.io.InputStream;    import java.io.InputStreamReader; imp

ssh远程登录linux live系统

要想ssh远程登录,需要准备两件事:配置同网段IP和开启SSH服务. 由于live系统没有IP,所以首先需要配置IP. 我的live系统是在虚拟机上启动的,宿主IP为192.168.230.1,live系统的IP我设置为192.168.230.180: #ifconfig eth0 192.168.230.180 然后在SecurityCRT上连接live系统,输入密码时发现live系统也没有用户密码.这时候若不输密码或者随便输入一个密码,SecurityCRT也是不让连接的.所以需要在live

虚拟机VMware网络类型&amp;&amp;SSH远程连接Linux

前言: Linux专题是16年11月开始写,说来惭愧,已经5个月没学Linux,至今感觉连入门还没达到.暑假实习有投运维开发岗位,无奈对Linux不熟悉,校招简历也被刷了.so, 我打算先花1个月内的时间入下门.前几天还专门去当当网买了一本<跟老男孩学Linux运维-Web集群实战>.lenrning begin!! 一.虚拟机网络类型 为虚拟机选择网络类型是极其关键的.VMware虚拟机常见的网络类型有Bridged(桥接).NAT.Host-only(仅主机)3种.在创建Linux虚拟机时

用ssh远程登录Linux主机

ssh [email protected]地址 输入登录密码即可进入远程主机 用ssh远程登录Linux主机

java使用jsch远程链接linux执行命令

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader; import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelExec;import com.jcraft.jsch.JSch;import com.jcraft.jsch.JSchException;i

在windows上通过ssh远程链接linux服务器[转]

本文分别转自 [http://jingyan.baidu.com/article/6d704a130de40e28db51cab5.html] [http://www.cnblogs.com/mliudong/p/4094519.html] 今天想要新装了一台linux服务器,想要将其搬到机房,同时也要实现通过别的电脑对该服务器的访问,通过windows上装的ssh访问linux服务器,发现链接不上,通过百度发现linux服务器上要装ssh-server,所以百度了安装教程(http://jin