java调用远程python命令,JAVA\Python\C#\C++实现SSH2远程调用Linux主机执行命令

JAVA

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

public class SSHHelper {

/**

* 远程 执行命令并返回结果调用过程 是同步的(执行完才会返回)

* @param host linux主机名

* @param user 用户名

* @param psw 密码

* @param port 端口

* @param command 命令行

* @return

*/

public static String exec(String host,String user,String psw,int port,String command){

StringBuffer sb= new StringBuffer();

Session session =null;

ChannelExec openChannel =null;

try {

JSch jsch=new JSch();

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

java.util.Properties config = new java.util.Properties();

config.put("StrictHostKeyChecking", "no");//跳过公钥的询问

session.setConfig(config);

session.setPassword(psw);

session.connect(5000);//设置连接的超时时间

openChannel = (ChannelExec) session.openChannel("exec");

openChannel.setCommand(command); //执行命令

int exitStatus = openChannel.getExitStatus(); //退出状态为-1,直到通道关闭

System.out.println(exitStatus);

// 下面是得到输出的内容

openChannel.connect();

InputStream in = openChannel.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String buf = null;

while ((buf = reader.readLine()) != null) {

sb.append(buf+"\n");

}

} catch (JSchException | IOException e) {

sb.append(e.getMessage()+"\n");

}finally{

if(openChannel!=null&&!openChannel.isClosed()){

openChannel.disconnect();

}

if(session!=null&&session.isConnected()){

session.disconnect();

}

}

return sb.toString();

}

public static void main(String args[]){

String exec = exec("***.***.***.***", "***", "***", 22, "ls");

System.out.println(exec);

}

}

Python

import paramiko

ssh = paramiko.SSHClient()

ssh.load_system_host_keys()

ssh.connect(hostname='***.***.***.***', port=22,username='root', password='password')

stdin, stdout, stderr = ssh.exec_command("ls -lh")

print(stdout.read().decode('UTF-8', 'ignore'))

ssh.close()

C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Tamir.SharpSsh;

namespace SSHTest{

class Program

{

public static string ssh_conn(string ip, string root, string pass, string command)

{

SshStream ssh = new SshStream(ip, root, pass);

ssh.Prompt = "#";

ssh.RemoveTerminalEmulationCharacters = true;

string response = ssh.ReadResponse();

ssh.Write(command);

ssh.Flush();

ssh.Write("/n");

response = ssh.ReadResponse();

//Console.WriteLine(response);

return response;

}

}

}

C++

#include

#include "ssh2.h"

int main(int argc, const char * argv[])

{

using namespace std;

using namespace fish;

Ssh2 ssh("***.***.***.***");

ssh.Connect("test","xxxxxx");

Channel* channel = ssh.CreateChannel();

channel->Write("cd /;pwd");

cout<Read()<

channel->Write("ssh 127.0.0.1");

cout<Read(":")<

channel->Write("xxxxxx");

cout<Read()<

channel->Write("pwd");

cout<Read()<

delete channel;

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,可以使用Java调用Python命令。可以使用Java的Runtime类来执行命令命令。下面是一个示例代码,展示了如何在Java调用Python命令: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class JavaCallPython { public static void main(String\[\] args) { try { // 创建Runtime对象 Runtime runtime = Runtime.getRuntime(); // 执行Python命令 Process process = runtime.exec("python3 /path/to/python_script.py"); // 获取命令执行的输出结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 等待命令执行完成 int exitCode = process.waitFor(); System.out.println("命令执行完成,退出码:" + exitCode); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们使用Java的Runtime类的exec()方法执行了一个Python命令。你需要将`/path/to/python_script.py`替换为你实际的Python脚本的路径。然后,我们通过读取命令的输出结果来获取Python脚本的执行结果。最后,我们使用waitFor()方法等待命令执行完成,并获取命令的退出码。 希望这个示例能帮助到你。如果你有任何其他问题,请随时提问。 #### 引用[.reference_title] - *1* [在linux中使用定时任务用java调用python程序](https://blog.csdn.net/weixin_45872600/article/details/128547273)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值