Write Your Own Bash Scripts for Automation [Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
bash scripting is a convenient way to automate things on any Linux system and today we're going to use it to learn how to automate certain tasks that we use all the time on this episode of cyber weapons lab bash scripting is a simple language for stringing together various different powerful Linux utilities and it's this power and simplicity that lets beginners really use it to make a lot of things that would be pretty complicated and require some programming skill relatively simple now if you know the right bash commands you can do all sorts of things and today we're going to focus on a couple commonly use things and automate them with bash scripts we're also going to do a little bit of aliasing and this is just to make things more convenient so if we have something we want to do we're going to just use a alias to call a bash script and have it run all sorts of things for us instead of having to actually run and call the script every time from wherever we're saving it now in order to follow along you'll need either an Ubuntu or a Kali Linux system really any Linux system will do and that is all so if you want to pick up a Linux system and you've never tried this before you can always grab a Raspberry Pi which you can find in the link below once you have a Linux system then we're ready to begin today we're gonna get started learning about bash scripting and also a little bit about aliasing and what I mean by that is we're gonna be using bash scripting to basically duct-tape together a bunch of Linux applications and use aliasing to make it so it's possible to run them with a single command now to get started we can go ahead and run which bash and if we are running this in a terminal window we should see in my case bin bash this will tell us which bash interpreter we're using and it's also useful information for writing our first bash script so once we have the location of our bash interpreter we can go ahead and copy this and let's go ahead and make our first bash script so I'm gonna type LS you can see I actually already have a bash script here at the CAS H script but that's an advanced one I've been working on so let's go ahead and start a new one by typing Nano and we'll just type for now Bosch Sh and you can name this whatever you want but it does need to be named SH so we'll go ahead and then add the signatory character that says this is where we're going to be running an interpreter so in this case we're going to be doing the shebang and then bin slash bash and that basically lets this program know immediately when it's opened how to interpret the language we're going to be writing in okay so we have a very basic batch file it identifies itself as a batch file and then there's nothing so let's go ahead and actually create a variable and then have something happen so we can create variables similar to many programming languages let's say in this case I want to just make string we can have string equals null byte and then we can echo something back so let's say so hackers love to learn on and then we want to use the variable so in bash scripting we'll use a dollar sign and then the variable name now we'll go ahead and I thought we'll go ahead and just use a quote to close that out and this should be everything we need in order to create our first bash script so let's take a look at what we're doing we're just creating a variable saving the name null byte we're and then we're echoing this back so we're just saying go ahead and repeat this back to me but instead of repeating it back literally it's going to swap in the string we just created so let's press control X in Nano Y to confirm enter and now we'll go ahead and try to run this batch file so we'll type bash bash dot sh and let's see if it works hackers love to learn on null byte sweet okay so now that's a little bit of very very basic bash scripting we've been able to basically test whether or not we can create a variable so the result was we were able to get something back that wasn't originally what we passed so we were able to get this variable printed back to us okay so that's pretty cool but what else can we do here now let's go ahead and go back into the bash script so we'll Nano back into it and instead of assuming that there is a string that is existing already the program say that we want to pass variables into it so when we run this batch script we want to be able to supply a couple variables and have them be a part of our script so here we're gonna go ahead and basically fill in the blank and say I firmly believe that dollar sign one is the best dollar sign two for the office of dollar sign three and this is all within some quotes so what do these dollar signs mean well basically these dollar signs are popping a variable into place anytime that we add something to the end of the bash script when we run it so let me show you what that means so we put in a dollar sign here a dollar sign here and one here and these are actually positional so if I run this - SH and I put a word here a word here and then a word here you should see them just automatically appear when this is echoed back in this place this place in this place and you can swap them around however you want the dollar sign one just means it's the first position the dollar sign two means is the second and the dollar sign three means it's the third so let's see if this actually works we'll go ahead and press control X again wide save and we'll then try to run this but we will say let's say firm and supreme there we go as soon as I echo this back I've supplied three different variables and I can see now it says I firmly believe that Berman supreme is the best candidate for the office of President Wow our script has the preference I don't endorse that but that's a really interesting opinion so as you can see bash scripting is a really easy way to take in a couple things like variables and distribute them throughout your script so that you can adapt to something that might be maybe piped in from another script or otherwise selected by the user now the next thing we want to do is take advantage of the ability to actually create something inside the script that depends on another tool being run so let's go back into our script we'll nano into it and let's save in addition to actually let's replace this we want to print something that is the result of something being run so we want to echo let's say we're gonna say dollar sign and then if we put a pair of parentheses anything that happens in here will basically be popped out and run so I can even do something like ifconfig so what this should do is echo the result of the ifconfig command so I'm actually not gonna run that because then we'll have to blur it I'll just do the Who am I control X Y and let's go ahead and run this again and what we should see isn't literally just that thing we typed spelled out we should see the result of the command instead be printed so we'll do bash bash tot Sh and we can see it runs the Who am I command and just runs that instead so that's pretty cool what about interactivity well bash scripting has that down as well we can go back here and we can say instead we want to echo something first so we'll echo what is your name and then we're going to read name and this is a variable and then we're going to echo Wow dollar sign name sounds like punk I don't think I've made any mistakes we'll see in just a second save this and now what we've done here is add a little bit of interaction so let's say I don't I'm not gonna pass this bash script I want to make it more interactive for the person using it I can run the bash script and demands what is your name Cody Wow Cody sounds like a pun well I mean step off bash script so we've now created an argumentative bash script that can take in something and insult us but obviously this is far more applicable if we want to be putting in a variable like maybe where a particular interfaces or if we want to select which port our Arduino is connected to I've used bash scripting for flashing a whole bunch of different D authored boards all at the same time and the way that it works is by first selecting which port they're connected to so this is critical for creating your own bash scripting tools now finally we're gonna go into something a little bit more advanced which is if statements so we can use conditional statements to basically put controls in our program so we can see whether or not something exists or if it matches a certain value so let's say we want to change this a little bit and we want to see whether or not that person is actually putting in a name or if there's nothing there so this is a pretty common example we can just basically verify that something you know has been entered correctly in the script so if we type if and then we're going to use a bracket dollar sign name then another bracket semicolon then and then we will do something so let's say we echo name sounds so basically now if something exists in this variable then it's gonna echo that that person sounds okay to me and then we can also put in an else condition and this will basically be if there's nothing inside this variable doesn't sound like anything to me all right so this is the flow of our program we demand a name we read the name if they type something in the name then it says that that name sounds all right to me otherwise it's going to say doesn't sound like anything to me let's go ahead and test it ask the name looks like there's a problem line 9 unexpected end of file oh we forgot one important part so this is really good if you get this error then this is what you did so when you are doing loops they don't end the Matta CLE you have to actually end the manually so I forgot to type two letters just two letters B so I'm gonna go ahead and add fee press X Y to save it let's go ahead and run this again coding sounds all right to me now let's run it one more time this time I'm going to type nothing doesn't sound like anything to me now you can see here that the script is reacting to me I've been able to actually change it and I'll go back to the script so you can see it I've been actually to chain able to change it so that now depending the input it's given it is able to react to that and this is really important if you want to start using bash scripting to do things that are more advanced now I'm not gonna get too much more into this script or make it much longer instead I'm going to show you some command-line things that are also relevant to bash scripting and are super easy so let's say that we just want to learn something about this computer and we don't want to pipe through a whole bunch of different output if I were to just type ifconfig I would get a whole big long string of stuff that I'm not that interested in but if I actually identify something that is maybe more unique to a particular line I'm interested in let's say let's try something I if I want to narrow this down I can use the type symbol which is right here and send all of this input to a program that's going to filter through it called grep so I will type grep and then the pattern I'm looking for in this case I'll try broadcast if I press ENTER I can see that I've identified my IP address is basically in the string that uniquely has the word broadcast in it so here let's say that I really want to find my IP address in one command I basically want to create something that gives me my current IP address on the network in a single command rather than typing I have config and digging through this big long mess of stuff so I'm gonna clear this and if we take a look at the last command I can type my config and pipe it into grep broadcast and this is a little bit of bash scripting that then gives me almost what I want still a line not everything's relevant but the answer is in there so I'm just gonna expand on this and this is why bash scripting is kind of the duct tape of Linux we're gonna go ahead and add a wk which will allow us to select any of these words we're going to add a single quote put it inside a bracket a type print and then in this case that's the second word so dollar sign - and then another bracket - close and then a quote and let's go ahead and see if this will give us what we want there we go so what have we done here we've taken the output of one command and pass it through to filters in order to get the result that we want and now we're at the point where let's say we want to save this I don't want to have to type all this big long command every time that I want to see my IP address let's say that I really want to instead copy this and be able to use it whenever I want it in a single command well that is where we get into aliasing and this is the best part of developing a really good batch script I can go ahead and type alias what is my IP or IP address and then equals and I can go ahead and put the bash script in here so this is awesome and you do need to be careful of the quotes because double and single quotes can mess this up you can see I have double and single quotes kind of separated here so if I go ahead and press Enter I should be able to type IP address now as you can see here this failed initially but there is a way to be able to make this work so if I go back up to this command and instead I am going to changes slightly I'm gonna alias IP address or just what is my IP address a little lot longer but that's fine and now we're gonna change this just a bit so that we don't run into the same problem that we get when we just echo this directly so I'm going to echo dollar sign and then remember this is the way that we basically have all this run before we print it let's go ahead and add another parenthesis here and let's see if this now works so we'll now type what is my IP address and there we go it works a little bit of a struggle but as you can see it does matter to learn a little bit about the things we just did in this case knowing about the dollar sign here and then making this command run first before we print it is what got us around this issue which by the way frustrated me for a good hour the other day that came about when trying to automate the process of simply typing in a single word and getting back the result of your current IP address so this has been a brief bash scripting intro there's lots of stuff you can do with this but in general this is a great way to get started automating just about anything you want to do in Linux bash scripting is an incredibly useful skill to learn and in general you don't need to know a lot about programming to duct-tape together a lot of different Linux applications and make something that actually works in order to get a prototype up and running now if you get confused you can check out the null byte article link the description and if you want to check out more articles like this make sure to check out lots of different content on the null byte website as well that's all we have for this episode of cyberweapons lab make sure to LIKE comment and subscribe and if you have any ideas for upcoming episodes send me a message on Twitter because they'd love to hear from you we'll see you next time
Info
Channel: Null Byte
Views: 264,921
Rating: 4.9238009 out of 5
Keywords: wht, wonderhowto, nullbyte, null byte, hack, hacking, hacker, hacks, hackers, how to hack, howto, how to, tutorial, guide, cyber weapon, cyber weapons, cyber, cyber weapons lab, bash, bash scripting, bash scripting tutorial, bash scripting tutorial for beginners, bash scripting linux, bash scripting basics, shell scripting, bash scripting automation, software automation, kali linux, kali linux 2020, shell scripting basics, bash scripting projects, bash script, shell scripting tutorial
Id: PPQ8m8xQAs8
Channel Id: undefined
Length: 16min 35sec (995 seconds)
Published: Tue Feb 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.