3 windows环境与shell交互操作
/**
* 由SshConfig配置获取一个Session
* @param conf
* @return
*/
public static Session createSession(SshConfig conf){
LOG.info("try connect to " + conf.getHostIp() + ",username: " + conf.getUsername() + ",password: " + conf.getPassword() + ",port: " + conf.getPort());
JSch jSch=new JSch(); //创建JSch对象
Session session= null;//根据用户名,主机ip和端口获取一个Session对象
try {
session = jSch.getSession(conf.getUsername(), conf.getHostIp(), conf.getPort());
session.setPassword(conf.getPassword()); //设置密码
Properties config=new Properties();
//StrictHostKeyChecking no
//"StrictHostKeyChecking"如果设为"yes",ssh将不会自动把计算机的密匙加入"$HOME/.ssh/known_hosts"文件,且一旦计算机的密匙发生了变化,就拒绝连接
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);//为Session对象设置properties
session.setTimeout(conf.getTimeOut());//设置超时
session.connect();//通过Session建立连接
LOG.info("connect to {} success",conf.getHostIp());
} catch (JSchException e) {
LOG.error("connect to {} fail, connect infos:{}",conf.getHostIp(),conf.toString());
e.printStackTrace();
}
return session;
} /**
* 关闭session
* @param session
*/
public static void closeSession(Session session){
session.disconnect();
}
/**
* 从src文件拿到本地dst文件
* @param session
* @param src
* @param dst
* @return
*/
public static Boolean downLoad(Session session,String src,String dst){
Boolean success=true;
//src linux服务器文件地址,dst 本地存放地址
ChannelSftp channelSftp= null;
try {
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(src, dst);
channelSftp.quit();
} catch (JSchException e) {
success=false;
e.printStackTrace();
LOG.error(e.getMessage());
} catch (SftpException e) {
success=false;
e.printStackTrace();
LOG.error(e.getMessage());
}
return success;
}
/**
* 由ip 用户名 密码 获取该ip对应的hostname
* @param hostIp
* @param username
* @param password
* @return
*/
public static String getHostName(String hostIp, String username, String password){
Session session=createSession(new SshConfig(hostIp,username,password));
String hostname = exeCommand(session,"hostname");
return hostname.trim();
}
/**
* 执行command命令,把执行结果返回
* @param session
* @param command
* @return
*/
public static String exeCommand(Session session,String command){
ChannelExec channelExec = null;
String out=null;
try {
channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.setErrStream(System.err);
channelExec.connect();
out = IOUtils.toString(in, "UTF-8"); channelExec.disconnect();
//session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(null==out){
LOG.error("execute {} error",command);
out="";
}
return out;
}
public class SshConfig {
private String hostIp;
private String username;
private String password;
//默认端口22
private int port=;
//10个小时超时
private int timeOut=***;
public SshConfig(String hostIp, String username, String password) {
this.hostIp = hostIp;
this.password = password;
this.username = username;
}
public String getHostIp() {
return hostIp;
}
public void setHostIp(String hostIp) {
this.hostIp = hostIp;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getTimeOut() {
return timeOut;
}
public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
}
@Override
public String toString() {
String builder="\nhostIp"+" : " + hostIp
+"\nusername"+" : " + username
+"\npassword"+" : " + password
+"\nport"+" : " + port
+"\ntimeOut"+" : " + timeOut;
return builder;
}
}
3 windows环境与shell交互操作的更多相关文章
- python操作三大主流数据库(1)python操作mysql①windows环境中安装python操作mysql数据库的MySQLdb模块mysql-client
windows安装python操作mysql数据库的MySQLdb模块mysql-client 正常情况下应该是cmd下直接运行 pip install mysql-client 命令即可,试了很多台 ...
- windows的类似shell 命令操作
一,类似linux的grep命令 参考网站:https://www.cnblogs.com/zxy1992/p/4372717.html windows执行cmd命令查找sql进程: C:\Users ...
- Shell之操作环境
目录 Shell之操作环境 参考 命令运行顺序 欢迎信息 环境配置 Shell之操作环境
- windows环境jar包部署到linux服务器,一键操作
背景: windows系统下生成的jar包通过FTP上传到linux服务器,然后通过XShell进行jar包的发布,这样反复了几个月后,开发阶段需要频繁更新包的部署.个人觉得很繁琐,想一键式把这个工作 ...
- Shell及其操作环境
来源: 鸟哥的Linux私房菜第十章.認識與學習BASH Shell是什么?1分钟理解Shell的概念! ssh在本地调用远程主机上的命令,不登录远程主机shell 一.Shell Shell 是一个 ...
- windows环境30分钟从0开始快速搭建第一个docker项目(带数据库交互)
前言 小白直接上手 docker 构建我们的第一个项目,简单粗暴,后续各种概念边写边了解,各种概念性的内容就不展开,没了解过的点击 Docker 教程 进行初步了解. Docker 是一个开源的应用 ...
- 第十一章、认识与学习 BASH Bash Shell 的操作环境
bash中的变量动不动就说环境变量,真是奇怪,bash只是一个c语言编写的程序而已,跟环境变量有什么关系?如果知道dos的历史的话就知道有个时代是只有命令行界面而没有图形用户界面,这只小小的程序就包揽 ...
- windows环境下protobuf的java操作{编译,序列化,反序列化}
google protocol buffer的使用和原理 概况: Protocol Buffers(也就是protobuf)是谷歌的语言中立的.平台中立的.可扩展的用于序列化结构化的数据: windo ...
- windows环境下安装pymysql(操作带图)
在windows环境下安装pymysql,首先要找到python的安装位置,如果在c盘,启动cmd的时候,要获取管理员权限. 具体步骤,一,管理员模式启动cmd.在箭头指定位置,搜索cmd,出现快捷方 ...
随机推荐
- Android基本组件-Activity
什么是Activity 在Android程序中,每个Activity继承自android.app.Activity, 有一个与之对应的xml布局文件, 一个界面对应一个Activity, 每个Acti ...
- linux笔记2.25
解决vsftpd用root登录不了的问题 把/etc/pam.d/vsftpd文件中的 auth required pam_listfile.so item=user sense=d ...
- PHPCMS V9 如何启用伪静态
最近在研究CMS时候,首先是使用DEDECMS,后来又转到了PHPCMS,感觉后者架构更加合理,而前者主要是模板众多,故使用者多一些,不过我都是需要自己写模板,那就无所谓了. 玩各种CMS我喜欢首先看 ...
- CentOS6.4 GOOGLE chrome install
yum install http://people.centos.org/hughesjr/chromium/6/i386/RPMS/chromium-28.0.1500.45-205727.i686 ...
- C++Primer charpter1.
一.输入输出流 endl:会刷新buffer.刷新之后你才能看到.不手动用endl的话,就只能依靠系统自动刷.程序崩溃的话,你看到的调试信息可能是错误的. >>: 两个连续的符号 ci ...
- C语言初学 给已知公式求圆周率
公式: 圆周率=1-1/3+1/5-1/7+......+1/(4n-3)-1/(4n-1) #include<stdio.h> #include<math.h> main() ...
- 转:CFile::Seek
CFile::Seek virtual LONG Seek(LONG lOff,UINT nFrom); throw(CFileException); 返回值:如果要求的位置合法,则Seek返回从文 ...
- ssh公钥自动登陆
第一步,在服务器上安装ssh服务 sudo apt-get install ssh 通过ssh -v查看是否安装成功 第二步创建本地公钥秘钥对 ssh-keygen -t rsa //创建ssh公钥 ...
- 文本阴影:text-shadow
例如: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- QTableWidget 用法总结(只能使用标准的数据模型,并且其单元格数据是QTableWidgetItem的对象)
QTableWidget是QT程序中常用的显示数据表格的空间,很类似于VC.C#中的DataGrid.说到QTableWidget,就必须讲一下它跟QTabelView的区别了.QTableWidge ...