{"id":257623,"date":"2021-05-24T19:00:27","date_gmt":"2021-05-24T16:00:27","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/"},"modified":"2021-05-24T19:00:27","modified_gmt":"2021-05-24T16:00:27","slug":"automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/","title":{"rendered":"#Automate Inputs to Linux Scripts With the expect Command \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2d7e42b29f3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2d7e42b29f3\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/#Automation_Means_Efficiency\" >Automation Means Efficiency<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/#Installing_the_expect_Command\" >Installing the expect Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/#How_expect_Works\" >How expect Works<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/#Thatll_get_Fiddly_Fast\" >That\u2019ll get Fiddly Fast<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/#Using_autoexpect\" >Using autoexpect<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/#Hang_On_My_Password\" >Hang On, My Password!<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-the-expect-command-cloudsavvy-it\/#You_Can_expect_Great_Things\" >You Can expect Great Things<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Automate Inputs to Linux <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a>s With the expect Command \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-4038\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/add8ac45.png?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Bash Shell\" width=\"1400\" height=\"600\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The Linux expect command lets you automate interactions with scripts and programs. You can send any kind of response to the script when it is waiting for some text input.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Automation_Means_Efficiency\"><\/span>Automation Means Efficiency<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When you administer a Linux computer or group of computers you\u2019ll run up against many repetitive tasks. The obvious answer is to write a script to perform the bulk of the work for you. The Bash shell, like all modern shells, provides a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.gnu.org\/software\/bash\/\">rich and flexible scripting language<\/a>.<\/p>\n<p>Scripting a long-winded or tedious job gives you the flexibility to run that task when the office is closed, or in the quietest periods of a 24\/7 operation. Scripts also give you repeatability. They won\u2019t forget to perform a step, no matter how many times they are asked to perform the same boring chores.<\/p>\n<p>That\u2019s fine, as far as it goes. But if your script requires user interaction, or if it calls a program that will need human input, what can you do? You don\u2019t want to have to be present when the script runs, or if you are present, to be watching the terminal window ready to jump in and hit a few keys.<\/p>\n<p>Linux has the <code>yes<\/code> command, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/yes.1.html\">which sends a stream of \u201cy\u201d characters<\/a>\u00a0or any other user-specified string to the terminal window. If a program is waiting for a response to a yes or no question it\u2019ll be force-fed one of the \u201cy\u201d characters. It\u2019ll accept it as input and will be able to proceed. You need to pipe the output from\u00a0<code>yes<\/code> into the script.<\/p>\n<pre>yes | script.sh<\/pre>\n<p>You might not want to send an affirmative response. Perhaps you need to say \u201cno.\u201d You can do that too, using the <code>yes<\/code> command. You can send any phrase you like, you\u2019re not restricted to responses like \u201cy\u201d, \u201cY\u201d, \u201cYes\u201d, \u201cn\u201d, \u201cN\u201d, or \u201cNo.\u201d<\/p>\n<p>Perhaps you need to send an \u201cr\u201d for reinstall to an \u201c<code>install\/upgrade\/reinstall [i\/u\/r]<\/code> \u201d prompt.<\/p>\n<pre>yes r<\/pre>\n<p>The <code>yes<\/code> command can\u2019t cope if it needs to provide more than one type of response. For that situation, we need to use <code>expect<\/code> .<\/p>\n<p><strong>RELATED:<\/strong> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/415535\/how-to-use-the-yes-command-on-linux\/\"><strong><em>How to Use the yes Command on Linux<\/em><\/strong><\/a><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Installing_the_expect_Command\"><\/span>Installing the expect Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We tested <code>expect<\/code> on Ubuntu, Fedora, and Manjaro. The package wasn\u2019t bundled with these distributions, so it had to be installed. On Ubuntu type:<\/p>\n<pre>sudo apt install expect<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11021\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/4a47a0db.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>On Fedora, the command you need is:<\/p>\n<pre>sudo dnf install expect<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11022\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/fb5c81ed.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>On Manjaro we use <code>pacman<\/code>:<\/p>\n<pre>sudo pacman -Sy expect<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11023\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/10fb15c7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_expect_Works\"><\/span>How expect Works<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>expect<\/code> command lets you manage the two ends of the conversation between your set of prepared answers and the program or script that you\u2019ll be sending them to. You do this by creating an \u201cexpect\u201d script that watches for prompts in the main script and sends the appropriate response for each one.<\/p>\n<p>Let\u2019s say you have a backup script that asks for a source directory name and a target directory name. It then makes a copy of the source directory files in the target directory. Without the bits that do the file copying, your script might look something like this:<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\necho \"Directory to backup?\"&#13;\nread source_directory&#13;\n&#13;\necho \"Backup location?\"&#13;\nread target_directory&#13;\n&#13;\necho&#13;\necho \"Target directory:\" $target_directory<\/pre>\n<p>All this script does is ask for the paths to the two directories. It prints the target directory to the terminal window so that we can see that it received a response from <code>expect<\/code> and that it could read it correctly.<\/p>\n<p>To provide the other half of the conversation we create an \u201cexpect script.\u201d By convention, these have a \u201c.exp\u201d extension. This example will work with our \u201cbackup.sh\u201d script.<\/p>\n<pre>#!\/usr\/bin\/expect -f&#13;\n&#13;\nset timeout -1&#13;\n&#13;\nspawn .\/backup.sh&#13;\n&#13;\nexpect \"Directory to backup?r\"&#13;\nsend -- \"\/home\/dave\/Documents\/r\"&#13;\n&#13;\nexpect \"Backup location?r\"&#13;\nsend -- \"\/<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>\/dave\/external\/backupr\"&#13;\n&#13;\nexpect eof<\/pre>\n<p>This shows the main three commands in expect scripts, the <code>spawn<\/code>, <code>expect<\/code>, and <code>send<\/code> commands.<\/p>\n<p>The first thing to notice <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Shebang_(Unix)\">is the shebang<\/a> is referring to <code>expect<\/code>, not to <code>bash<\/code>. The <code>-f<\/code> (file) flag tells <code>expect<\/code> the responses are coming from a file.<\/p>\n<p>We effectively turn off timeouts by setting them to infinity with -1.<\/p>\n<p>The <code>spawn<\/code> command launches the backup script.<\/p>\n<p>We know the two questions the \u201cbackup.sh\u201d script will ask us.\u00a0For each question we create an \u201cexpect\u201d line. These contain the text prompt that will be sent to the terminal window by the \u201cbackup.sh\u201d script. This what the <code>expect<\/code> program will watch out for. When <code>expect<\/code> sees the prompt, the text in the <code>send<\/code> line is returned to the \u201cbackup.sh\u201d script.<\/p>\n<p>The <code>expect eof<\/code>\u00a0lines tells <code>expect<\/code> to wait for the final end-of-file at the completion of processing in the automated script.<\/p>\n<p>Make both scripts executable:<\/p>\n<pre>chmod +x backup.sh<\/pre>\n<pre>chmod +x backup.exp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11087\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/09dd8c26-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>And use the \u201cbackup.exp\u201d script to fire off the whole process.<\/p>\n<pre>.\/backup.exp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11029\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/8266e4bf.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"210\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can see the two prompts from \u201cbackup.sh\u201d and the responses from \u201cbackup.exp.\u201d<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Thatll_get_Fiddly_Fast\"><\/span>That\u2019ll get Fiddly Fast<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>It\u2019s great to see two scripts interacting like this, with the automated responses being sent by our expect script and being accepted as genuine input by the automated script. This is just a simple example, of course. If you try to automate a long-winded process\u2014and they\u2019re the ones you\u2019ll want to automate\u2014you can soon find yourself bogged down.<\/p>\n<p>The text in the <code>expect<\/code> lines must match exactly the prompts from the script you\u2019re automating. Misspellings and incorrect wording will cause a prompt to be skipped, and the automated process will stall. If you\u2019re still developing or tweaking the script you\u2019re trying to automate, the wording of prompts and their order within the script are likely to change.\u00a0 Upgrades to scripts or programs might change, remove, or introduce prompts. Keeping track of the changes and replicating them in your expect script becomes tiresome and error-prone.<\/p>\n<p>The most convenient way to create an expect script is to use <code>autoexpect<\/code>. This is installed along with <code>expect<\/code>. We can tell <code>autoexpect<\/code> to monitor our real-life interaction with a script or program, and to generate an expect file for us.<\/p>\n<p>There might be some tidying up to do in the generated expect file, but it\u2019s much faster than writing one by hand. For example, if you mistype something in your session when autoexpect is monitoring you, the incorrect keystrokes and your editing and backspacing to correct them will all be captured. To tidy that up, simply delete that section and type the correct text.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_autoexpect\"><\/span>Using autoexpect<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Using <code>autoexpect<\/code> is easy. We use the <code>-f<\/code> (filename) flag to tell it the name of the expect file it should create, and the program or script that we want it to run. The prompts from the process and our responses are recorded and sent to the expect file. As a nice touch, <code>autoexpect<\/code> makes the expect file executable for us.<\/p>\n<p>Let\u2019s say we frequently use <code>rsync<\/code> to send files from one computer to\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/rsync.1.html\">a directory on another computer<\/a>\u00a0over the network. We\u2019ll use <code>autoexpect<\/code> to create an expect file of that process.<\/p>\n<p>Our command line looks like this. The first command is <code>autoexpect<\/code>\u00a0 followed by the <code>-f<\/code> flag and the name of the expect file we want to create. In this case, it is \u201csend-pics.exp.\u201d<\/p>\n<p>The rest of the command is the regular <code>rsync<\/code> command.\u00a0Because <code>rsync<\/code> uses the <code>SSH<\/code> protocol to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Secure_Shell_Protocol\">remotely connect to the target computer<\/a>, we\u2019ll need to provide the password for\u00a0user account \u201cdave\u201d on the target computer.<\/p>\n<pre>autoexpect -f send-pics.exp rsync -rasv ~\/Pictures\/raw\/ dave@nostromo.local:\/home\/dave\/raw\/<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11047\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/f19c9085.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>When we execute that command <code>rsync<\/code> is launched and we\u2019re prompted for the password for user dave\u2019s account on the remote computer. Once that\u2019s entered, some files are sent to the remote computer, <code>rsync<\/code> breaks the connection, and the process ends.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11048\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/9eb9cd58.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"295\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We\u2019re told that the file \u201csend-pics.exp\u201d has been created. Let\u2019s take a look:<\/p>\n<pre>ls -l send-pics.exp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11049\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/602e8f04-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>It has indeed been created, and it is executable. We can run it by calling it by name:<\/p>\n<pre>.\/send-pics.exp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11051\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/7afbb160.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"645\" height=\"280\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This time the process runs without any human interaction. Any new files since the last transfer are sent to the remote machine.<\/p>\n<p>This is a very small example script. It hardly saves any effort compared to running the <code>rsync<\/code> command by hand. While that\u2019s true, it does illustrate the point that you can control programs as well as scripts, and you can apply the principles seen here to scripts or processes of any length.<\/p>\n<p><strong>RELATED:<\/strong> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/427480\/how-to-back-up-your-linux-system\/\"><strong><em>How to Back Up Your Linux System<\/em><\/strong><\/a><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Hang_On_My_Password\"><\/span>Hang On, My Password!<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You need to be aware that any passwords\u2014or any other sensitive information\u2014gathered during an <code>autoexpect<\/code> session are stored in plain text in the generated expect script. Passwords should never be written in plain text anywhere. For SSH connections a\u00a0 better solution is to use SSH keys and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/424510\/how-to-create-and-install-ssh-keys-from-the-linux-shell\/\">have a passwordless authentication scheme<\/a>.<\/p>\n<p>Obviously, that\u2019s not going to help if you\u2019re not using SSH.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11053\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/586e508f.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Password stored in plain text in expect script, in the gedit editor\" width=\"644\" height=\"150\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can edit your expect script and change the line that deals with your password to:<\/p>\n<pre>interact ++ return<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11056\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/59b2900a-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Interactive command in expect script, in the gedit editor\" width=\"644\" height=\"160\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>When the script reaches this line it will await your input, allowing you to enter your password. Control then returns to the expect script once you\u2019ve provided your password.<\/p>\n<pre>.\/send-pics.exp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11058\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/9eb60bc8.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"110\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>But that presents a different problem. It means your script is no longer able to run unattended. If you\u2019ll always be hand-launching your script that probably doesn\u2019t matter, especially if the password request happens right at the start of the script. You can launch the script, enter your password, and then let it look after itself.<\/p>\n<p>Another way to address the issue is to:<\/p>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/438435\/how-to-use-the-chown-command-on-linux\/\">Change the file permissions<\/a>\u00a0on\u00a0your expect script using <code>chown<\/code> to 740, <code>-rwxr-x---<\/code>.<\/li>\n<li>Use <code>chmod<\/code> to change the group owner of the expect script to a trusted group.<\/li>\n<li>Create a regular script that launches your expect script. Set this file to be owned by the trusted group too. Set the permissions on this to 2751, <code>-rwxr-s--x<\/code>.<\/li>\n<li>This creates a launcher script that anyone can read and execute, that is in the same trusted group as the expect script, and with its\u00a0<code>setgid<\/code> bit set. Regardless of who runs this script, its effective group will be the trusted group and not the group of the person running the script. So this script will always be able to launch the expect script.<\/li>\n<li>Meanwhile, the expect script containing the password is inaccessible to anyone but you.<\/li>\n<\/ul>\n<p><strong>RELATED:<\/strong> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/656646\/how-to-use-suid-sgid-and-sticky-bits-on-linux\/\"><strong><em>How to Use SUID, SGID, and Sticky Bits on Linux<\/em><\/strong><\/a><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"You_Can_expect_Great_Things\"><\/span>You Can expect Great Things<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There\u2019s enough here to get you going, but there\u2019s a lot more to <code>expect<\/code> than the areas we\u2019ve covered. The <code>expect<\/code> man page is over 1700 lines!<\/p>\n<p>For tasks like automated installs, scheduled backups, or repetitive deployments, expect will transform your workflow.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/11017\/automate-inputs-to-linux-scripts-with-the-expect-command\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Automate Inputs to Linux Scripts With the expect Command \u2013 CloudSavvy IT&#8221; The Linux expect command lets you automate interactions with scripts and programs. You can send any kind of response to the script when it is waiting for some text input. Automation Means Efficiency When you administer a Linux computer or group of computers&#8230;<\/p>\n","protected":false},"author":1,"featured_media":257624,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/add8ac45.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-257623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/257623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=257623"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/257623\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/257624"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=257623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=257623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=257623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}