JAVA-Python-C#-C++实现SSH2远程调用Linux主机执行命令

JAVA

1import java.io.BufferedReader; 2import java.io.IOException; 3import java.io.InputStream; 4import java.io.InputStreamReader; 5import com.jcraft.jsch.ChannelExec; 6import com.jcraft.jsch.JSch; 7import com.jcraft.jsch.JSchException; 8import com.jcraft.jsch.Session; 9 10public class SSHHelper { 11 /** 12 * 远程 执行命令并返回结果调用过程 是同步的(执行完才会返回) 13 * @param host linux主机名 14 * @param user 用户名 15 * @param psw 密码 16 * @param port 端口 17 * @param command 命令行 18 * @return 19 */ 20 public static String exec(String host,String user,String psw,int port,String command){ 21 StringBuffer sb= new StringBuffer(); 22 Session session =null; 23 ChannelExec openChannel =null; 24 try { 25 JSch jsch=new JSch(); 26 session = jsch.getSession(user, host, port); 27 java.util.Properties config = new java.util.Properties(); 28 config.put("StrictHostKeyChecking", "no");//跳过公钥的询问 29 session.setConfig(config); 30 session.setPassword(psw); 31 session.connect(5000);//设置连接的超时时间 32 openChannel = (ChannelExec) session.openChannel("exec"); 33 openChannel.setCommand(command); //执行命令 34 int exitStatus = openChannel.getExitStatus(); //退出状态为-1,直到通道关闭 35 System.out.println(exitStatus); 36 37 // 下面是得到输出的内容 38 openChannel.connect(); 39 InputStream in = openChannel.getInputStream(); 40 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 41 String buf = null; 42 while ((buf = reader.readLine()) != null) { 43 sb.append(buf+"\n"); 44 } 45 } catch (JSchException | IOException e) { 46 sb.append(e.getMessage()+"\n"); 47 }finally{ 48 if(openChannel!=null&&!openChannel.isClosed()){ 49 openChannel.disconnect(); 50 } 51 if(session!=null&&session.isConnected()){ 52 session.disconnect(); 53 } 54 } 55 return sb.toString(); 56 } 57 58 59 public static void main(String args[]){ 60 String exec = exec("***.***.***.***", "***", "***", 22, "ls"); 61 System.out.println(exec); 62 } 63}

Python

1import paramiko 2 3ssh = paramiko.SSHClient() 4ssh.load_system_host_keys() 5ssh.connect(hostname='***.***.***.***', port=22,username='root', password='password') 6stdin, stdout, stderr = ssh.exec_command("ls -lh") 7print(stdout.read().decode('UTF-8', 'ignore')) 8ssh.close()

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7using Tamir.SharpSsh; 8 9namespace SSHTest{ 10class Program 11{ 12 13public static string ssh_conn(string ip, string root, string pass, string command) 14 { 15 16 SshStream ssh = new SshStream(ip, root, pass); 17 ssh.Prompt = "#"; 18 ssh.RemoveTerminalEmulationCharacters = true; 19 string response = ssh.ReadResponse(); 20 ssh.Write(command); 21 ssh.Flush(); 22 ssh.Write("/n"); 23 response = ssh.ReadResponse(); 24 //Console.WriteLine(response); 25 return response; 26 27 } 28} 29 30}

C++

1#include <iostream> 2#include "ssh2.h" 3 4int main(int argc, const char * argv[]) 5{ 6 using namespace std; 7 using namespace fish; 8 9 Ssh2 ssh("***.***.***.***"); 10 ssh.Connect("test","xxxxxx"); 11 Channel* channel = ssh.CreateChannel(); 12 channel->Write("cd /;pwd"); 13 cout<<channel->Read()<<endl; 14 channel->Write("ssh 127.0.0.1"); 15 cout<<channel->Read(":")<<endl; 16 channel->Write("xxxxxx"); 17 cout<<channel->Read()<<endl; 18 channel->Write("pwd"); 19 cout<<channel->Read()<<endl; 20 delete channel; 21 return 0; 22}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

JAVA-Python-C#-C++实现SSH2远程调用Linux主机执行命令 - HelloWorld