-
-
Save s-kush/6504c0fadc3faecfe01e77a4795db622 to your computer and use it in GitHub Desktop.
Revisions
-
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -183,7 +183,7 @@ If-Statments are extremely simple, and an example should be clear enough to make ``` if { $a == "soup" } { puts "You have soup!" else { puts "No soup for you!" } -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -182,10 +182,10 @@ expect { If-Statments are extremely simple, and an example should be clear enough to make the point: ``` if { $a == "soup" } { puts "You have an apple!" else { puts "No soup for you!" } ``` -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,20 @@ The first step, similar to writing a bash script, is to tell the script what it' #!/usr/bin/expect ``` You also must place `interact` at the end, so your script looks like this: ``` #!/usr/bin/expect ... All your codez... interact ``` ### Puts & Output -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -156,8 +156,8 @@ expect { "> " { } "$ " { } } } default { send_user "Login failed\n" exit } -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -214,4 +214,6 @@ You should buy some apples, bananas, and cantalopes!" ### Conclusion The above describes the basics of expect scripting. It's really simple when you think about it in terms of taking actions you would perform by hand and converting them into a scripted set of actions. I decided not to give some big example but rather cover the core concepts. I did this because when I got started the examples I saw just threw me off and once I spent some time digging to understand the fundamentals I quickly found myself writing the scripts with ease. Hope this helps you do the same! -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 82 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -128,10 +128,90 @@ expect { In the above example we could encounter two results. The first is that I have connected to the server previously and am prompted with `Password:` or `Enter your password for server:` (or something similar). The looseness of the `assword` define covers both cases and submits the `$password` variable. The `"yes/no"` would be if you're prompted to accept the remote host's key - it will submit `yes` for you, then wait for the password-prompt and handle that for you as well. This can be adapted to different prompts as well - you could do something like the following to account for different styles of prompts: ``` expect { "> " { } "$ " { } } ``` This can be built-upon more to really close the error-gap: ``` expect { "> " { } "$ " { } "assword: " { send "$password\n" expect { "> " { } "$ " { } } } "(yes/no)? " { send "yes\n" expect { "> " { } "$ " { } } } default { send_user "Login failed\n" exit } } ``` ### Conditionals If-Statments are extremely simple, and an example should be clear enough to make the point: ``` if { $a == "apple" } { puts "You have an apple!" else { puts "No apple for you!" } ``` ### Looping There are several methods for looping. I tend to take the `while` approach when possible: ``` set count 10; while {$count > 0 } { puts "$count\n" set count [expr $count-1]; } ``` The above will simply loop backwards from `10` and echo-out the number. ### Functions & Proc What about code re-use? Oh yeah - expect has that: ``` proc do_something { a b c } { puts "You should buy some $a, $b, and $c!\n" } ``` Can then be called with: ``` set running [do_something "apples" "bananas" "cantalopes"] ``` Which would output: ``` You should buy some apples, bananas, and cantalopes!" ``` ### Conclusion The above describes the basics of expect scripting. It's really simple when you think about it in terms of taking actions you would perform by hand and converting them into a scripted set of actions. -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 29 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,30 @@ The first step, similar to writing a bash script, is to tell the script what it' You also must place `interact` at the end ### Puts & Output Instead of the `echo` command, expect uses puts, which is pretty 1:1... ``` puts "I am performing a command..." ``` Would do exactly what you think it would, just echo out the text. Now, the script will also show you the commands being performed. Good for testing, but maybe not required in "production" or daily-use. In that case you can just show the `puts` text via: ``` log_user 0 ``` Which suppresses the commands and responses, showing only what you output via `puts`. You can always turn it back on later via: ``` log_user 1 ``` If for example you wanted to show exactly what is coming back to the console. ### Variables @@ -105,5 +129,9 @@ expect { In the above example we could encounter two results. The first is that I have connected to the server previously and am prompted with `Password:` or `Enter your password for server:` (or something similar). The looseness of the `assword` define covers both cases and submits the `$password` variable. The `"yes/no"` would be if you're prompted to accept the remote host's key - it will submit `yes` for you, then wait for the password-prompt and handle that for you as well. # Conditionals If-Statments are extremely simple, and an example should be clear enough to make the point: ``` if ($a == "apple" ) -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 36 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -64,11 +64,46 @@ spawn sudo "apt-get install $app" expect "assword" send "$password\r" ``` The above would allow you to pass in your sudo password and the name of an application to install from apt, then wait for the password prompt and send it. No `assword` is not a mispelling, let's look at that `expect` statement... ### Expect The `expect` statement is where the magic happens. Let's start with the basic, say you run a command and just want to wait for the console to return to prompt before it moves on... ``` spawn apt-get "update" expect "$" # Move on to the next thing... ``` Very simple stuff, you spawn `apt-get` then just wait for the `$` (or prompt). The expect statement looks for a "close match" so in the above example your prompt (depending on your shell) could be something like `root@server $~`. The expect would find that `$` and know that it's good to move forward. So, how about more difficult cases, where you may not have a finite expect. Let's take a look at ssh again: ``` spawn ssh "$user/@server" expect { "assword" { send "$password\r" } "yes/no" { send "yes\r" } } # NOW we can move on... ``` In the above example we could encounter two results. The first is that I have connected to the server previously and am prompted with `Password:` or `Enter your password for server:` (or something similar). The looseness of the `assword` define covers both cases and submits the `$password` variable. The `"yes/no"` would be if you're prompted to accept the remote host's key - it will submit `yes` for you, then wait for the password-prompt and handle that for you as well. -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 38 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,7 @@ ### Intro TCL-Expect scripts are an amazingly easy way to script out laborious tasks in the shell when you need to be interactive with the console. Think of them as a "macro" or way to programmaticly step through a process you would run by hand. They are similar to shell scripts but utilize the `.tcl` extension and a different `#!` call. ### Setup Your Script The first step, similar to writing a bash script, is to tell the script what it's executing under. For `expect` we use the following: @@ -6,6 +10,9 @@ The first step, similar to writing a bash script, is to tell the script what it' #!/usr/bin/expect ``` You also must place `interact` at the end ### Variables Variables are very simple, just use `set {name} {value}`, for example: @@ -34,4 +41,34 @@ Then, when running the script I could supply the varaible content via: ./myscript.tcl myuser mypassword ``` ### Spawn & Send You can start a process with the `spawn` command. For example, `ssh` or `scp` are great examples: ``` set user "myuser" set server "myserver.com" spawn ssh "$user\@server" ``` The above would spawn the ssh process and submit the user and server, so essentially like entering `ssh myuser@myserver.com` in the console. The `send` command allows you to send something to the console. For example, a password: ``` set password [lindex $argv 0] set app [lindex $argv 1] spawn sudo "apt-get install $app" expect "assword" send $password ``` The above would allow you to pass in your sudo password and the name of an application to install from apt, then wait for the password prompt and send it. No `assword` is not a mispelling, let's look at that `expect` statement... ### Expect -
Kent Safranski revised this gist
Aug 21, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ The first step, similar to writing a bash script, is to tell the script what it' ``` #!/usr/bin/expect ``` ### Variables -
Kent Safranski created this gist
Aug 21, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ ### Setup Your Script The first step, similar to writing a bash script, is to tell the script what it's executing under. For `expect` we use the following: ``` #!/usr/bin/expect `` ### Variables Variables are very simple, just use `set {name} {value}`, for example: ``` set a "apple" set b "banana" set c "cantalope" ``` Referencing variables is done by simply appending `$` to the name - so `$a` would contain `apple`. ### Arguments If you want to set a variable by passing in an argument simply use the following: ``` set user [lindex $argv 0] set password [lindex $argv 1] ``` Then, when running the script I could supply the varaible content via: ``` ./myscript.tcl myuser mypassword ``` ###