 Hey everyone, welcome back to another Python programming tutorial video in the last couple of videos and tutorials We've been looking at this PX SSH module and that was really cool because it allowed us to Connect to an SSH server or any any other computer online on the internet that is running at SSH So we were able to create this little object that allows us to kind of manipulate our SSH utility It's kind of like a controller or a manager for us And then we were able to log in to a specific server or anywhere else on the web We've been using over-the-wires bandit accounts for their bandit Wargame and the bandit user. It has a bandit zero username and the bandit zero password So we were able to log in just fine And we've been testing out different options and different properties and attributes that this s object has But this time we actually want to look at the cool stuff that this does We actually want to be able to send commands to SSH and see their output and see what we're actually doing on the server So this is pretty simple, right? All it takes is a send command the send function and you can see the description here send s This function will take one argument s which is the string that it gives the child process It will return the number of bytes that are written to it And if a log file is specified within the s object or a manager a copy is written to that log So let's actually just give it a command Let's say echo Subscribe You know nice little thing there Okay, cool. So we sent 17 bytes to the server and we should be good to go We just entered that command now. We want to actually get to it and see where it is remember that our s object is kind of like an Invisible pointer to segments of the text that we're seeing on our SSH Connection so what we can do here is we can actually get over to where we have Output you remember probably in the API overview that we looked at way way back in the first video that this is actually Expecting different things and different outputs from the program that we're running That's why the module is called P.expect or Pexpect because we expect different things we can find them with regular expressions and Strings and that sort of thing Now that's kind of hard to do sometimes right Because if you're connecting to a server you've never been on before you might not know What to expect like what output will you get back? Of course? I know that yeah, we'll get the subscribe output back since we're calling echo And I know how that works, but this is for learning purposes and for educational tutorial stuff in the real world It might not be that easy So interestingly enough and kind of fortunately because we're working with SSH We know that we're always going to see a prompt right after we find But after we enter a command like every time you're in a terminal and you enter help echo Hello or something the same prompt you had before is going to be returned back at back at you After you see the output of the command So if we run s dot prompt, this is a function here that matches the shell prompt It's interesting because it's a little it's a little bit of a shortcut in the expect method that we I was just talking about and you saw online What it does is it will return true if the shell prompt was matched like if we were able to find it And it will return false if a timeout was raised now if a timeout's raised then Well, I don't know we're probably screwed we got an exception if we didn't if we didn't do any exception handling It'll throw up when we get an error. So anyway, let's just try it and run it You'll notice the argument is optional. So we can just go ahead and run this so it uh It should return either either true or false If it finds the prompt right Uh Why isn't that happening? Okay, so I'm gonna restart the shell control f6 is what I did to do that And I want to talk a little bit more as to why that happened what went wrong there Well, I'll do that as I type through all this stuff. Hopefully I don't screw up But notice that what we entered in the send command that was actually of course the command that we wanted to run But kind of consider it as what you typed right from the keyboard, right? You're entering this command you're typing it in and that's literally what you see on the screen That's the command that you want to enter you have to enter that command Why am I saying that so much? Well notice that we didn't enter the command you didn't press to enter key you didn't get the new line So when we actually want to send something to the server when we want to send a command We have to include that new line at the very very end That's what actually tells us. Oh, we ran the command now the prompt will come back up So if I type in s.prompt now Hey, we get true. We actually found the prompt So now we want to be able to find the output like what did we see what what more can we find in the actual program right In ssh and its output So this is done by some interesting methods or not not even methods but variables that allow us to kind of find where we are So i'll actually check this out in the documentation with help for the object So i'm gonna pause it and find it right now Okay, cool. Here we go. I think this is under the Expect functions documentation. Yeah, here it is So the expect function is like I said what we were talking about beforehand that we saw on online And what we would normally use to find the output Once we've entered something to the server and actually to ssh So once it's found something once a match has been found It's going to create the instances and the attributes before and after and match Now you can read all this data in those variables before and after But you read the things that were set before you found the match, which is in this case are prompt And after with after of course matched is in match So prompt is kind of what we're looking for in this case So prompt is like it said in the function description kind of a shortcut for all this like uh Like it told us it's kind of a neat caveat to the expect function And it's a it's a more more of a shortcut to the expect method than anything else But let's check it out Let's try s dot before because obviously Anything before our current prompt will include The command output and the command itself when we actually send to the server. Let's check it out Hey, there we are if I print this out. So we actually interpret those new line characters It tells us echo subscribe, which is the command that we ran and then the output subscribe And of course the new line character and character turn feed So it's interesting right because We sent in a new line And we sent in We we received anyway a carriage return with it Why did that happen? So this is happening because of certain operating system specific things on the server And I'll I'll use this as a as kind of a segue and a transition again to talk about another function That is part of our s object and of course it's very similar to send But it's actually send line And it's kind of cool because here I'll I'll show you the description It wraps the send function Which of course sends the string s like we like the same thing that s that send function had done It sends it to our ssh child process, but it uses os dot line sep Automatically appended to it So it's pretty much giving our new line character right at the very end for us But it's specific to the operating system or the platform that the target server Or what you're connected to it as ssh is running There returns a number of bytes written just like our send function did but like I told you like I told you it's Passing in that little function for us So i'm sorry not little function our our our new line character. So if I echo please this time If I s dot prompt Get us there If I do s dot before Now you'll see echo please With the character return and the new line character and the character return and the new line character Now the character return is included even though Like we when we ran s dot send Echo hey new line s dot prompt s dot before the character return the character feed Is also part of it because that's how it works on that operating system. That's it's os dot line sep If I import os I can show you os dot line sep line separator and for us It's only a new line character, but for that server on bandit on over the wire It might be the character return and the new line. So that's an interesting thing to know and kind of denote there So here's some things that I want to talk about right It's interesting that you have to always type in prompt You always have to run that function and check out before To get the output of every command that you pass in it might be worthwhile to write a string that uh, sorry write a script The write a script that actually runs this function for you Like there's a function that all this is included in Once you just like you know, you could have like a send command function And then within that function it'll call Obviously send line or send whichever one you want to use and prompt and then read before and notice that the before prompt At least the before variable Always includes the command that you sent to it But it's of course going to be separated by our os dot line sep So maybe Maybe you could invoke python on the other on the like target machine that you're looking at Get the value of os dot line sep And then use that in your script part of the function that will determine. Okay, this first slice Of s dot before with the line sep as the delimiter. We know that's the command everything after it Well, we know that's going to be the output of the command so If you have something that of course that has multiple lines That way you'll actually retrieve all of it without being confused as to where is your command start Where does your command end? Where does the command output start? Where does the command output end? And that sort of thing so Food for thought it might be a good idea to write a script or a function that can Get the prompt function call done for you and look at s dot before And manipulate it so that you get the command output that you want to see But i'll leave that to you I wanted to show you how you can use this stuff with px ssh What kind of you need to know to be able to get it to work in a manageable way So hopefully you've been following along you can understand it and hopefully it wasn't too tedious kind of following me through this So, uh, this is all I really want to show you for this video. Thank you guys for sticking with me Thank you for watching. Um, if you like this video Well, hey, maybe like the video if you have any comments for me or constructive criticism I love constructive criticism. I need to know what what can I improve what can I fix? What more can I give you guys? Maybe leave a comment and uh, if you're feeling generous subscribe You know, I'd love that. Thanks so much guys. I'll see you in the next tutorial