Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,270 --> 00:00:01,800
Okay.
2
00:00:01,839 --> 00:00:05,830
Now we know what a mac address is and how to change it.
3
00:00:06,520 --> 00:00:14,020
What we want to do now is write a Python program or script that will do this for us automatically.
4
00:00:14,350 --> 00:00:17,880
Now, I know you're thinking right now that you don't know anything about Python.
5
00:00:17,890 --> 00:00:20,590
How are you going to write a program that will do this?
6
00:00:20,740 --> 00:00:25,390
Now, like I said, learning programming can get boring if you don't have a purpose.
7
00:00:25,420 --> 00:00:28,530
That's why I want to teach this course by example.
8
00:00:28,540 --> 00:00:35,290
So we're going to start programming from the start, and as we program, I'm going to teach you programming
9
00:00:35,290 --> 00:00:38,230
concepts and how to program in general.
10
00:00:38,650 --> 00:00:43,930
So this is the first proper programming lecture that we're doing, but we're actually going to build
11
00:00:43,930 --> 00:00:47,440
a very simple script that will change the Mac address for us.
12
00:00:47,890 --> 00:00:54,320
Now, from what you know so far, all you have to do to change the Mac address is run these commands.
13
00:00:54,340 --> 00:01:00,130
So if you want to write a program that does this, the first thing that you need to do is be able to
14
00:01:00,130 --> 00:01:03,340
execute Linux commands through that program.
15
00:01:03,340 --> 00:01:09,130
We want our program to be able to execute terminal commands the same way that we are executing them
16
00:01:09,130 --> 00:01:11,560
here by typing the commands manually.
17
00:01:12,100 --> 00:01:17,560
Now, anything that you can think of, there is probably a way to do it in Python or there is probably
18
00:01:17,560 --> 00:01:19,900
a Python module that does that.
19
00:01:20,500 --> 00:01:27,370
So in order to execute system commands, we have a module called sub process in Python.
20
00:01:28,030 --> 00:01:31,170
Now sub process can execute any system commands.
21
00:01:31,180 --> 00:01:33,220
It doesn't have to be Linux commands.
22
00:01:33,250 --> 00:01:39,700
The difference is if you run the script from Windows, it will allow you to execute Windows commands.
23
00:01:39,700 --> 00:01:43,690
If you run the script from Mac, it will allow you to execute Mac commands.
24
00:01:43,690 --> 00:01:47,260
If you run it from Linux, it will allow you to run Linux commands.
25
00:01:47,290 --> 00:01:52,210
Now, since we're running our script from Linux, then we can run the commands that we learned in the
26
00:01:52,210 --> 00:01:57,340
previous lecture and be able to change our Mac address through a Python program.
27
00:01:58,090 --> 00:02:03,280
Now using the module is very simple and we're going to talk about that in a minute.
28
00:02:03,430 --> 00:02:07,990
But before I show you how to use it, I want to show you the module page.
29
00:02:08,169 --> 00:02:12,340
Now, for every Python module, there is a documentation.
30
00:02:12,340 --> 00:02:19,030
The documentation talks about what the module does, the functions included in this module, and how
31
00:02:19,030 --> 00:02:24,920
to use any of these functions and examples and even good practices and things to avoid.
32
00:02:24,940 --> 00:02:32,470
So it's a very good idea to learn how to go over the documentation and learn what modules do because
33
00:02:32,620 --> 00:02:37,360
there is no course online or anywhere that covers Python completely.
34
00:02:37,360 --> 00:02:39,850
There is no way someone can cover everything.
35
00:02:39,850 --> 00:02:47,560
So all we can do is cover Python for hacking, for example, or Python for data processing or even maybe
36
00:02:47,560 --> 00:02:48,940
a general Python course.
37
00:02:48,940 --> 00:02:50,950
But there is no way to cover everything.
38
00:02:50,950 --> 00:02:56,350
So you need to get into the habit of reading and experimenting and learning how modules work.
39
00:02:56,620 --> 00:03:02,260
Once you get comfortable with using a number of modules, using others is very easy and you'd be able
40
00:03:02,260 --> 00:03:06,580
to just read over the documentation and figure out how to use the modules.
41
00:03:06,850 --> 00:03:11,470
Now, we obviously don't expect you to know this right now, but by the end of the course it should
42
00:03:11,470 --> 00:03:12,880
be very easy for you.
43
00:03:13,210 --> 00:03:16,900
So the module that we want to use is called Sub Process.
44
00:03:17,050 --> 00:03:20,680
And like I said, this module allows us to run system commands.
45
00:03:21,340 --> 00:03:26,890
Now each module contains a number of functions that do something similar.
46
00:03:26,950 --> 00:03:33,190
For example, the sub process module, the one that we want to use, contains a number of functions,
47
00:03:33,190 --> 00:03:37,770
all of which allow us to run system commands but in different ways.
48
00:03:37,780 --> 00:03:43,120
For example, some of them will run commands in the background, some of them will run commands in the
49
00:03:43,120 --> 00:03:47,590
foreground and wait for the command to finish executing and so on.
50
00:03:48,310 --> 00:03:52,510
The function that we want to use today is called call.
51
00:03:52,690 --> 00:03:57,940
So the module name is sub process and the function is called call.
52
00:03:58,330 --> 00:04:04,330
This function allows us to run system commands, but it runs the command in the foreground, it doesn't
53
00:04:04,330 --> 00:04:09,790
run it in the background, it doesn't run it in a different thread, and it waits for the command to
54
00:04:09,790 --> 00:04:13,330
finish executing before it moves to the next line.
55
00:04:13,360 --> 00:04:19,720
This is very important because we don't want our script to exit or continue doing other stuff while
56
00:04:19,720 --> 00:04:21,910
it's still trying to change my Mac address.
57
00:04:21,910 --> 00:04:26,800
I wanted to change the Mac address, finish changing it, and then do other stuff.
58
00:04:26,800 --> 00:04:33,700
So that's why we're using the call, because it will not execute anything else until the command that
59
00:04:33,700 --> 00:04:35,890
I give it finishes executing.
60
00:04:36,640 --> 00:04:42,130
Now if we scroll down, you can see that there is two ways of using this function.
61
00:04:42,670 --> 00:04:47,680
We're going to use the second way in here and then we're going to go and use the first one in a different
62
00:04:47,680 --> 00:04:49,750
lecture and talk about the differences.
63
00:04:51,050 --> 00:04:54,980
So for the second weigh in here, I also have the example here.
64
00:04:55,010 --> 00:05:00,320
All you have to do is just type, sop, process, dot, call followed by the command that you want to
65
00:05:00,320 --> 00:05:03,320
run, followed by shell equals true.
66
00:05:03,740 --> 00:05:05,450
So it's very, very simple.
67
00:05:05,990 --> 00:05:12,320
Now let's have a very simple example of the sub process module before we actually use it to change the
68
00:05:12,320 --> 00:05:15,440
MAC address, just to see how the module works.
69
00:05:15,710 --> 00:05:25,700
So I'm just going to go to my machine and I have my pie chart in here and I have already created a project
70
00:05:25,700 --> 00:05:27,100
called Mac Changer.
71
00:05:27,110 --> 00:05:32,540
So I'm just going to create a new file and I'm creating a Python file.
72
00:05:34,870 --> 00:05:36,520
And I'm going to call it Mark.
73
00:05:36,520 --> 00:05:38,020
Change your dot pie.
74
00:05:41,820 --> 00:05:42,380
Okay.
75
00:05:42,390 --> 00:05:44,550
I'm going to add the shebang at the top.
76
00:05:47,400 --> 00:05:52,230
And then the first thing that I need to do is import the sub process module.
77
00:05:52,230 --> 00:05:55,380
So I'm going to have to say import sub process.
78
00:05:56,750 --> 00:06:03,740
So this literally tells Python that I want to use the sub process module, import all the functions
79
00:06:03,740 --> 00:06:05,780
that I can use with this module.
80
00:06:06,800 --> 00:06:11,540
Once I do that, I'm going to use the module by doing sub process.
81
00:06:12,630 --> 00:06:16,200
So this is the module name and then I'm going to put a dot.
82
00:06:16,260 --> 00:06:22,980
And you can see as soon as I put the dot pie chart automatically shows me all the functions that I can
83
00:06:22,980 --> 00:06:24,410
use with this module.
84
00:06:24,420 --> 00:06:28,140
So these are similar to the functions that you see in here.
85
00:06:28,800 --> 00:06:32,190
Now, like I said, the one that I want to use is called call.
86
00:06:32,850 --> 00:06:39,600
And again, as you can see, as soon as I type call, it shows me the arguments or the inputs that this
87
00:06:39,600 --> 00:06:40,800
function takes.
88
00:06:41,280 --> 00:06:47,130
So I'm going to hit enter to select this function and it will auto fill the brackets for me and it's
89
00:06:47,130 --> 00:06:50,700
going to give me a description on how to use this module.
90
00:06:51,210 --> 00:06:55,320
Now I know how to use it, so I'm just going to move my character to the middle.
91
00:06:56,680 --> 00:07:01,990
And in here we're going to put our command between two quotation marks.
92
00:07:02,560 --> 00:07:04,950
So this is a very simple example.
93
00:07:04,960 --> 00:07:08,020
So all we're going to do is just run if config.
94
00:07:09,250 --> 00:07:16,030
Now we know if config shows us the interfaces that are available to our current operating system.
95
00:07:16,870 --> 00:07:24,940
And finally, we're going to put a comma and add the shell argument and we're going to set that to true
96
00:07:24,940 --> 00:07:28,930
so that we can run shell commands through this function.
97
00:07:30,040 --> 00:07:31,000
And that's it.
98
00:07:31,000 --> 00:07:32,350
Our code is done.
99
00:07:32,350 --> 00:07:38,560
So as you can see, it's a very, very simple code and all it's going to do is it's going to import
100
00:07:38,560 --> 00:07:40,780
the sub process module first.
101
00:07:41,140 --> 00:07:46,000
Then we're using the sub process module and we're adding dot call.
102
00:07:46,000 --> 00:07:52,300
And this is the same way with all modules, you put the module name first followed by the function that
103
00:07:52,300 --> 00:07:55,090
you want to use that stored in this module.
104
00:07:55,360 --> 00:08:01,180
So we're using the call function and we're giving it the first argument as the command that we want
105
00:08:01,180 --> 00:08:02,170
to execute.
106
00:08:02,170 --> 00:08:05,860
And in our case right now, we run, we want to run if config.
107
00:08:06,130 --> 00:08:12,160
And secondly, we're giving it the second argument which sets the shell variable to true.
108
00:08:12,160 --> 00:08:15,310
So we can run Linux commands through this function.
109
00:08:15,850 --> 00:08:19,720
Now you can see that py charm is telling me that there is something wrong with this.
110
00:08:19,720 --> 00:08:24,940
And what's wrong in here is that shell should be a small s, not a capital S.
111
00:08:25,850 --> 00:08:28,760
And this is fine right now.
112
00:08:28,850 --> 00:08:34,340
Now, I'm going to save this by doing control s and then we're going to run it.
113
00:08:34,340 --> 00:08:39,140
You can run it from terminal or you can just go to run and run.
114
00:08:40,240 --> 00:08:43,510
And we're going to select this script, which is mark changer.
115
00:08:44,380 --> 00:08:51,760
And as you can see now, the script got executed in here and it just listed for me the result of IFF
116
00:08:51,760 --> 00:08:52,150
config.
117
00:08:52,150 --> 00:08:54,910
So you can see that it executed if config for me.
118
00:08:55,150 --> 00:09:01,750
And then we see all the interfaces that are available to the current operating system.
119
00:09:02,050 --> 00:09:04,060
So this is a very simple example.
120
00:09:04,060 --> 00:09:08,680
I just wanted to show you how to import a module and run a very simple system command.
121
00:09:08,770 --> 00:09:14,470
And then in the next lecture, this is going to make more sense because we're going to use it to write
122
00:09:14,470 --> 00:09:17,260
a script that will change the Mac address for us.
12835
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.