1 00:00:00,000 --> 00:00:01,000 [no audio] 2 00:00:01,000 --> 00:00:02,600 Friends here we are going to 3 00:00:02,600 --> 00:00:06,300 write a simple Python script with your 'subprocess' module. 4 00:00:06,600 --> 00:00:12,100 The requirement is, find bash version of Linux OS. See, if I 5 00:00:12,100 --> 00:00:16,800 take my Linux OS or terminal, you know, you have bash on 6 00:00:16,800 --> 00:00:19,900 your Unix-like systems and if you run 'bash --version' 7 00:00:19,900 --> 00:00:21,100 you are getting some output. 8 00:00:22,100 --> 00:00:26,500 But here what I need, I need this value, only 4.2.46. 9 00:00:26,600 --> 00:00:29,200 That is actually my version, my bash version. 10 00:00:29,200 --> 00:00:30,200 I need that output only. 11 00:00:30,200 --> 00:00:31,500 I don't want entire output. 12 00:00:32,900 --> 00:00:36,800 See before going to run your command with your Python, you 13 00:00:36,800 --> 00:00:38,900 have to think about your requirement. 14 00:00:39,300 --> 00:00:44,700 I mean whether you want to run simply your command, or you 15 00:00:44,700 --> 00:00:48,200 also need some modifications on your output. 16 00:00:49,100 --> 00:00:52,800 If you want to run simply your command, then just go and use 17 00:00:52,800 --> 00:00:54,800 'os.system()'. That is very simple one. 18 00:00:56,000 --> 00:00:59,700 But on the output if you need some modifications, as of now 19 00:00:59,700 --> 00:01:03,200 see that, we need to get this output, from this we want only 20 00:01:03,200 --> 00:01:05,099 this 4.2.46. 21 00:01:05,099 --> 00:01:07,400 That means we need some modifications on your output. 22 00:01:08,000 --> 00:01:11,200 That's why just go and run your command using 'subprocess' module. 23 00:01:12,000 --> 00:01:13,200 Right. See, now 24 00:01:13,200 --> 00:01:16,800 I am going to write a Python script just to get bash version, 25 00:01:16,900 --> 00:01:21,200 'bash_version.py'. Guys, I am going to 'import subprocess' module. 26 00:01:21,200 --> 00:01:24,000 [no audio] 27 00:01:24,000 --> 00:01:27,100 See, before going to write your Python scripts using 28 00:01:27,100 --> 00:01:28,800 'subprocess', right, 29 00:01:28,900 --> 00:01:32,000 you just keep in your hand this document. Already 30 00:01:32,000 --> 00:01:35,600 we have that from our previous video. For time being directly 31 00:01:35,600 --> 00:01:39,100 I'm copying this line, but you people don't copy and paste 32 00:01:39,100 --> 00:01:42,900 the code here. You just try to write, type your code 33 00:01:42,900 --> 00:01:47,200 so that you will clearly, it is easy to remember your code, right, 34 00:01:47,500 --> 00:01:49,700 your syntaxes for your 'subprocess'. 35 00:01:51,100 --> 00:01:54,400 Now actually 'bash --version' 36 00:01:54,400 --> 00:01:57,400 command is not depending on any environment variables, 37 00:01:57,400 --> 00:02:00,100 that's why I can take simply 'cmd = False', 38 00:02:00,800 --> 00:02:03,800 sorry, I mean 'shell = False'. If I take 'shell = False', 39 00:02:03,800 --> 00:02:06,500 you know, you have to take your command as a list. 40 00:02:07,600 --> 00:02:10,300 So 'bash --version', 41 00:02:10,300 --> 00:02:14,300 so how to get a command as a list means, once again I'm showing 42 00:02:14,300 --> 00:02:18,400 that. Just to go to your Python terminal, take your Windows command 43 00:02:18,500 --> 00:02:21,500 as it is, I mean windows or Unix-like systems, 44 00:02:21,500 --> 00:02:23,300 [no audio] 45 00:02:23,300 --> 00:02:25,300 then try to split this in this way, 46 00:02:25,900 --> 00:02:27,900 then you are getting some output. That is nothing but your 47 00:02:28,400 --> 00:02:31,600 list for 'shell = False'. 48 00:02:32,200 --> 00:02:34,700 That's what I am doing here directly because as of now 49 00:02:34,700 --> 00:02:37,800 I know the required thing, so that's why directly I am writing 50 00:02:38,200 --> 00:02:40,300 'bash --version' as a list. 51 00:02:41,500 --> 00:02:44,000 Fine. After running your command, right, 52 00:02:44,100 --> 00:02:46,500 it may take some time to execute your command. Anyway, 53 00:02:46,500 --> 00:02:49,500 'bash --version' is a very simple command. 54 00:02:49,500 --> 00:02:54,200 It won't take much time. You don't need to wait, but it is a 55 00:02:54,200 --> 00:02:55,900 better practice to use 56 00:02:57,200 --> 00:03:00,100 'sp.wait' because sometimes whenever you are going to 57 00:03:00,100 --> 00:03:03,200 use some SSH command it will take some time to execute your 58 00:03:03,200 --> 00:03:06,300 commands. That's why just try to make a habit that using 59 00:03:06,500 --> 00:03:10,300 'sp.wait' in your code. Then find out your output and 60 00:03:10,400 --> 00:03:13,700 error. So, you know how to get your output and error simply by 61 00:03:13,700 --> 00:03:15,700 communicating with your 62 00:03:15,700 --> 00:03:17,800 [no audio] 63 00:03:17,800 --> 00:03:23,000 'sp'. That's it. Now guys, see based on 'rc' code 64 00:03:23,000 --> 00:03:26,300 I can say that whether your command was success or failed, right? 65 00:03:26,500 --> 00:03:27,500 Let me do one thing. 66 00:03:27,500 --> 00:03:32,900 'If rc', if it is equal to 0, then I can say that, strongly 67 00:03:32,900 --> 00:03:33,900 I can say 68 00:03:34,800 --> 00:03:35,800 'print 69 00:03:35,800 --> 00:03:39,600 [no audio] 70 00:03:39,600 --> 00:03:41,800 ("out is :"). So what is the output? 71 00:03:42,900 --> 00:03:46,300 'o'. That is the variable we have taken. 'else' means, if 72 00:03:46,300 --> 00:03:48,800 'rc' code is not equal to 0 means there was a fail. 73 00:03:49,900 --> 00:03:51,400 So, "command was failed 74 00:03:51,400 --> 00:03:54,900 [no audio] 75 00:03:54,900 --> 00:03:55,900 with an error 76 00:03:55,900 --> 00:03:57,900 [no audio] 77 00:03:57,900 --> 00:03:59,100 and error is : ". 78 00:03:59,100 --> 00:04:01,400 [no audio] 79 00:04:01,400 --> 00:04:02,900 So error is 'e' we 80 00:04:02,900 --> 00:04:04,800 are taking. But you can take any variable, right? 81 00:04:04,800 --> 00:04:07,300 Let me save this, and run the code. 82 00:04:07,300 --> 00:04:11,400 [no audio] 83 00:04:11,400 --> 00:04:13,000 Let me run this with 'python3'. 84 00:04:13,000 --> 00:04:14,300 We need 'python3', right? 85 00:04:14,300 --> 00:04:16,800 [no audio] 86 00:04:16,899 --> 00:04:18,399 Right. You are getting your output. 87 00:04:18,300 --> 00:04:20,100 [no audio] 88 00:04:20,100 --> 00:04:23,800 Right. Now what I am doing is I am going to take a invalid command. 89 00:04:24,100 --> 00:04:28,500 Instead of your 'bash --version' if I run '- bash version', 90 00:04:28,900 --> 00:04:30,200 you're getting an invalid syntax. 91 00:04:30,200 --> 00:04:32,600 I mean command is not executing properly, right? 92 00:04:32,900 --> 00:04:34,600 That is actually not a valid command. 93 00:04:34,800 --> 00:04:38,200 So that's why what I am doing is, just I am taking your list 94 00:04:38,200 --> 00:04:43,400 bash, then instead of '--version', I am taking, you should 95 00:04:43,400 --> 00:04:46,800 not take, just to show that whether our code is working properly 96 00:04:46,800 --> 00:04:48,700 or not for that I am taking this one. 97 00:04:48,700 --> 00:04:51,500 [no audio] 98 00:04:51,500 --> 00:04:54,900 Let me show your code once, and then let me run your code, 99 00:04:56,300 --> 00:05:00,700 see, "Command was failed and error is : bash: version: 100 00:05:00,700 --> 00:05:01,800 No such file or directory." 101 00:05:01,900 --> 00:05:03,500 So something wrong with your command. 102 00:05:03,700 --> 00:05:05,200 So we know that what is wrong here. 103 00:05:05,300 --> 00:05:08,200 That's why be careful whenever if you are taking your commands. 104 00:05:08,200 --> 00:05:10,200 [no audio] 105 00:05:10,200 --> 00:05:14,100 So same command, you can also run by using 'shell = True', by 106 00:05:14,100 --> 00:05:17,600 simply taking your command as a string, but on Windows, 107 00:05:17,800 --> 00:05:20,400 please prefer 'shell = True' always. 108 00:05:21,000 --> 00:05:22,200 Okay. Fine. 109 00:05:22,700 --> 00:05:26,700 So guys, as of now you are getting your output with your Python, 110 00:05:26,700 --> 00:05:29,300 but requirement is, I don't want to get this entire output 111 00:05:29,300 --> 00:05:30,800 right? I need only this one. 112 00:05:32,200 --> 00:05:33,500 That's why what I'm doing is, 113 00:05:33,500 --> 00:05:36,800 first of all I'm going to convert your entire output into 114 00:05:37,600 --> 00:05:43,400 a list. You can convert your output, which is from your 'sp', right, 115 00:05:43,400 --> 00:05:44,700 'splitlines', 116 00:05:44,700 --> 00:05:47,900 [no audio] 117 00:05:47,900 --> 00:05:50,100 by using that you can get your output as a list. See that 118 00:05:50,100 --> 00:05:51,200 you are getting as a list. 119 00:05:51,200 --> 00:05:53,400 [no audio] 120 00:05:53,400 --> 00:05:56,500 Right. Now what I am doing is, I am going to print your line one 121 00:05:56,500 --> 00:06:02,700 by one. Just observe the logic. So I don't want to print in this way. 122 00:06:02,900 --> 00:06:06,400 What I am doing is I, am taking one by one value from 123 00:06:06,400 --> 00:06:10,600 your list which has been created from your output, simply 124 00:06:10,800 --> 00:06:16,300 'for each_line in o.splitlines()'. 125 00:06:16,700 --> 00:06:19,500 Simply I am printing 'each_line'. 126 00:06:21,200 --> 00:06:24,600 See that. See the logic, right. The same way you just practice 127 00:06:24,600 --> 00:06:27,600 it guys so that it will be easy for you to understand this code. 128 00:06:28,200 --> 00:06:30,100 Yes, again you are getting your entire output. 129 00:06:30,500 --> 00:06:34,000 But I want to print the lines which are having only 'version. 130 00:06:34,500 --> 00:06:35,500 Now, it's simple. 131 00:06:35,500 --> 00:06:40,500 [no audio] 132 00:06:40,500 --> 00:06:42,200 See that. What I am doing is, 133 00:06:43,800 --> 00:06:46,400 'if Version', in 134 00:06:46,400 --> 00:06:49,400 line, in your 'each_line' then only print that line, 135 00:06:50,500 --> 00:06:51,800 otherwise don't print that line. 136 00:06:51,800 --> 00:06:53,600 [no audio] 137 00:06:53,600 --> 00:06:56,000 Now, see the logic what you're going to get your output. 138 00:06:56,000 --> 00:07:03,300 [no audio] 139 00:07:03,300 --> 00:07:07,000 Yes, you are getting these two lines. 140 00:07:07,000 --> 00:07:10,500 [no audio] 141 00:07:10,500 --> 00:07:13,800 Right, but what you need? You need first line as of now, right, 142 00:07:13,900 --> 00:07:18,600 I mean version. So as of now, you don't know RegEx concept, 143 00:07:18,600 --> 00:07:20,800 what we will do is, yeah based on 'release' 144 00:07:20,800 --> 00:07:25,300 I will take. 'if version and release', both words are there in 145 00:07:25,300 --> 00:07:29,700 a given line then only I want to print that. See guys, I'm 146 00:07:29,700 --> 00:07:34,800 going to use now here your logical operator called 'and'. 147 00:07:35,900 --> 00:07:42,900 'if version in each_line and', then 'release'. 'version' as well 148 00:07:42,900 --> 00:07:45,800 as 'release' both I should have in my line, then only I want 149 00:07:45,800 --> 00:07:47,600 to print that line. Guys, 150 00:07:47,600 --> 00:07:49,500 you need to understand about logical operator 151 00:07:49,500 --> 00:07:52,900 'and'. When you have to take 'and' means, see my requirement 152 00:07:52,900 --> 00:07:54,500 is, this thing should be true, 153 00:07:54,600 --> 00:07:55,500 this thing should be true, 154 00:07:56,000 --> 00:07:59,000 whenever these two are true at that time only 155 00:07:59,000 --> 00:08:02,000 I want to execute my 'if' condition. So in that case you need 156 00:08:02,000 --> 00:08:04,800 to club your two conditions with the help of 'and' operator. 157 00:08:06,000 --> 00:08:09,100 So if both are true then only your final result will be true 158 00:08:09,100 --> 00:08:12,400 if you use 'and' operator. But if you take 'or' operator no, 159 00:08:12,500 --> 00:08:17,000 you are going to get again two lines. Just check it so that 160 00:08:17,000 --> 00:08:20,100 you can understand. Now, let me run it and see the output. 161 00:08:20,300 --> 00:08:22,800 Now you're getting only one line which is having 'version' 162 00:08:23,100 --> 00:08:26,900 as well as 'release', but I need this part, right? I need this part. 163 00:08:26,900 --> 00:08:31,400 So what I am doing is, guys, as of now you don't know about RegEx 164 00:08:31,400 --> 00:08:34,299 that's why I am doing so many steps. Later 165 00:08:34,299 --> 00:08:35,900 we will see again with RegEx. 166 00:08:36,000 --> 00:08:36,900 It's very simple. 167 00:08:38,500 --> 00:08:42,100 As of now don't concentrate on RegEx, just see the logic what I am doing. 168 00:08:42,100 --> 00:08:45,200 [no audio] 169 00:08:45,200 --> 00:08:51,400 Right. So now one, two, three, fourth part you are getting as, I mean 170 00:08:51,600 --> 00:08:55,900 index four is your required output as of now. Let me take 171 00:08:55,900 --> 00:08:59,900 this. From this I am taking index 3 value. 172 00:08:59,900 --> 00:09:03,800 [no audio] 173 00:09:03,800 --> 00:09:08,100 That's it. But I need only 4.2.46. Then again split this with the 174 00:09:08,100 --> 00:09:09,600 help of this parentheses. 175 00:09:10,200 --> 00:09:16,000 See that. What I am doing is, again I am splitting your output 176 00:09:17,400 --> 00:09:19,900 based on this parentheses symbol. 177 00:09:19,900 --> 00:09:22,200 [no audio] 178 00:09:22,200 --> 00:09:23,200 Where is it? 179 00:09:23,000 --> 00:09:26,700 [no audio] 180 00:09:26,700 --> 00:09:27,300 That's it. 181 00:09:28,400 --> 00:09:34,200 And then I am taking index 0. Just see first without this 182 00:09:34,200 --> 00:09:35,700 one what is the output you're getting. 183 00:09:36,800 --> 00:09:40,600 If I split that output with the help of parentheses what 184 00:09:40,600 --> 00:09:45,900 we are getting? You are getting a list which consists of 4.2.46. 185 00:09:47,900 --> 00:09:51,300 And then second value is something, but I need this value 186 00:09:51,300 --> 00:09:52,900 that's why just take index as 0. 187 00:09:52,900 --> 00:09:58,500 [no audio] 188 00:09:58,500 --> 00:10:06,000 Let me write that index 0. So this is the final requirement 189 00:10:06,000 --> 00:10:09,200 through which you are going to get your bash version 4.2.46. 190 00:10:10,700 --> 00:10:16,600 Right. So guys successfully we implemented a code called 191 00:10:16,900 --> 00:10:20,400 getting bash version using Python on your Unix-like systems. 192 00:10:21,600 --> 00:10:24,400 Right. So guys, this is not that much important. 193 00:10:24,400 --> 00:10:28,600 But the thing is how you are using your 'subprocess' for your 194 00:10:28,600 --> 00:10:32,100 requirement, that is important. Getting bash version is not 195 00:10:32,100 --> 00:10:37,700 the important thing, right. How you are implementing your Unix 196 00:10:37,700 --> 00:10:42,000 command to get your bash version using your Python, that is 197 00:10:42,000 --> 00:10:46,000 important. So that's why I have given this example, right. 198 00:10:46,300 --> 00:10:48,600 Now before going to next video, please. 199 00:10:48,600 --> 00:10:51,200 please try to find a java 200 00:10:51,400 --> 00:10:56,300 version using your Python. Yeah, for Java 201 00:10:56,300 --> 00:11:00,600 you need to take only one '-', right. If you run your code 202 00:11:00,600 --> 00:11:03,600 I need to get only 1.7.0_211. 203 00:11:04,400 --> 00:11:07,800 So before going to next video, please, please try to implement 204 00:11:07,800 --> 00:11:09,700 by yourselves this output. 205 00:11:09,700 --> 00:11:12,200 [no audio] 206 00:11:12,200 --> 00:11:15,700 I need to, if I run, if I provide this command for my 'subprocess', 207 00:11:16,200 --> 00:11:20,800 I mean for my Python script I need to get version as 1.7.0_211 208 00:11:21,900 --> 00:11:25,500 And just I am giving one clue, in case if you are not getting 209 00:11:25,500 --> 00:11:30,400 any output. Please see your error as well, see your output and error, and 210 00:11:30,400 --> 00:11:34,100 try to implement your logic to get your Java -version. So 211 00:11:34,100 --> 00:11:38,000 why I am giving this example is you have one important point 212 00:11:38,000 --> 00:11:40,600 with your Java - version command whenever if you are running 213 00:11:40,600 --> 00:11:41,500 with your Python. 214 00:11:42,800 --> 00:11:46,100 Right. So before coming to next video, please try to implement the 215 00:11:46,100 --> 00:11:50,600 code to get your Java version using your Python by executing 216 00:11:50,600 --> 00:11:54,300 your command with the help of 'subprocess' module, right? 217 00:11:55,700 --> 00:11:57,700 Okay guys, thank you for watching this video. 218 00:11:57,700 --> 00:12:04,672 [no audio]