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

一个简单的jsch链接linux并执行命令的utils。

importjava.io.BufferedReader;

    importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importcom.jcraft.jsch.Channel;importcom.jcraft.jsch.ChannelExec;importcom.jcraft.jsch.JSch;importcom.jcraft.jsch.JSchException;importcom.jcraft.jsch.Session;publicclassShellUtils{privatestaticJSchjsch;privatestaticSessionsession;/***连接到指定的IP**@throwsJSchException*/publicstaticvoidconnect(Stringuser,Stringpasswd,Stringhost)throwsJSchException{jsch=newJSch();session=jsch.getSession(user,host,22);session.setPassword(passwd);java.util.Propertiesconfig=newjava.util.Properties();config.put("StrictHostKeyChecking","no");session.setConfig(config);session.connect();}/***执行相关的命令*@throwsJSchException*/publicstaticvoidexecCmd(Stringcommand,Stringuser,Stringpasswd,Stringhost)throwsJSchException{connect(user,passwd,host);BufferedReaderreader=null;Channelchannel=null;try{while(command!=null){channel=session.openChannel("exec");((ChannelExec)channel).setCommand(command);channel.setInputStream(null);((ChannelExec)channel).setErrStream(System.err);channel.connect();InputStreamin=channel.getInputStream();reader=newBufferedReader(newInputStreamReader(in));Stringbuf=null;while((buf=reader.readLine())!=null){System.out.println(buf);}}}catch(IOExceptione){e.printStackTrace();}catch(JSchExceptione){e.printStackTrace();}finally{try{reader.close();}catch(IOExceptione){e.printStackTrace();}channel.disconnect();session.disconnect();}}}

值不值得,真是不足为外人道,自己心里有数就行。

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

相关文章:

  • 【算法】直接插入排序C语言实现
  • 嵌入式 FAAC1.28 在海思HI3518C/HI3518A平台linux中的编译优化
  • Android 动画animation 深入分析
  • Mybatis极其(最)简(好)单(用)的一个分页插件
  • Ext JS Kitchen Sink [Learning by doing](2)ArrayGrid
  • 你感兴趣的文章:

    标签云:

    亚洲高清电影在线, 免费高清电影, 八戒影院夜间, 八戒电影最新大片, 出轨在线电影, 午夜电影院, 在线影院a1166, 在线电影院, 在线观看美剧下载, 日本爱情电影, 日韩高清电影在线, 电影天堂网, 直播盒子app, 聚合直播, 高清美剧, 高清美剧在线观看 EhViewer-E站, E站, E站绿色版, qqmulu.com, qq目录网, qq网站目录,