{"id":965,"date":"2022-09-06T18:22:25","date_gmt":"2022-08-29T15:35:12","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/ssh-example-tutorial-for-expect-script\/"},"modified":"2024-03-15T12:48:42","modified_gmt":"2024-03-15T12:48:42","slug":"ssh-example-tutorial-for-expect-script","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/","title":{"rendered":"SSH Example Tutorial for Expect Script"},"content":{"rendered":"<p>The Expect script is a valuable utility for Linux\/Unix systems. In my daily life, whether it is at work or on my hosting server, I frequently interact with numerous Unix servers. Due to the large number of servers involved, it becomes challenging to remember all the SSH user credentials along with the SU user credentials. To simplify this situation, I decided to create a script that would automatically log me into the servers. Initially, I faced a roadblock when attempting to enter the SSH password because standard Unix shells do not provide a means to send the password when prompted for login. However, I discovered the expect script, which allows for automation of interactions with programs that require terminal input.<\/p>\n<h2>Anticipate Script<\/h2>\n<p>Expect Script is highly intuitive to pick up, living up to its name by analyzing the command&#8217;s output and executing the specified instruction whenever it detects a match with the designated regular expression.<\/p>\n<h3>An example of an SSH script using Expect.<\/h3>\n<p>I have developed a script named sshsudologin.expect that enables automatic SSH server login, authentication as a super user, and execution of a basic command.<\/p>\n<pre class=\"post-pre\"><code>#!\/usr\/bin\/expect\r\n\r\n#Usage sshsudologin.expect &lt;host&gt; &lt;ssh user&gt; &lt;ssh password&gt; &lt;su user&gt; &lt;su password&gt;\r\n\r\nset timeout 60\r\n\r\nspawn ssh [lindex $argv 1]@[lindex $argv 0]\r\n\r\nexpect \"yes\/no\" { \r\n\tsend \"yes\\r\"\r\n\texpect \"*?assword\" { send \"[lindex $argv 2]\\r\" }\r\n\t} \"*?assword\" { send \"[lindex $argv 2]\\r\" }\r\n\r\nexpect \"# \" { send \"su - [lindex $argv 3]\\r\" }\r\nexpect \": \" { send \"[lindex $argv 4]\\r\" }\r\nexpect \"# \" { send \"ls -ltr\\r\" }\r\ninteract\r\n<\/code><\/pre>\n<h3>Key considerations regarding Expect Script<\/h3>\n<ol>Please take note of the initial line which specifies that the expect script will serve as the interpreter. To avoid any timeout issues in case the login prompt takes longer to appear, I have adjusted the timeout from the default 10 seconds to 60 seconds. Also, observe the use of the expect command with the associated regular expression and the desired response to send. The inclusion of the initial Yes\/No choice ensures that the script does not fail if the remote server key has not been imported yet. The expect regular expressions may differ depending on the servers \u2013 for my server, it ends with &#8220;#&#8221; but for others, it could be &#8220;$&#8221; \u2013 so please modify it accordingly. The important thing is to ensure that the regular expression matches the expect command, enabling it to send the appropriate command or password. Lastly, the final expect command demonstrates that we can send commands once logged into the server.<\/ol>\n<p>This is what happens when I execute the expect script above using the right parameters.<\/p>\n<pre class=\"post-pre\"><code>scdev@Pankajs-MacBook-Pro:~$\/Users\/scdev\/scripts\/sshloginsudo.expect 'scdev.com' 'scdev' 'ssh_pwd' 'su_user' 'su_pwd'\r\nspawn ssh scdev@scdev.com\r\nscdev@scdev.com's password: \r\nLast login: Sun Jun  9 19:54:17 2013 from c-67-161-57-160.hsd1.ca.comcast.net\r\nscdev@journal [~]# su - su_user\r\nPassword: \r\nsu_user@journal [~]# ls -ltr\r\ntotal 708\r\n...\r\n<\/code><\/pre>\n<h3>Additional suggestions for enhancing your experience with the expect script.<\/h3>\n<ol>You can reuse the expect script by passing all the information as arguments. To save time typing all the parameters, it is recommended to create aliases for each one of them for quick login. For instance, you could create an alias like this: alias journal=&#8221;\/Users\/scdev\/scripts\/sshloginsudo.expect &#8216;scdev.com&#8217; &#8216;scdev&#8217; &#8216;ssh_pwd&#8217; &#8216;su_user&#8217; &#8216;su_pwd'&#8221;. When passing arguments, it&#8217;s advisable to use single quotes, especially for passwords that may contain special characters, as not quoting them can lead to unexpected results.<\/ol>\n<p>Please be aware that the script mentioned above has been tested on both Mac OS and Linux operating systems. For more information, you can refer to the <a href=\"https:\/\/sourceforge.net\/\">SourceForge<\/a> Page.<\/p>\n<p>&nbsp;<\/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\/expect-the-easymock-void-method-to-have-its-last-call\/\" target=\"_blank\" rel=\"noopener\">EasyMock void method to have its last call.<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\/basics-plot-function-in-r\/\" target=\"_blank\" rel=\"noopener\">Basics of Graph Plotting &#8211; Comprehending the plot() Function in R<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-possible-paraphrase-could-beexample-in-java-using-jsch-library-to-execute-shell-commands-on-a-unix-server-through-ssh\/\" target=\"_blank\" rel=\"noopener\">JSch library to execute shell commands on a Unix<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Expect script is a valuable utility for Linux\/Unix systems. In my daily life, whether it is at work or on my hosting server, I frequently interact with numerous Unix servers. Due to the large number of servers involved, it becomes challenging to remember all the SSH user credentials along with the SU user credentials. [&hellip;]<\/p>\n","protected":false},"author":12,"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-965","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>SSH Example Tutorial for Expect Script - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"I have developed a script named sshsudologin.expect that enables automatic SSH server login, authentication as a super user, and execution of\" \/>\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\/ssh-example-tutorial-for-expect-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SSH Example Tutorial for Expect Script\" \/>\n<meta property=\"og:description\" content=\"I have developed a script named sshsudologin.expect that enables automatic SSH server login, authentication as a super user, and execution of\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/\" \/>\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=\"2022-08-29T15:35:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-15T12:48:42+00:00\" \/>\n<meta name=\"author\" content=\"Liam\" \/>\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=\"Liam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/\"},\"author\":{\"name\":\"Liam\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\"},\"headline\":\"SSH Example Tutorial for Expect Script\",\"datePublished\":\"2022-08-29T15:35:12+00:00\",\"dateModified\":\"2024-03-15T12:48:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/\"},\"wordCount\":570,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/\",\"name\":\"SSH Example Tutorial for Expect Script - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2022-08-29T15:35:12+00:00\",\"dateModified\":\"2024-03-15T12:48:42+00:00\",\"description\":\"I have developed a script named sshsudologin.expect that enables automatic SSH server login, authentication as a super user, and execution of\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SSH Example Tutorial for Expect Script\"}]},{\"@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\/23786905eb7b377f45ddb01c17da7671\",\"name\":\"Liam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"caption\":\"Liam\"},\"sameAs\":[\"http:\/\/Wilson\"],\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"SSH Example Tutorial for Expect Script - Blog - Silicon Cloud","description":"I have developed a script named sshsudologin.expect that enables automatic SSH server login, authentication as a super user, and execution of","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\/ssh-example-tutorial-for-expect-script\/","og_locale":"en_US","og_type":"article","og_title":"SSH Example Tutorial for Expect Script","og_description":"I have developed a script named sshsudologin.expect that enables automatic SSH server login, authentication as a super user, and execution of","og_url":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2022-08-29T15:35:12+00:00","article_modified_time":"2024-03-15T12:48:42+00:00","author":"Liam","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Liam","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/"},"author":{"name":"Liam","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671"},"headline":"SSH Example Tutorial for Expect Script","datePublished":"2022-08-29T15:35:12+00:00","dateModified":"2024-03-15T12:48:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/"},"wordCount":570,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/","url":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/","name":"SSH Example Tutorial for Expect Script - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2022-08-29T15:35:12+00:00","dateModified":"2024-03-15T12:48:42+00:00","description":"I have developed a script named sshsudologin.expect that enables automatic SSH server login, authentication as a super user, and execution of","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/ssh-example-tutorial-for-expect-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SSH Example Tutorial for Expect Script"}]},{"@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\/23786905eb7b377f45ddb01c17da7671","name":"Liam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","caption":"Liam"},"sameAs":["http:\/\/Wilson"],"url":"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/965","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\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=965"}],"version-history":[{"count":0,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/965\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}