{"id":1401,"date":"2022-07-25T13:36:02","date_gmt":"2023-05-26T19:38:02","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/"},"modified":"2024-03-07T14:49:17","modified_gmt":"2024-03-07T14:49:17","slug":"one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/","title":{"rendered":"JSch library to execute shell commands on a Unix"},"content":{"rendered":"<p>In today&#8217;s tutorial, we will explore the example of JSch. JSch is a tool in Java that allows us to establish an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Secure_Shell#:~:text=The%20Secure%20Shell%20Protocol%20(SSH,Protocol%20stack\">SSH<\/a> connection. I have previously written a program to connect to a remote database on an SSH server. Today, I will be sharing a program that connects to an SSH-enabled server and executes shell commands. For this task, I will be utilizing JSch to establish the connection from a Java program.<\/p>\n<h2>Example of JSch<\/h2>\n<p>You have the option to download the JSch jar from its official website or obtain the JSch jars through the provided maven dependency.<\/p>\n<pre class=\"post-pre\"><code>&lt;dependency&gt;\r\n    &lt;groupId&gt;com.jcraft&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;jsch&lt;\/artifactId&gt;\r\n    &lt;version&gt;0.1.53&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/code><\/pre>\n<p>Here is a basic program written in JSch to execute the command &#8220;ls -ltr&#8221; on the server.<\/p>\n<pre class=\"post-pre\"><code>import java.io.InputStream;\r\n\r\nimport com.jcraft.jsch.Channel;\r\nimport com.jcraft.jsch.ChannelExec;\r\nimport com.jcraft.jsch.JSch;\r\nimport com.jcraft.jsch.Session;\r\n\r\n\r\npublic class JSchExampleSSHConnection {\r\n\r\n\t\/**\r\n\t * JSch Example Tutorial\r\n\t * Java SSH Connection Program\r\n\t *\/\r\n\tpublic static void main(String[] args) {\r\n\t    String host=\"ssh.scdev.com\";\r\n\t    String user=\"sshuser\";\r\n\t    String password=\"sshpwd\";\r\n\t    String command1=\"ls -ltr\";\r\n\t    try{\r\n\t    \t\r\n\t    \tjava.util.Properties config = new java.util.Properties(); \r\n\t    \tconfig.put(\"StrictHostKeyChecking\", \"no\");\r\n\t    \tJSch jsch = new JSch();\r\n\t    \tSession session=jsch.getSession(user, host, 22);\r\n\t    \tsession.setPassword(password);\r\n\t    \tsession.setConfig(config);\r\n\t    \tsession.connect();\r\n\t    \tSystem.out.println(\"Connected\");\r\n\t    \t\r\n\t    \tChannel channel=session.openChannel(\"exec\");\r\n\t        ((ChannelExec)channel).setCommand(command1);\r\n\t        channel.setInputStream(null);\r\n\t        ((ChannelExec)channel).setErrStream(System.err);\r\n\t        \r\n\t        InputStream in=channel.getInputStream();\r\n\t        channel.connect();\r\n\t        byte[] tmp=new byte[1024];\r\n\t        while(true){\r\n\t          while(in.available()&gt;0){\r\n\t            int i=in.read(tmp, 0, 1024);\r\n\t            if(i&lt;0)break;\r\n\t            System.out.print(new String(tmp, 0, i));\r\n\t          }\r\n\t          if(channel.isClosed()){\r\n\t            System.out.println(\"exit-status: \"+channel.getExitStatus());\r\n\t            break;\r\n\t          }\r\n\t          try{Thread.sleep(1000);}catch(Exception ee){}\r\n\t        }\r\n\t        channel.disconnect();\r\n\t        session.disconnect();\r\n\t        System.out.println(\"DONE\");\r\n\t    }catch(Exception e){\r\n\t    \te.printStackTrace();\r\n\t    }\r\n\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>If you encounter any issues with running the JSch example program, please inform me. This program demonstrates a simple method in JSch to establish an SSH connection in a Java program. The JSch jar file can be obtained from the official website.<\/p>\n<p>&nbsp;<\/p>\n<p>More Tutorials<\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/common-errors-that-occur-when-using-nginx-for-connections\/\" target=\"_blank\" rel=\"noopener\">Common errors that occur when using Nginx for connections.<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/using-telnet-commands-in-linux-unix\/\" target=\"_blank\" rel=\"noopener\">Using Telnet Commands in Linux\/Unix<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/one-option-for-paraphrasing-the-given-phrase-could-be-different-server-configurations-frequently-used-for-your-web-application\/\" target=\"_blank\" rel=\"noopener\">Server Configurations Frequently Used for Your Web Application<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/spring-boot-cli\/\" target=\"_blank\" rel=\"noopener\">Spring Boot CLI<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/multithreading-in-java-that-you-need-to-know\/\" target=\"_blank\" rel=\"noopener\">multithreading in Java that you need to know<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s tutorial, we will explore the example of JSch. JSch is a tool in Java that allows us to establish an SSH connection. I have previously written a program to connect to a remote database on an SSH server. Today, I will be sharing a program that connects to an SSH-enabled server and executes [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1401","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JSch library to execute shell commands on a Unix - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"we will explore the example of JSch. JSch is a tool in Java that allows us to establish an SSH connection.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JSch library to execute shell commands on a Unix\" \/>\n<meta property=\"og:description\" content=\"we will explore the example of JSch. JSch is a tool in Java that allows us to establish an SSH connection.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-26T19:38:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-07T14:49:17+00:00\" \/>\n<meta name=\"author\" content=\"Emily Johnson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Emily Johnson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/\"},\"author\":{\"name\":\"Emily Johnson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\"},\"headline\":\"JSch library to execute shell commands on a Unix\",\"datePublished\":\"2023-05-26T19:38:02+00:00\",\"dateModified\":\"2024-03-07T14:49:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/\"},\"wordCount\":235,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/\",\"name\":\"JSch library to execute shell commands on a Unix - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2023-05-26T19:38:02+00:00\",\"dateModified\":\"2024-03-07T14:49:17+00:00\",\"description\":\"we will explore the example of JSch. JSch is a tool in Java that allows us to establish an SSH connection.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JSch library to execute shell commands on a Unix\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\",\"name\":\"Emily Johnson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"caption\":\"Emily Johnson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"JSch library to execute shell commands on a Unix - Blog - Silicon Cloud","description":"we will explore the example of JSch. JSch is a tool in Java that allows us to establish an SSH connection.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/","og_locale":"en_US","og_type":"article","og_title":"JSch library to execute shell commands on a Unix","og_description":"we will explore the example of JSch. JSch is a tool in Java that allows us to establish an SSH connection.","og_url":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2023-05-26T19:38:02+00:00","article_modified_time":"2024-03-07T14:49:17+00:00","author":"Emily Johnson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Emily Johnson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/"},"author":{"name":"Emily Johnson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378"},"headline":"JSch library to execute shell commands on a Unix","datePublished":"2023-05-26T19:38:02+00:00","dateModified":"2024-03-07T14:49:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/"},"wordCount":235,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/","url":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/","name":"JSch library to execute shell commands on a Unix - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2023-05-26T19:38:02+00:00","dateModified":"2024-03-07T14:49:17+00:00","description":"we will explore the example of JSch. JSch is a tool in Java that allows us to establish an SSH connection.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/one-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JSch library to execute shell commands on a Unix"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378","name":"Emily Johnson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","caption":"Emily Johnson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=1401"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1401\/revisions"}],"predecessor-version":[{"id":1785,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1401\/revisions\/1785"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=1401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=1401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=1401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}