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}