scala用ssh2连接Linux
这个需要安装库:
import ch.ethz.ssh2.{Connection, Session, StreamGobbler}
首先用 ip 和 post 创建连接:
val conn: Connection = new Connection(ipAddr, post)
判断这个连接是否连接上了,这个用了一个 Boolean 类型判断:
def login(): Boolean = {
conn.connect()
// 连接
val ret: Boolean = conn.authenticateWithPassword(userName, password)
ret
}
如果连接成功的话,那么就将命令发送过去,命令发送只需要建立一个会话即可,执行命令返回的值保存在 in 中:
val session = conn.openSession()
session.execCommand(cmds)
val in = session.getStdout
最后就是处理解析 in 中的返回结果就行了:
val is = new StreamGobbler(in)
val brs: BufferedReader = new BufferedReader(new InputStreamReader(is))
val line = brs.lines().toArray().toList.mkString(",")
完整的类封装成:
class RemoteShellTool(ipAddr: String, post: Int, userName: String, password: String) {
val conn: Connection = new Connection(ipAddr, post)
//判断是否连接上了
def login(): Boolean = {
conn.connect()
// 连接
val ret: Boolean = conn.authenticateWithPassword(userName, password)
ret
}
//发送命令过去
def exec(cmds: String): String = {
var result: String = ""
try {
val str_ret: Boolean = login()
if (str_ret) {
// 打开一个会话
val session = conn.openSession()
session.execCommand(cmds)
val in = session.getStdout
result = processStdout(in)
} else {
println("连接失败")
}
} catch {
case e: IOException => {
e.printStackTrace()
}
} finally {
conn.close()
}
result
}
//处理返回结果
def processStdout(in: InputStream): String = {
val is = new StreamGobbler(in)
val brs: BufferedReader = new BufferedReader(new InputStreamReader(is))
val line = brs.lines().toArray().toList.mkString(",")
line
}
在 main 函数中调用这个类即可:
def main(args: Array[String]): Unit = {
//读取配置文件
val filePath = System.getProperty("user.dir")
val properties: Properties = new Properties()
val ipstream = new BufferedInputStream(new FileInputStream(filePath + "/conf/configssh.properties"))
properties.load(ipstream)
val ip = "ip"
val post = "post".toInt
val userName = "userName"
val password = password"
val cmd = "cmd"
val rms = new RemoteShellTool(ip, post, userName, password)
val result = rms.exec(cmd)
println(result)
}
这个方法能发送的 cmd 有点少,比如能识别 date 、 ls 等,但是不能识别 history 、 ll
scala用ssh2连接Linux的更多相关文章
- 第2章 Linux系统安装(3)_SSH连接Linux工具:SecureCRT和WinSCP
4. SSH连接Linux工具 4.1 Linux网卡配置 (1)临时配置: ifconfig eth0 192.168.32.100 //给eth0网卡指定IP,写在ROM里的,关机会丢失. (2) ...
- putty连接linux as5 输入密码后连接中断
putty连接linux as5 输入密码后连接中断 1.修改putty首页的设置,选择“close session on exit” 为 “never”,之后发现输入密码后,“session clo ...
- windows上使用SecureCRT连接linux
前言: SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件.这样操作的时候不必进入到linux桌面,可以更方便的 ...
- java使用SSH连接Linux系统
SSH连接linux系统使我们在开发项目中常用到的,现在留下来,做个记录 package com.log; import java.io.BufferedReader; import java.io. ...
- SecureCRT连接linux步骤
SecureCRT连接linux步骤 做个笔记,以免隔段时间后忘了 LINUX系统一般都是用来作服务器使用,而且都是通过命令行来操作,为了操作方便我们都会使用第三方软件来远程操作.CRT就是比较常用 ...
- 用Putty连接Linux
随着linux应用的普及,linux管理越来越依赖远程管理.在各种telnet类工具中,putty是其中最出色的一个. 一.Putty简介 Putty是一个免费小巧的Win32平台下的teln ...
- Python (paramiko) 连接Linux服务器
目录 参考资料 Paramiko 安装 连接Linux 文件上传/下载 文件封装 其他 参考资料 https://www.liujiangblog.com/blog/15/ https://blog. ...
- Windows远程连接Linux
目录 xrdp方式 vnc方式 xrdp方式 ----------------------------------------------------------------------------- ...
- 1、linux网络服务实验 用PuTTY连接Linux
这个是大三下学期的Linux网络服务配置详解时,感觉老师上得简单,就整理下,岭南师范学院师弟妹有福,如果是蔡老师交的话,可以拿来预习,复习. 一.用PuTTY连接Linux ①.装有redhat系统的 ...
随机推荐
- 业务线接入前端异常监控sentry
1.前端异常处理的框架对比 是否开源 收费 语言 监控范围 sentry 是 自己搭建服务器(免费)价格 英文 Angular.AngularJs.Backbone.Ember.JavaScrip ...
- swool配置ssl
1 yum install openssl --enable-openssl -y 2 切换在swoole 安装目录 cd /usr/local/swoole 3 ./configure --en ...
- JAVA基础复习与总结<十> Ruannable和Callable
程序运行原理 1.分时调度:所有线程轮流使用CPU的使用权,平均分配给每个线程占用CPU的时间. 2.抢占式调度:优先让优先级高的线程使用CPU,如果线程的优先级相同,那么会随机选择一个(线程随机性 ...
- Big big world
Big big world I'm a big big girl 我是个大女孩 in a big big world 在这个大大的世界上 It's not a big big thing if you ...
- Python数据处理PDF
Python数据处理(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1h8a5-iUr4mF7cVujgTSGOA 提取码:6fsl 复制这段内容后打开百度网盘手机A ...
- RSP小组——团队冲刺博客三
RSP小组--团队冲刺博客三 冲刺日期:2018年12月12日 各成员今日(12.12)完成的任务 马瑞蕃页面布局 李闻洲音乐代码的实现 赵乾宸,找bug,处理bug,使游戏滑动,消除实现 蒋子行会议 ...
- 数据分析——pandas
简介 import pandas as pd # 在数据挖掘前一个数据分析.筛选.清理的多功能工具 ''' pandas 可以读入excel.csv等文件:可以创建Series序列,DataFrame ...
- 将普通用户添加到sudo
将普通用户添加到sudo组 可以编辑/etc/sudoers文件将普通用户加入sudo组.要注意的是修改该文件只能使用visudo命令:1.首先切换到root #su - (注意有 “-” ,这和su ...
- 如何解决PeopleSoft Process Scheduler发布问题
常见PeopleSoft进程调度程序发布问题 此发布问题中遇到的一些常见错误是: 将HTTP响应转换为UCS2时出错 XML文档对象创建失败. 无法处理来自Report Repository的HTTP ...
- ef.core Mysql
Entity层 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; ...