当前位置: 首页 > news >正文

邢台做网站推广价格网站制作的内容什么好

邢台做网站推广价格,网站制作的内容什么好,泉州关键词排名,安徽和县住房城乡建设局网站java远程连接Linux执行命令的三种方式 1. 使用JDK自带的RunTime类和Process类实现2. ganymed-ssh2 实现3. jsch实现4. 完整代码#xff1a;执行shell命令下载和上传文件 1. 使用JDK自带的RunTime类和Process类实现 public static void main(String[] args){Process proc Run… java远程连接Linux执行命令的三种方式 1. 使用JDK自带的RunTime类和Process类实现2. ganymed-ssh2 实现3. jsch实现4. 完整代码执行shell命令下载和上传文件 1. 使用JDK自带的RunTime类和Process类实现 public static void main(String[] args){Process proc RunTime.getRunTime().exec(cd /home/tom; ls;)// 标准输入流必须写在 waitFor 之前String inStr consumeInputStream(proc.getInputStream());// 标准错误流必须写在 waitFor 之前String errStr consumeInputStream(proc.getErrorStream());int retCode proc.waitFor();if(retCode 0){System.out.println(程序正常执行结束);} }/*** 消费inputstream并返回*/ public static String consumeInputStream(InputStream is){BufferedReader br new BufferedReader(new InputStreamReader(is));String s ;StringBuilder sb new StringBuilder();while((sbr.readLine())!null){System.out.println(s);sb.append(s);}return sb.toString(); }2. ganymed-ssh2 实现 pom !--ganymed-ssh2包-- dependencygroupIdch.ethz.ganymed/groupIdartifactIdganymed-ssh2/artifactIdversionbuild210/version /dependencyimport ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session;public static void main(String[] args){String host 210.38.162.181;int port 22;String username root;String password root;// 创建连接Connection conn new Connection(host, port);// 启动连接conn.connection();// 验证用户密码conn.authenticateWithPassword(username, password);Session session conn.openSession();session.execCommand(cd /home/winnie; ls;);// 消费所有输入流String inStr consumeInputStream(session.getStdout());String errStr consumeInputStream(session.getStderr());session.close;conn.close(); }/*** 消费inputstream并返回*/ public static String consumeInputStream(InputStream is){BufferedReader br new BufferedReader(new InputStreamReader(is));String s ;StringBuilder sb new StringBuilder();while((sbr.readLine())!null){System.out.println(s);sb.append(s);}return sb.toString(); }3. jsch实现 pom dependencygroupIdcom.jcraft/groupIdartifactIdjsch/artifactIdversion0.1.55/version /dependencyimport com.jcraft.jsch.JSch; import com.jcraft.jsch.Session;public static void main(String[] args){String host 210.38.162.181;int port 22;String username root;String password root;// 创建JSchJSch jSch new JSch();// 获取sessionSession session jSch.getSession(username, host, port);session.setPassword(password);Properties prop new Properties();prop.put(StrictHostKeyChecking, no);session.setProperties(prop);// 启动连接session.connect();ChannelExec exec (ChannelExec)session.openChannel(exec);exec.setCommand(cd /home/winnie; ls;);exec.setInputStream(null);exec.setErrStream(System.err);exec.connect();// 消费所有输入流必须在exec之后String inStr consumeInputStream(exec.getInputStream());String errStr consumeInputStream(exec.getErrStream());exec.disconnect();session.disconnect(); }/*** 消费inputstream并返回*/ public static String consumeInputStream(InputStream is){BufferedReader br new BufferedReader(new InputStreamReader(is));String s ;StringBuilder sb new StringBuilder();while((sbr.readLine())!null){System.out.println(s);sb.append(s);}return sb.toString(); }4. 完整代码 执行shell命令 dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.6/version /dependency dependencygroupIdcom.jcraft/groupIdartifactIdjsch/artifactIdversion0.1.55/version /dependency dependencygroupIdch.ethz.ganymed/groupIdartifactIdganymed-ssh2/artifactIdversionbuild210/version /dependencyimport cn.hutool.core.io.IoUtil; import com.jcraft.jsch.ChannelShell; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Vector;/*** shell脚本调用类** author Micky*/ public class SshUtil {private static final Logger logger LoggerFactory.getLogger(SshUtil.class);private VectorString stdout;// 会话sessionSession session;//输入IP、端口、用户名和密码连接远程服务器public SshUtil(final String ipAddress, final String username, final String password, int port) {try {JSch jsch new JSch();session jsch.getSession(username, ipAddress, port);session.setPassword(password);session.setConfig(StrictHostKeyChecking, no);session.connect(100000);} catch (Exception e) {e.printStackTrace();}}public int execute(final String command) {int returnCode 0;ChannelShell channel null;PrintWriter printWriter null;BufferedReader input null;stdout new VectorString();try {channel (ChannelShell) session.openChannel(shell);channel.connect();input new BufferedReader(new InputStreamReader(channel.getInputStream()));printWriter new PrintWriter(channel.getOutputStream());printWriter.println(command);printWriter.println(exit);printWriter.flush();logger.info(The remote command is: );String line;while ((line input.readLine()) ! null) {stdout.add(line);System.out.println(line);}} catch (Exception e) {e.printStackTrace();return -1;}finally {IoUtil.close(printWriter);IoUtil.close(input);if (channel ! null) {channel.disconnect();}}return returnCode;}// 断开连接public void close(){if (session ! null) {session.disconnect();}}// 执行命令获取执行结果public String executeForResult(String command) {execute(command);StringBuilder sb new StringBuilder();for (String str : stdout) {sb.append(str);}return sb.toString();}public static void main(String[] args) {String cmd ls /opt/;SshUtil execute new SshUtil(XXX,abc,XXX,22);// 执行命令String result execute.executeForResult(cmd);System.out.println(result);execute.close();} }下载和上传文件 /*** 下载和上传文件*/ public class ScpClientUtil {private String ip;private int port;private String username;private String password;static private ScpClientUtil instance;static synchronized public ScpClientUtil getInstance(String ip, int port, String username, String passward) {if (instance null) {instance new ScpClientUtil(ip, port, username, passward);}return instance;}public ScpClientUtil(String ip, int port, String username, String passward) {this.ip ip;this.port port;this.username username;this.password passward;}public void getFile(String remoteFile, String localTargetDirectory) {Connection conn new Connection(ip, port);try {conn.connect();boolean isAuthenticated conn.authenticateWithPassword(username, password);if (!isAuthenticated) {System.err.println(authentication failed);}SCPClient client new SCPClient(conn);client.get(remoteFile, localTargetDirectory);} catch (IOException ex) {ex.printStackTrace();}finally{conn.close();}}public void putFile(String localFile, String remoteTargetDirectory) {putFile(localFile, null, remoteTargetDirectory);}public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory) {putFile(localFile, remoteFileName, remoteTargetDirectory,null);}public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) {Connection conn new Connection(ip, port);try {conn.connect();boolean isAuthenticated conn.authenticateWithPassword(username, password);if (!isAuthenticated) {System.err.println(authentication failed);}SCPClient client new SCPClient(conn);if ((mode null) || (mode.length() 0)) {mode 0600;}if (remoteFileName null) {client.put(localFile, remoteTargetDirectory);} else {client.put(localFile, remoteFileName, remoteTargetDirectory, mode);}} catch (IOException ex) {ex.printStackTrace();}finally{conn.close();}}public static void main(String[] args) {ScpClientUtil scpClient ScpClientUtil.getInstance(XXX, 22, XXX, XXX);// 从远程服务器/opt下的index.html下载到本地项目根路径下scpClient.getFile(/opt/index.html,./);// 把本地项目下根路径下的index.html上传到远程服务器/opt目录下scpClient.putFile(./index.html,/opt);} }
http://www.dnsts.com.cn/news/147733.html

相关文章:

  • 个人 网站建设wordpress纯笔记主题
  • 网站后端技术语言个人做网站开发
  • 公司网站备案需要什么开发公司起名大全
  • 微信小程序开发和网站开发的区别展台设计公司
  • 影视网站建设多少钱佛山产品设计公司
  • 网站开发验收模板做网站什么时候注册商标
  • 下列哪个网站不属于sns(社交网络)u盘启动盘制作工具
  • 医疗器械网站建设策划书电子商城网站的设计与实现
  • 食品行业网站源码昆明网站建设的公司
  • 职校网站模板网站开发与网站设计区别
  • 泰州做网站淘宝做一个电影网站需要多少钱
  • 增城网站怎么做seo常营网站建设
  • 自助网站建设 网易沈阳h5网站建设
  • 网站规划与建设课程来宾建设网站
  • 网站规划的类型怎么做网上卖货
  • 网站管理怎么做百度免费推广网站
  • 网站开发环境的配置设置网站建设方案
  • 建网站 西安iis添加网站ip地址
  • 营销型网站的建设智慧校园信息门户网站建设
  • 集团网站设计案例wordpress时区问题
  • 南京 高端网站制作wordpress伪静态后二级目录错误
  • 企业为什么做网站优化推广wordpress安装路径
  • 中国广东网站建设在哪些网站能接到活做
  • 网站建设系统哪家好旅游的网页设计模板
  • 手机网站怎么备案百度推广网站吸引力
  • 网站经营科协网站建设的意见
  • 福建省漳州市建设厅网站企业网站建设的经验心得
  • 迅速编程做网站自己开发的软件如何赚钱
  • 电子商务网站建设需求文档wordpress在线题库
  • 信息管理网站开发的视频教程一个女装店网站建设的策划模板