Okay.
Welcome back.
In the previous lecture, we covered how to use the exit keyword to exit this program from running.
Okay.
Now again if I came back here if I do.
Clear and execute this program and then go back to Windows Machine.
And here again if I run this program, the reverse back to here.
If I use a simple command like this, it is showing all the details within a folder okay.
If I use exit command, you see it is exiting without any error.
And also if I go back here now you see that it is also exited.
Okay, so what if I use city command?
You know that city command is not going to work here.
We need to handle that separately here Into the listener and also the back door which is inside windows
machine.
So while we are here, okay, while we are sending this command, okay.
Right here and then we are sending here the first one, which is just exit command right now.
And in Windows Machine we are just receiving the first one, not the second one.
For example, if I use city to desktop so I will be able to only receive or get the city command, not
the second one which is the desktop.
And if I remove this from here, this zero.
So it is now this command will be a list and it will say that uh, command list do not have input uh
attribute to use.
So here we need to use another thing that is called JSON.
Okay.
For handling this we need to use JSON.
So let's import that JSON here.
Okay now I imported this.
And here okay when I receive this this I need to serialize this data.
Okay.
So after I serialize this data then I can send this data.
Not using this index okay.
That means I can send the whole list from here to the back door.
Okay, so how can I serialize this data?
If you try to.
If you want to know this, we have, uh, with, uh, JSON, we can use dump method that will handle
this for us.
Okay.
So let's use serialize.
Okay.
Serialize the data.
It is going to be equal to JSON dot dumps.
And right here we are going to give the command because that is what we receive from user.
Okay.
So let's give it.
Okay.
Now we give this command to this um dumps method okay.
And now.
Okay.
Or even I can do one more thing here.
I can put this right here.
And now I can use it again.
Now it looks better.
So.
Okay, now that I'm trying to send this data using send function, we also have another method called
send all okay with the socket.
Now instead of sending the command I can send the serialized data dot encode okay.
And now this serialized data had been sent by using send all method from here.
So let me save this and let's go back to Windows Machine here.
We need to handle that okay.
Let me zoom this a little more okay.
Here also we need to use two things.
One of them is the OS module.
The next one is um, uh, Jason again.
Okay, so for that, we need to import that.
So let's import JSON.
And also import.
OS.
OS will allow you to use a system command.
Okay.
With your code within your code.
Um, so we're trying to get that here.
Okay.
Actually, uh, we are receiving the command right here.
And we're storing the command into inside a variable called commands here.
Right now we are not going to receive that as byte.
Okay.
So I need to remove this.
And also let's create another way.
Another method here for handling this.
Okay.
So let's use div.
Div of I'm going to name this changing or change working directory.
To something.
So here we need to provide the path.
Okay.
So after that I am going to use this OS dot Chdir means change directory to what to path.
And at the end I need to return this path.
Okay, after it is changed I need to return it.
And here inside the run method I can use it okay.
Okay.
Perfect.
So we have the command here.
Okay.
That is something we are receiving.
So right now this is not a byte.
It is serialized data.
So when it is serialized data, we need to use again JSON.
Okay, so let me create a variable here called data.
Use JSON dot loads and give the commands to this.
And now instead of using command here I can use data.
And I can tell this that check for the first one.
If the first one is equal to exit then close the connection.
So this will check the first one and it will handle the exit method here.
And now the main purpose of this lecture is to handle the city command.
Okay so here let's use Elsif or Elif Elif data which is the first one.
Okay.
If this is equal to city that user enter and length of this data.
The first one okay is greater than one okay.
For example if it is two then I want this to be executed.
Okay.
All right.
Now let's hit enter.
Inside here I want to call that method that we just created called change working directory to something.
Okay.
So here let's again create another variable I am going to name this command result.
It is equal to self dot change working directory to something.
And that is data.
So before we had zero okay.
Because we need the we needed the first one now because we need the second one.
So I'm going to use index one.
And after I use this I need to encode this okay.
So when it is encoded now I can use it again.
So here we are going to use else.
And we put this into else okay.
Command result is equal to self dot execute command data.
So I am sure it is going to work.
Now let's save this.
Let's go back to play machine.
Let's go to cmd or terminal I am going to run the listener.
Go back here again.
Run this one.
Let's go back here okay.
Connection established.
Now I'm going to use a simple command like dir that is not working.
Perfect.
What if I use city?
It's not.
I'm sure it's not going to work.
So here, when we provide dir, it's returning non-zero status code one.
We are having an error here.
Let's see.
What is that okay.
So while we are sending this it is serialized data and it is encoded okay.
So.
I'm sure the problem is not right here because we are we may have some kind of error right in here that
we need to handle that.
Okay.
We have the task city.
Okay.
Command result.
We provide data here.
Command result.
System command.
We provide the command here, which is not allowed to be used here.
Okay.
Here we need to give data instead of commands because here we change the commands to data.
Okay.
Now let me save this again and let's go back to.
Scala.
Linux.
We execute the program.
And come to windows.
Run this again.
Come back here.
So the connection is established.
Now let's use dir which is a simple command is working.
Okay.
Now I'm going to use Siri to go back.
Okay.
One step back and hit enter.
So it is giving me this one.
That is because I use return here.
Okay.
I'm returning the path.
And after I return the path somewhere here.
Okay.
And then back.
I send this to, uh, the listener and in listener here, I print that.
Okay, I receive it.
And after I receive it, I print that here.
So because of that, it is printing that for me.
Now if again I use dir, you see that we change the directory to this one to this store before it was
Python project.
Now it is this one.
Let's use uh, I want to go back to for example exploit.
I use Siri and now I use dir.
You see, this time we are into exploit cheap and I can go to any of them.
For them.
For example, I want to go to document.
Okay.
So use d o c u m e n t s uh documents.
I think we did not move to writing here.
Let's use dir.
Okay.
We are into here.
Looks like because we have that use city hit enter.
So now you see we are into that document okay.
Let's see if the program is still working on windows Machine or it's closed.
See it is still working bad.
That that means it is working.
Okay, let's go back to here.
Okay.
I want to go back to desktop.
Desktop.
Hit It enter the air and I want to go to.
Looks like it is not working.
Disk up.
Okay.
It is working right now.
You see, it is giving me an error because I am into the desktop and I'm asking for desktop.
So because of that now the program is broken.
And also here the program is broken.
That means it is saying the system cannot find the file that you give that.
Is this because right now the location you are looking for is inside the desktop.
So that means it is working perfect.
Okay, let's try this once again.
Run the program.
Run this one.
Let me go back here I use d I r right now I am into Python project.
Okay.
I use city to come back here if I use dir.
Now you see I am into desktop instead of being in Python project.
Let's go back again here.
Now I am into exploit cheap instead of being right there.
So let me use city.
For example, I want to go back to Windows machine.
I don't know if in windows we use this kind of slash or this one.
I don't know exactly.
Let's try that.
This top hit.
Enter.
Dir.
And it's not working.
No problem.
But the program that we are trying to work trying to use now, it is completed and it is working okay.
We can change our directories that we want.
So thanks for watching and I will see you in the next lecture.