我想用

java代码连接我的unix服务器.首先它将连接到我的服务器位置.在里面,它将连接到我的服务器名称.但那时它会要求确认(是/否)和密码我不知道该怎么做?

这是我所做的代码.请帮我解决这个问题

public static void main (String args[]) {

String user = "user";

String password = "password";

String host = "hostName";

int port=22;

//String remoteFile="/home/john/test.txt";

//String yes="yes";

try {

JSch jsch = new JSch();

Session session = jsch.getSession(user, host, port);

session.setPassword(password);

session.setConfig("StrictHostKeyChecking", "no");

System.out.println("Establishing Connection...");

session.connect();

Channel channel = session.openChannel("exec");

// After this it'll ask for confirmation and password

((ChannelExec)channel).setCommand("ssh myServerName");

channel.connect();

InputStream output = channel.getInputStream();

System.out.println("aafter stream");

int readByte = output.read();

StringBuilder outputBuffer = new StringBuilder();

while (readByte != 0xffffffff) {

//System.out.println("read byte" + readByte);

outputBuffer.append((char)readByte);

readByte = output.read();

}

System.out.println(outputBuffer.toString());

channel.disconnect();

} catch (Exception e){

System.err.print("error message" + e);

}

}

最佳答案 您需要获取JSch安全通道的OutputStream并在其中插入密码.

Channel channel = session.openChannel("exec");

((ChannelExec) channel).setCommand("sudo -S -p '' " + command);

channel.setInputStream(null);

OutputStream out = channel.getOutputStream();

((ChannelExec) channel).setErrStream(System.err);

InputStream in = channel.getInputStream();

((ChannelExec) channel).setPty(true);

channel.connect();

out.write((password + "\n").getBytes());

out.flush();

java ssh连接 如何不使用密码_如何在java中为ssh连接设置密码相关推荐

  1. rdp连接工具_如何在Windows10中清除RDP连接历史记录?

    如何在Windows中清除RDP连接历史记录? 内置的Windows远程桌面连接(RDP)客户端(mstsc.exe)保存每次成功连接到远程计算机后的远程计算机名(或IP地址)和用于登录的用户名.在下 ...

  2. linux 更改ssh端口_如何在Linux中更改SSH端口-简易指南

    linux 更改ssh端口 The default port on SSH is 22. But for security reasons, it's a good idea to change SS ...

  3. java中将字符串顺序反传转_如何在Java中将字符串序列化的Erlang术语反序列化为JInterface对象?...

    我的接口系统提供了来自Erlang世界的结果,该结果发送了erlang术语的字符串表示形式,例如元组列表: [ {"key1" , ["AAA","B ...

  4. linux中更改用户密码_如何在Linux中更改用户密码

    linux中更改用户密码 In this tutorial, we will focus on how you can change a user's password in Linux. We wi ...

  5. wordpress默认密码_如何在WordPress中为新用户设置默认管理员配色方案

    wordpress默认密码 One of the most talked about feature of WordPress 3.8 is the new admin interface. It i ...

  6. java将输出结果写入csv文件_如何在Java中将数据写入.csv文件?

    名为OpenCSV的库提供API来从.CSV文件读取数据或将数据写入.CSV文件.此处说明了如何使用Java程序写入.csv文件的内容. Maven依赖 com.opencsv opencsv 4.4 ...

  7. Java错误提示框口怎么使用_如何在Swing中显示错误消息对话框?

    以下示例展示了如何在基于swing的应用程序中显示错误消息警告. 使用以下API - JOptionPane - 创建标准对话框. JOptionPane.showMessageDialog() - ...

  8. centos root密码_如何在CentOS中恢复丢失的root密码

    centos root密码 In Linux, when you forget your account password, you can easily reset it using a root ...

  9. java如何新建一个空的压缩包_如何在Java中创建zip文件

    慕哥6287543 Java 7内置了ZipFileSystem,可用于从zip文件创建,写入和读取文件.Java Doc:ZipFileSystem ProviderMap env = new Ha ...

最新文章

  1. 详解LINUX 的DHCP服务
  2. JSR380(Bean Validation 2.0)
  3. 凭证 90000000 保存(帐户确定出错)
  4. WSUS专题之二:部署与规划-参数选择
  5. docker下安装Pillow模块
  6. python怎么创建txt文件啊_python根据txt文本批量创建文件夹
  7. Python 开发者 2017 应该关注的 7 个类库
  8. select2,利用ajax高效查询大数据列表(可搜索、可分页)
  9. Caffe傻瓜系列(4):其它常用层及参数
  10. SuperMap GIS 10i软件概览
  11. windows 网卡驱动安装
  12. 你知道这些SOLIDWORKS零件图知识吗?
  13. html onclick点击事件失效,HTML onfocus,onclick事件不起作用
  14. 使用git命令提交远程github仓库的时候提示rejected(拒绝)解决办法
  15. 坦白说php源码,qq坦白说新思路解密 附源码
  16. AB测试的来源及适用场景
  17. 手机塔防游戏-手机塔防游戏收入、毛利率、市场规模及市场份额
  18. 因果推断what if
  19. android原生系统 抽屉,Android N或取消原生应用抽屉
  20. vue延迟渲染组件_Vue 动态组件渲染问题分析

热门文章

  1. (转载)The shortest, fastest, and easiest way to compare two tables in SQL Server: UNION
  2. WOJ 43 电话邀请
  3. 【BZOJ 1415】 1415: [Noi2005]聪聪和可可 (bfs+记忆化搜索+期望)
  4. RNQOJ Jam的计数法
  5. Python学习笔记--2--面向对象编程
  6. 8天学通MongoDB——第五天 主从复制
  7. 在Indicator中添加动态Checkbox,无需绑定数据源,支持全选 - Ehlib学习(二)
  8. xml处理相关文章收藏
  9. UA MATH564 概率分布总结
  10. 关于数据存储的那些事1