Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

33
LINES

< > BotCompany Repo | #1003579 // quickSSH - issue SSH command on server and return output

JavaX fragment (include)

1  
lib 1000308 // sshj.jar
2  
3  
import net.schmizz.sshj.SSHClient;
4  
import net.schmizz.sshj.common.IOUtils;
5  
import net.schmizz.sshj.connection.channel.direct.Session;
6  
7  
// not sure which encoding is used for input and output
8  
// pass == null => try our public key
9  
static S quickSSH(S host, S verifier, S pass, S command, int timeoutSeconds) ctex {
10  
  final new SSHClient client;
11  
  client.loadKnownHosts();
12  
  client.connect(host);
13  
  try {
14  
    if (pass == null)
15  
      client.authPublickey(user);
16  
    else
17  
      client.authPassword(user, pass);
18  
19  
    final Session session = client.startSession();
20  
    //print("Started session");
21  
    try {
22  
      //print("Executing command: " + command);
23  
      final Session.Command cmd = session.exec(command);
24  
      S output = IOUtils.readFully(cmd.getInputStream()).toString();
25  
      cmd.join(timeoutSeconds, TimeUnit.SECONDS);
26  
      ret output;
27  
    } finally {
28  
      session.close();
29  
    }
30  
  } finally {
31  
    client.disconnect();
32  
  }
33  
}

Author comment

Began life as a copy of #1001229

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003579
Snippet name: quickSSH - issue SSH command on server and return output
Eternal ID of this version: #1003579/1
Text MD5: 95c22188fd44ae5a8a600a897629ab30
Author: stefan
Category: javax / networking
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-03 14:29:21
Source code size: 990 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 633 / 596
Referenced in: [show references]