一个简单的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();}}}
值不值得,真是不足为外人道,自己心里有数就行。
相关文章:
你感兴趣的文章:
标签云:
亚洲高清电影在线,
免费高清电影,
八戒影院夜间,
八戒电影最新大片,
出轨在线电影,
午夜电影院,
在线影院a1166,
在线电影院,
在线观看美剧下载,
日本爱情电影,
日韩高清电影在线,
电影天堂网,
直播盒子app,
聚合直播,
高清美剧,
高清美剧在线观看
EhViewer-E站,
E站,
E站绿色版,
qqmulu.com,
qq目录网,
qq网站目录,