1 00:00:00,000 --> 00:00:01,000 Friends here 2 00:00:01,500 --> 00:00:05,800 our concept is 'Working with remote Linux Server from Linux 3 00:00:05,800 --> 00:00:10,100 or Windows as your localhost by using paramiko of Python'. 4 00:00:11,400 --> 00:00:14,700 See you're having one remote server, and there is a Linux 5 00:00:14,700 --> 00:00:19,000 server, and you are having a local server and this local 6 00:00:19,000 --> 00:00:22,700 server maybe windows or maybe Linux. And from here 7 00:00:22,700 --> 00:00:27,000 I need to work with your remote server by using paramiko 8 00:00:27,000 --> 00:00:29,300 of Python. Right. 9 00:00:29,300 --> 00:00:30,700 This is our concept here. 10 00:00:30,900 --> 00:00:33,700 But before going to work with that, let me give some introduction 11 00:00:33,700 --> 00:00:35,000 to your Paramiko Module. 12 00:00:35,400 --> 00:00:39,300 See first of all Paramiko is your Python module or Library, 13 00:00:39,300 --> 00:00:42,700 which is useful to work with your remote servers. See working 14 00:00:42,700 --> 00:00:45,800 with your remote servers means sometimes you need to execute 15 00:00:45,800 --> 00:00:48,800 some commands on your remote server without logging into 16 00:00:48,800 --> 00:00:52,100 that from your local server, or sometimes you need to transfer a 17 00:00:52,100 --> 00:00:55,600 file from local server to remote server, or sometimes you 18 00:00:55,600 --> 00:00:58,600 need to download a file from remote server to local server. 19 00:00:59,100 --> 00:01:02,400 So all these actions we can perform with the help of Python 20 00:01:02,400 --> 00:01:07,400 Paramiko Module, but before going to use that you should 21 00:01:07,400 --> 00:01:13,100 have a SSH or SSH V2 protocol on your local server, or simply 22 00:01:13,100 --> 00:01:15,800 from where you are running your Python code 23 00:01:16,200 --> 00:01:19,300 there you should have, on that operating system 24 00:01:19,300 --> 00:01:22,100 you should have SSH V2 protocol. That's it. 25 00:01:23,500 --> 00:01:28,000 And guys we know that by default we have SSH protocol on 26 00:01:28,100 --> 00:01:29,600 your Unix like systems. 27 00:01:30,100 --> 00:01:32,700 If your local server is Windows you need to check for your 28 00:01:32,700 --> 00:01:35,100 SSH. If it is not there you have to install it. 29 00:01:36,000 --> 00:01:37,200 Right. That's fine. 30 00:01:37,700 --> 00:01:41,300 Then how it is going to work? How your Python Paramiko 31 00:01:41,300 --> 00:01:42,400 Module is going to work? 32 00:01:43,100 --> 00:01:46,500 See your Python Paramiko Module is going to create 33 00:01:46,500 --> 00:01:50,600 a SSH client first with the help of your SSH 2 protocol, 34 00:01:50,700 --> 00:01:55,100 I mean SSH V2 protocol. And by using that it is going to connect 35 00:01:55,100 --> 00:01:57,300 with your remote servers and it is going to work. 36 00:01:58,100 --> 00:02:00,900 Working means executing commands and transferring files. 37 00:02:01,800 --> 00:02:05,400 That's fine. Now for authentication purpose your paramiko 38 00:02:05,400 --> 00:02:10,699 is using two ways. One is using 'username' and 'password', and 39 00:02:10,699 --> 00:02:13,800 second one is using 'username' or your 'keys', 40 00:02:14,100 --> 00:02:15,800 I mean 'username' and your 'keys'. 41 00:02:15,800 --> 00:02:17,600 [no audio] 42 00:02:17,600 --> 00:02:21,400 And guys finally the last point for your introduction is 43 00:02:21,500 --> 00:02:25,400 we can connect from Linux to Linux, or Linux to Windows, or 44 00:02:25,400 --> 00:02:27,900 Windows to Linux, or Windows to Windows using your 45 00:02:27,900 --> 00:02:29,000 Paramiko Module. 46 00:02:29,300 --> 00:02:31,500 So we have four combinations, right? 47 00:02:32,200 --> 00:02:35,400 Let me write all combinations here. Suppose local, 48 00:02:36,300 --> 00:02:42,200 let me write here in this way. "Local server", then your 49 00:02:42,200 --> 00:02:43,300 "Remote server". 50 00:02:44,700 --> 00:02:49,500 So local server maybe Windows, and your remote server, 51 00:02:51,200 --> 00:02:54,000 let me give all the combinations - Windows servers, 52 00:02:54,300 --> 00:02:58,400 or local server is Windows and remote servers maybe Linux 53 00:02:58,400 --> 00:03:04,600 servers. Now local server, maybe Linux or Windows Server 54 00:03:04,600 --> 00:03:08,700 maybe Linux, or it is Linux and Windows, 55 00:03:08,700 --> 00:03:10,300 remote server is Windows. 56 00:03:10,300 --> 00:03:12,609 [no audio] 57 00:03:12,600 --> 00:03:16,600 So these four combination will works with your Paramiko 58 00:03:16,600 --> 00:03:20,900 Module, and here for time being we are going to work with 59 00:03:21,000 --> 00:03:25,000 by conserving your local server as Windows or Linux and your 60 00:03:25,000 --> 00:03:26,800 remote server is only Linux. 61 00:03:28,000 --> 00:03:30,900 Right. So first I am going to consider 62 00:03:32,300 --> 00:03:34,200 local server as Linux 63 00:03:34,200 --> 00:03:39,300 [no audio] 64 00:03:39,300 --> 00:03:41,300 and remote server also Linux. 65 00:03:41,500 --> 00:03:46,510 [no audio] 66 00:03:46,500 --> 00:03:49,400 Second we will work by considering local as Windows and remote 67 00:03:49,400 --> 00:03:51,200 server is Linux. That's fine. 68 00:03:52,500 --> 00:03:54,500 Now guys before going to use your paramiko 69 00:03:54,800 --> 00:03:58,300 the first thing is you should have a SSH on your localhost. 70 00:03:58,400 --> 00:04:00,100 Let me check. See suppose 71 00:04:00,700 --> 00:04:04,200 this is my Local server. 72 00:04:04,200 --> 00:04:06,810 [no audio] 73 00:04:06,800 --> 00:04:07,900 from my VMware. 74 00:04:13,100 --> 00:04:17,100 Let me remove this. Fine. Now see here. 75 00:04:17,100 --> 00:04:19,500 [no audio] 76 00:04:19,500 --> 00:04:21,800 Yes, I have SSH. Then no problem. 77 00:04:22,399 --> 00:04:25,399 We have already SSH, then we can work with your Python paramiko. 78 00:04:25,399 --> 00:04:29,700 And one more thing just check it whether we have paramiko or not. 79 00:04:30,400 --> 00:04:31,800 So here I have paramiko. 80 00:04:31,800 --> 00:04:34,100 Anyway, this is not the default module 81 00:04:34,400 --> 00:04:37,300 so if it is not there on your host, then you need to install. 82 00:04:37,500 --> 00:04:39,400 See let me show you on my Windows suppose 83 00:04:39,400 --> 00:04:42,400 see I tried to 'import paramiko'. Here 84 00:04:42,700 --> 00:04:47,200 there is no name called 'paramiko', I mean "No module named 85 00:04:47,200 --> 00:04:49,300 paramiko". Then that's why what I did here is 86 00:04:49,300 --> 00:04:52,500 simply I installed with the help of 'pip', 'pip install paramiko'. 87 00:04:52,500 --> 00:04:55,800 Same thing you can follow on your Linux system as well to install your 88 00:04:56,300 --> 00:04:57,700 paramiko. That's fine. 89 00:04:58,200 --> 00:05:01,700 So after installing I imported, then there is no error. That's fine. 90 00:05:02,500 --> 00:05:05,700 So if it is not there you have to install your Paramiko 91 00:05:05,700 --> 00:05:11,300 Module. So I am giving you simply here steps, 'pip install paramiko' 92 00:05:11,300 --> 00:05:15,000 to install your Paramiko Module. That's fine. 93 00:05:15,900 --> 00:05:18,000 Then second thing is, how your, 94 00:05:18,600 --> 00:05:22,600 yeah, now let me start with code to use your paramiko to work 95 00:05:22,600 --> 00:05:23,700 with your remote servers. 96 00:05:24,500 --> 00:05:26,200 Let me write a simple code here. 97 00:05:27,600 --> 00:05:30,400 Sorry, not this, this is my local server. 98 00:05:30,400 --> 00:05:31,700 Let me exit from here. 99 00:05:31,700 --> 00:05:34,210 [no audio] 100 00:05:34,200 --> 00:05:42,200 'working_with_remote_linux', maybe any server, remote, that 101 00:05:42,200 --> 00:05:44,800 that actually any server, you can work with any remote server. 102 00:05:45,300 --> 00:05:49,600 Of course, but time being we are going to work with only remote 103 00:05:49,600 --> 00:05:53,100 server as Linux. And one more thing, let me take 104 00:05:54,800 --> 00:05:55,900 my 'python' path, 105 00:05:55,900 --> 00:06:03,100 [no audio] 106 00:06:03,100 --> 00:06:06,500 see that. The very first thing, you need to 107 00:06:06,500 --> 00:06:08,300 import Paramiko Module. 108 00:06:08,300 --> 00:06:11,010 [no audio] 109 00:06:11,000 --> 00:06:12,200 Right. That's fine. 110 00:06:12,400 --> 00:06:16,000 So after that your paramiko is going to work with your 111 00:06:16,000 --> 00:06:18,000 remote servers by creating a client. 112 00:06:18,000 --> 00:06:19,600 Actually, we need to create that client. 113 00:06:19,600 --> 00:06:24,200 So to create your client, this is the syntax. So here 114 00:06:24,200 --> 00:06:26,000 [no audio] 115 00:06:26,000 --> 00:06:28,300 guys, I will give all these steps on the below of this video, 116 00:06:28,300 --> 00:06:31,400 then you can go with that and you can practice. So this is 117 00:06:31,400 --> 00:06:35,000 simply any variable, but this is your client, 'SSHClient'. 118 00:06:35,000 --> 00:06:38,100 Simply you can assume 'ssh' of your operating system. 119 00:06:39,000 --> 00:06:42,600 Now I created from paramiko this 'SSHClient'. Be clear 120 00:06:42,600 --> 00:06:46,000 this is a variable, you can take any name, but I have 121 00:06:46,000 --> 00:06:47,100 taken suitable name. 122 00:06:47,800 --> 00:06:49,100 So after that 123 00:06:50,200 --> 00:06:53,000 let me try to connect with my remote server. 124 00:06:53,500 --> 00:06:55,300 So to connect with your remote server 125 00:06:55,300 --> 00:06:56,600 we have a syntax called, 126 00:06:56,700 --> 00:06:59,700 I mean anyway we have two procedures -One is using 'username' 127 00:06:59,700 --> 00:07:05,500 and 'password', second one is using 'username' and 'keys'. 128 00:07:05,700 --> 00:07:09,900 So for time being I am going to use 'username' and 'password'. 129 00:07:11,000 --> 00:07:13,600 So let me take my remote server 'hostname' first. 130 00:07:13,700 --> 00:07:17,400 So guys, this is the syntax simply, 'ssh.connect', your remote 131 00:07:17,400 --> 00:07:21,200 server 'hostname', with what user and what is the password, and 132 00:07:21,200 --> 00:07:24,700 by default it is going to use port 22 only, you no need to 133 00:07:24,700 --> 00:07:28,000 mention, but if you want to mention you can mention it. Now 134 00:07:28,000 --> 00:07:30,600 let me open my remote server. 135 00:07:30,600 --> 00:07:32,710 [no audio] 136 00:07:32,700 --> 00:07:35,200 So for this remote server, this is a public IP, 137 00:07:35,900 --> 00:07:37,100 no, I mean 'hostname'. 138 00:07:37,100 --> 00:07:41,800 You can also mention in your real-time FQDN name of your server. 139 00:07:43,500 --> 00:07:46,900 That's fine. 'username' is this one. And I have given already 'password' as 140 00:07:47,200 --> 00:07:51,800 'paramiko123' I think. Let me check it. 'paramiko123'. 141 00:07:52,900 --> 00:07:55,900 Let me save it and run this. So before going to run 142 00:07:55,900 --> 00:07:59,200 let me provide execution permission because we are on Linux. 143 00:07:59,200 --> 00:08:01,200 [no audio] 144 00:08:01,200 --> 00:08:03,000 See that, you are getting an error called 145 00:08:03,000 --> 00:08:06,000 'paramiko.ssh_exception', 146 00:08:07,200 --> 00:08:11,300 and if you observe here, "not found in known hosts". See whenever 147 00:08:11,300 --> 00:08:14,700 if you try to connect with any remote server using your SSH at 148 00:08:14,700 --> 00:08:20,400 very first time, so let me show you that, 'ssh' suppose 149 00:08:20,400 --> 00:08:22,200 this is my 'hostname', 150 00:08:23,800 --> 00:08:26,400 see it is going to ask 'yes/no' permission, right? 151 00:08:26,800 --> 00:08:29,600 So manually you are entering here, 152 00:08:29,700 --> 00:08:32,600 but in Python script we are not entering that 'yes/no' option. 153 00:08:32,600 --> 00:08:34,700 If you say 'yes' then only you can connect. If you say 'no' you 154 00:08:34,700 --> 00:08:35,700 are going to reject. 155 00:08:36,700 --> 00:08:40,500 Now if you don't want to ask this, right, in your SSH you are 156 00:08:40,500 --> 00:08:43,100 providing some strict host key checking as 'no'. 157 00:08:43,100 --> 00:08:45,000 [no audio] 158 00:08:45,000 --> 00:08:49,799 Right. But in Python how we can avoid that checking this, host key checking, 159 00:08:50,299 --> 00:08:51,299 right? Simple. 160 00:08:52,200 --> 00:08:56,500 You need to add a 'AutoAddPolicy' to avoid that. 161 00:08:57,800 --> 00:09:00,500 I don't want to check 'yes/no' confirmation. By default 162 00:09:00,500 --> 00:09:02,900 I want to work with that given server. 163 00:09:03,300 --> 00:09:06,900 So for that you need to add this line before going to connect 164 00:09:06,900 --> 00:09:08,700 with your remote server. That's it. 165 00:09:08,700 --> 00:09:10,910 [no audio] 166 00:09:10,900 --> 00:09:15,100 Right. Now if I run this, now you're not going to get any 167 00:09:15,100 --> 00:09:18,800 error. Just let me run this and see the result. 168 00:09:20,100 --> 00:09:24,100 That's it. You're not getting any error means now your script has been 169 00:09:24,700 --> 00:09:28,300 connected with the, I mean through script you have been connected 170 00:09:28,300 --> 00:09:30,200 with your remote server, this server. 171 00:09:30,500 --> 00:09:33,500 So guys the connection, whatever the connection you established 172 00:09:33,500 --> 00:09:37,000 with your remote server will be there with this 'ssh' object 173 00:09:37,000 --> 00:09:41,900 only. Now to execute any commands, or to transfer a file, for 174 00:09:41,900 --> 00:09:44,200 all those things you have to use this SSH. 175 00:09:45,700 --> 00:09:49,500 Now, let me use this to execute some commands. 176 00:09:50,400 --> 00:09:52,500 So simply to execute your commands 177 00:09:52,500 --> 00:09:54,400 you have to write 'ssh.', 178 00:09:55,400 --> 00:09:57,800 yeah, one more thing. You can write, I mean you can execute 179 00:09:57,800 --> 00:10:03,200 any command here, 'exec', execute, 'command', but before that 180 00:10:04,200 --> 00:10:08,000 if you are running some commands on your remote server sometimes 181 00:10:08,000 --> 00:10:12,600 you may need some input to provide that, I mean to provide 182 00:10:12,600 --> 00:10:18,200 input for that command, or some, forget about input, sometimes 183 00:10:18,200 --> 00:10:21,000 you're going to get success output, sometimes you're going 184 00:10:21,000 --> 00:10:24,000 to get error output while running your command, while executing 185 00:10:24,000 --> 00:10:27,200 your command on your remote server. That's why here we are 186 00:10:27,200 --> 00:10:31,400 having, to execute our command this is the syntax. You can 187 00:10:31,400 --> 00:10:36,700 take any command. Suppose I am running, see guys 188 00:10:37,100 --> 00:10:40,600 I am using 'ssh' to execute our commands on your remote server. 189 00:10:40,600 --> 00:10:42,460 [no audio] 190 00:10:42,450 --> 00:10:47,600 Right. And if you observe here, this command, I mean after 191 00:10:47,600 --> 00:10:50,300 running this command you may get some success output. If you 192 00:10:50,300 --> 00:10:53,100 got success that is going to store into this variable. 193 00:10:53,500 --> 00:10:56,300 If you get an error while running this command that output 194 00:10:56,300 --> 00:11:00,900 will store into this. If you get output, error is null, empty. 195 00:11:01,200 --> 00:11:04,400 If you get error, output is null. Then what is the use of this 196 00:11:04,400 --> 00:11:08,400 'stdin'. See while running your commands on your remote server 197 00:11:08,400 --> 00:11:10,800 sometimes that command may need input. 198 00:11:11,400 --> 00:11:13,200 Let's say you are running some 'sudo' command, 199 00:11:14,800 --> 00:11:15,500 and 'sudo' command 200 00:11:15,500 --> 00:11:18,300 sometimes may need password, that password we can provide with 201 00:11:18,300 --> 00:11:19,500 the help of 'stdin'. 202 00:11:20,500 --> 00:11:23,200 Guys working with 'stdin' we will see in the later 203 00:11:23,200 --> 00:11:24,100 videos, as of now 204 00:11:24,100 --> 00:11:27,600 I am going to take only 'stdout' and 'stderr'. Nothing is there. 205 00:11:28,400 --> 00:11:30,200 I am running my command in this way. 206 00:11:31,600 --> 00:11:34,900 Then let me run first very simple command. Suppose 207 00:11:34,900 --> 00:11:37,000 I want to get, 'whoami'? 208 00:11:38,300 --> 00:11:40,100 First let me give invalid command. 209 00:11:41,900 --> 00:11:47,600 Right. See actually my intention is I'm trying to find with 210 00:11:47,600 --> 00:11:48,900 what user we connected. 211 00:11:48,900 --> 00:11:51,200 We know that we connected with this user only. That output 212 00:11:51,200 --> 00:11:54,800 actually we are expecting. But this is a invalid command 213 00:11:54,800 --> 00:11:56,700 that's why you're going to get an error. That error will be 214 00:11:56,700 --> 00:12:01,700 there in this. And these two 'stdout' and 'stdin', they are like 215 00:12:01,700 --> 00:12:02,700 file objects. 216 00:12:03,000 --> 00:12:07,200 So if you know, if you remember how to read a content from 217 00:12:07,200 --> 00:12:12,500 file object, by using '.read', or '.readlines'. Let me write 218 00:12:12,500 --> 00:12:20,100 first. "The output is: ", in case if you have a output, and let me print. 219 00:12:20,100 --> 00:12:22,110 [no audio] 220 00:12:22,100 --> 00:12:26,800 "The error is: ". Now I am going to print here the success output 221 00:12:26,800 --> 00:12:31,000 in case if we have that output, 'stdout.read'. 222 00:12:32,500 --> 00:12:35,800 Then in case if you get an error that we are going to print 223 00:12:35,800 --> 00:12:38,600 here, 'stderr.read' 224 00:12:40,300 --> 00:12:41,300 Let me save it. 225 00:12:42,800 --> 00:12:43,900 And let me run this. 226 00:12:44,900 --> 00:12:50,500 See the output. What happened? "invalid syntax". 'print(stderr.read()))' 227 00:12:50,500 --> 00:12:55,800 Yeah, we have given one extra parentheses I think. Now it's fine. 228 00:13:00,200 --> 00:13:03,200 See, output you are getting an empty but error you 229 00:13:03,200 --> 00:13:07,400 are getting something because the command whatever we have 230 00:13:07,400 --> 00:13:09,600 given that is invalid command. That's why it's saying, "command 231 00:13:09,600 --> 00:13:12,600 not found". That's why we are getting an error. And if you observe 232 00:13:12,600 --> 00:13:16,200 you're also getting an empty, empty line because this is the 233 00:13:16,200 --> 00:13:19,500 file content, right. If you read a file content by default 234 00:13:19,500 --> 00:13:22,800 for each and every line at the end you have a '\n', so 235 00:13:22,800 --> 00:13:24,900 for this line also you're having at the end '\n', and that's 236 00:13:24,900 --> 00:13:29,200 why we are getting this. Now you can do something. Not only 'read' 237 00:13:29,200 --> 00:13:31,800 you can also write, 'readlines' information. 238 00:13:31,800 --> 00:13:34,410 [no audio] 239 00:13:34,400 --> 00:13:38,900 But if you remember while working with files, 'readlines' is 240 00:13:38,900 --> 00:13:40,900 a list of values. 241 00:13:41,600 --> 00:13:45,400 I mean simply your file content will be converted into number 242 00:13:45,400 --> 00:13:47,200 of values in your list. 243 00:13:48,800 --> 00:13:50,700 Each line is one value in your list. 244 00:13:50,700 --> 00:13:53,710 [no audio] 245 00:13:53,700 --> 00:13:55,500 That's it. See that '\n' is there. 246 00:13:55,500 --> 00:13:57,400 [no audio] 247 00:13:57,400 --> 00:13:59,900 Right. Anyway, that's fine. Based on your requirement 248 00:13:59,900 --> 00:14:02,000 you can go with 'read' or 'readlines'. 249 00:14:02,500 --> 00:14:04,900 Now, let me give a valid command. 250 00:14:05,000 --> 00:14:10,500 Let's say 'whoami'. 'whoami', now this is a valid command, right? 251 00:14:11,000 --> 00:14:12,100 Now see the result. 252 00:14:12,100 --> 00:14:15,410 [no audio] 253 00:14:15,400 --> 00:14:19,800 Easy to user. But output is, I mean error output is empty. Because 254 00:14:19,800 --> 00:14:23,200 you got success output that’s why error is empty, right? 255 00:14:23,500 --> 00:14:26,600 Guys forget about input 'stdin', but syntax is you have to 256 00:14:26,600 --> 00:14:28,900 use 'stdin, stdout, stderr'. 257 00:14:29,800 --> 00:14:31,800 That's fine. Now, let me run one more command. 258 00:14:31,800 --> 00:14:32,800 You can run any command. 259 00:14:32,800 --> 00:14:33,900 Let me comment this. 260 00:14:33,900 --> 00:14:36,610 [no audio] 261 00:14:36,600 --> 00:14:38,400 Let me write same syntax. 262 00:14:38,400 --> 00:14:41,910 [no audio] 263 00:14:41,900 --> 00:14:42,900 Suppose I am running, 264 00:14:44,300 --> 00:14:45,300 let's say 'uptime'. 265 00:14:46,800 --> 00:14:48,800 Save it, and run it, and see the result. 266 00:14:48,800 --> 00:14:51,710 [no audio] 267 00:14:51,700 --> 00:14:52,700 That's it. 268 00:14:53,600 --> 00:14:55,500 Right. So not only that, 269 00:14:57,000 --> 00:15:01,700 let me execute one more thing like 'free - m'. 270 00:15:01,700 --> 00:15:04,410 [no audio] 271 00:15:04,400 --> 00:15:05,400 See the result. 272 00:15:05,400 --> 00:15:07,700 [no audio] 273 00:15:07,700 --> 00:15:08,700 That's it. 274 00:15:08,700 --> 00:15:11,800 [no audio] 275 00:15:11,800 --> 00:15:15,200 Right. Yeah, what you are getting here? 276 00:15:15,200 --> 00:15:20,110 [no audio] 277 00:15:20,100 --> 00:15:24,300 Okay, 'stdout.readline', not 'lines', right? 278 00:15:24,400 --> 00:15:26,700 So that's why you got a, I mean you're not getting perfect 279 00:15:26,700 --> 00:15:29,200 output. 'readlines' is going to give all the lines from your 280 00:15:29,200 --> 00:15:31,800 content. So previously I used only 'readline'. 281 00:15:31,800 --> 00:15:33,100 Now see that, 'readlines'. 282 00:15:33,100 --> 00:15:36,000 You're going to get complete output for your 'free -m'. 283 00:15:37,800 --> 00:15:40,800 That's fine. So guys, this is the simple way to execute your commands 284 00:15:40,800 --> 00:15:42,000 on your remote server. 285 00:15:42,300 --> 00:15:45,700 And here we are using 'username' and 'password'. And one more 286 00:15:45,700 --> 00:15:48,300 thing, our local server is Linux and remote server is also 287 00:15:48,300 --> 00:15:54,900 Linux. Now, let me execute same thing with the help of 'keys'. 288 00:15:54,900 --> 00:15:57,310 [no audio] 289 00:15:57,300 --> 00:16:00,000 First let me comment this, 290 00:16:01,300 --> 00:16:03,800 working with your 'username' and 'password'. 291 00:16:03,800 --> 00:16:06,000 So if you want to work with 'keys', very simple. 292 00:16:06,000 --> 00:16:07,900 [no audio] 293 00:16:07,900 --> 00:16:10,200 First thing, this is the syntax. 294 00:16:10,200 --> 00:16:17,610 [no audio] 295 00:16:17,600 --> 00:16:21,400 Right. Simply your 'hostname', 'username', instead of 'password' 296 00:16:21,400 --> 00:16:25,000 we are going to use 'key_filename', then port, that is your 297 00:16:25,000 --> 00:16:26,500 wish, if you want to use you can use it 298 00:16:26,500 --> 00:16:28,400 otherwise you can also skip that. No problem. 299 00:16:30,000 --> 00:16:31,000 And one more thing. What 300 00:16:31,000 --> 00:16:33,800 is this 'key'? From where you can get this 'key'? 301 00:16:33,800 --> 00:16:36,010 [no audio] 302 00:16:36,000 --> 00:16:37,000 Right. See that. 303 00:16:37,000 --> 00:16:40,210 [no audio] 304 00:16:40,200 --> 00:16:46,500 See, you can create your 'keys' using 'ssh', and you can exchange your 'key' 305 00:16:46,700 --> 00:16:49,700 then the private key path you have to mention in this file, 306 00:16:51,300 --> 00:16:52,300 I mean in your code. 307 00:16:53,400 --> 00:16:55,600 The private 'key_filename' you have to mention here. 308 00:16:56,300 --> 00:16:57,200 Let me do one thing. 309 00:16:58,000 --> 00:17:00,200 I'm going to do, '.ssh'. 310 00:17:00,200 --> 00:17:03,010 [no audio] 311 00:17:03,000 --> 00:17:05,400 Let me, no files here, right? 312 00:17:05,400 --> 00:17:06,700 Let me create some keys. 313 00:17:06,700 --> 00:17:08,200 'ssh-keygen'. 314 00:17:09,200 --> 00:17:12,500 So I am going to create my SSH key values, 315 00:17:12,500 --> 00:17:17,098 I mean keys. See that. We have private key and public key. 316 00:17:17,098 --> 00:17:20,500 First we have to share this key with your suppose your remote server. 317 00:17:21,200 --> 00:17:23,900 Let me share that with my remote server. 318 00:17:23,900 --> 00:17:27,010 [no audio] 319 00:17:27,000 --> 00:17:28,300 What is the remote server IP? 320 00:17:28,800 --> 00:17:29,800 Let me get it. 321 00:17:29,800 --> 00:17:37,400 [no audio] 322 00:17:37,400 --> 00:17:40,300 And be clear, here I am sharing 323 00:17:40,300 --> 00:17:42,310 [no audio] 324 00:17:42,300 --> 00:17:46,000 with 'ec2-user'. 'ec2-user' 325 00:17:46,700 --> 00:17:47,800 I'm sharing my key. 326 00:17:47,800 --> 00:17:56,410 [no audio] 327 00:17:56,400 --> 00:17:58,600 Now 'ec2-user'. Sorry 328 00:17:58,600 --> 00:18:03,100 what is the 'password' we've given for that? 'paramiko123'. 329 00:18:03,100 --> 00:18:05,510 [no audio] 330 00:18:05,500 --> 00:18:06,500 That's it. 331 00:18:06,700 --> 00:18:08,300 Now, what is your private key? 332 00:18:08,500 --> 00:18:10,900 This is your private key. In this part 333 00:18:11,200 --> 00:18:12,900 we have your private key. 334 00:18:12,900 --> 00:18:23,210 [no audio] 335 00:18:23,200 --> 00:18:24,200 Simply. 336 00:18:24,200 --> 00:18:28,200 [no audio] 337 00:18:28,200 --> 00:18:32,700 Right. Now this file we have to use in our code. That's it. 338 00:18:32,700 --> 00:18:35,110 [no audio] 339 00:18:35,100 --> 00:18:38,100 So in case by default anyway you are having now password-less 340 00:18:38,100 --> 00:18:39,400 authentication, you can use it. 341 00:18:39,400 --> 00:18:43,800 But if you want to use key or just provide your 'key_filename' 342 00:18:43,800 --> 00:18:46,500 path here completely. If your key is there in the current 343 00:18:46,500 --> 00:18:48,200 location directly 344 00:18:48,200 --> 00:18:49,800 you can write your only key name. 345 00:18:49,800 --> 00:18:52,510 [no audio] 346 00:18:52,500 --> 00:18:53,500 That's it. Now, 347 00:18:53,500 --> 00:18:56,500 let me save it and run it. Be clear now 348 00:18:56,500 --> 00:19:01,800 we are using username and your cryptographic key, SSH private 349 00:19:01,800 --> 00:19:02,800 key, right. Now, 350 00:19:02,800 --> 00:19:03,800 Let me run, 351 00:19:04,800 --> 00:19:05,800 and see the result. 352 00:19:05,800 --> 00:19:07,810 [no audio] 353 00:19:07,800 --> 00:19:08,800 What happened? 354 00:19:08,800 --> 00:19:10,500 [no audio] 355 00:19:10,500 --> 00:19:12,200 'Authentication failed'. 356 00:19:12,800 --> 00:19:18,100 Oh I'm using 'ansadmin' user. That's not correct. 357 00:19:18,100 --> 00:19:21,410 [no audio] 358 00:19:21,400 --> 00:19:22,400 Yeah. 359 00:19:23,500 --> 00:19:25,800 Username is 'ec2-user'. With 'ec2-user' 360 00:19:25,800 --> 00:19:29,800 we exchanged our key, right. So be clear about user and 361 00:19:30,600 --> 00:19:31,700 your keys. 362 00:19:31,700 --> 00:19:35,410 [no audio] 363 00:19:35,400 --> 00:19:38,200 Yeah, now let me run this and see the result. 364 00:19:38,200 --> 00:19:40,400 [no audio] 365 00:19:40,400 --> 00:19:41,400 That's it. 366 00:19:41,800 --> 00:19:45,100 So guys this is the way how we can work with 'username' and 'password', 367 00:19:45,100 --> 00:19:47,600 and how we can work with your 'SSH keys' 368 00:19:48,500 --> 00:19:50,900 using your paramiko, okay. 369 00:19:51,200 --> 00:19:54,000 I will share this key below this video just go through that. 370 00:19:54,800 --> 00:19:57,700 And just now we worked with Linux with Linux. Now, 371 00:19:57,700 --> 00:20:02,900 my requirement is I want to work from my local server, from 372 00:20:02,900 --> 00:20:06,000 sorry, my local server as Windows. Let's say this is my 373 00:20:06,000 --> 00:20:07,000 Windows Server, right? 374 00:20:07,000 --> 00:20:10,110 [no audio] 375 00:20:10,100 --> 00:20:14,100 Now, let me write a new code on my Windows Server, save it. 376 00:20:14,100 --> 00:20:16,910 [no audio] 377 00:20:16,900 --> 00:20:18,800 Some key, some script name as, 378 00:20:18,800 --> 00:20:27,100 'working_with_remote_Linux_from_windows.py' 379 00:20:28,000 --> 00:20:30,600 And before going to work with that, you have to check it. 380 00:20:30,700 --> 00:20:32,600 Do we have SSH? Yes. On my Windows 381 00:20:32,600 --> 00:20:35,700 I already installed SSH. If it is there, okay. 382 00:20:35,700 --> 00:20:37,700 If it is not there, please install them first. 383 00:20:38,900 --> 00:20:40,300 Right. Then after that 384 00:20:41,600 --> 00:20:44,300 let me take step by step. 385 00:20:44,700 --> 00:20:45,900 We know we are already having. 386 00:20:47,200 --> 00:20:51,000 First we need to install, we need to install 'paramiko', 387 00:20:51,000 --> 00:20:54,600 then we have to import that in our code, then we need to add, 388 00:20:55,000 --> 00:20:59,100 we need to create SSH client, then 'AutoAddPolicy'. 389 00:21:00,800 --> 00:21:06,100 [no audio] 390 00:21:06,100 --> 00:21:09,600 Just now I added 'AutoAddPolicy'. Just to avoid yes or 391 00:21:09,600 --> 00:21:12,800 no confirmation like that, right. Then after that you need 392 00:21:12,800 --> 00:21:14,100 to connect with your server. 393 00:21:14,100 --> 00:21:15,700 So to connect with your server 394 00:21:15,700 --> 00:21:17,910 [no audio] 395 00:21:17,900 --> 00:21:18,900 this the syntax, 396 00:21:18,900 --> 00:21:23,500 [no audio] 397 00:21:23,500 --> 00:21:25,600 'ssh.connect' your 'hostname', 398 00:21:25,600 --> 00:21:26,700 maybe this is 'hostname'. 399 00:21:26,900 --> 00:21:27,900 Let me verify it. 400 00:21:28,300 --> 00:21:32,600 392, yeah, 'hostname'. Then 'username' is as of now 401 00:21:32,600 --> 00:21:39,600 we are having our username as 'ec2-user'. Then we are not 402 00:21:39,600 --> 00:21:41,100 using 'key_filename'. 403 00:21:41,800 --> 00:21:44,200 So what we are using? We are using simply 'password'. 404 00:21:45,300 --> 00:21:49,500 Right. So what is the 'password'? Did we mention here? No. 405 00:21:50,000 --> 00:21:53,400 Yeah, 'password' is 'paramiko123'. 406 00:21:55,000 --> 00:21:59,200 Save it. First let me run this. If it is fine 407 00:21:59,200 --> 00:22:00,400 it won't give any exception. 408 00:22:00,400 --> 00:22:03,810 [no audio] 409 00:22:03,800 --> 00:22:04,900 Yeah, it's fine. That's why 410 00:22:04,900 --> 00:22:07,400 it has been executed without any errors. Now 411 00:22:07,400 --> 00:22:10,600 I want to execute some command, any command you can execute 412 00:22:10,600 --> 00:22:11,600 on your remote server. 413 00:22:12,700 --> 00:22:15,800 So guys now I'm working with from my local server. 414 00:22:15,800 --> 00:22:18,910 [no audio] 415 00:22:18,900 --> 00:22:22,300 Yes. Yeah, let me print the output, then only we can see the output, 416 00:22:22,300 --> 00:22:29,800 right. 'stdout.'. Sorry not 'stderr', 'stdout.read', maybe 'readlines' 417 00:22:30,500 --> 00:22:31,500 That's fine. 418 00:22:31,500 --> 00:22:33,100 [no audio] 419 00:22:33,100 --> 00:22:33,500 That's it. 420 00:22:33,500 --> 00:22:36,310 [no audio] 421 00:22:36,300 --> 00:22:37,500 You're getting your output, right? 422 00:22:38,800 --> 00:22:41,900 That's fine. Now on Windows, 423 00:22:43,100 --> 00:22:45,500 I mean from my local server as Windows 424 00:22:45,500 --> 00:22:48,300 I connected with my remote server Linux, and I executed 425 00:22:48,300 --> 00:22:51,900 some command with the 'username' and 'password', now if you want 426 00:22:51,900 --> 00:22:53,800 to work with some keys, 427 00:22:53,800 --> 00:22:56,800 yes, you can also generate your keys on your local server 428 00:22:56,900 --> 00:22:59,700 the same way how we worked on Linux server. 429 00:23:00,900 --> 00:23:04,400 So in Linux server, I generated some keys and I shared with 430 00:23:04,400 --> 00:23:08,300 my remote server, and I then used this in this way, right. The 431 00:23:08,300 --> 00:23:09,300 same way on Windows 432 00:23:09,300 --> 00:23:12,400 also you can generate your keys and you can work with your 433 00:23:12,400 --> 00:23:16,200 remote server from your local server as Windows. That's it. 434 00:23:16,200 --> 00:23:18,610 [no audio] 435 00:23:18,600 --> 00:23:23,800 Guys, not only this, this is just basic way to execute your 436 00:23:23,800 --> 00:23:26,500 commands on your remote server, but while working with your 437 00:23:26,500 --> 00:23:29,700 remote servers you will get different exceptions and you 438 00:23:29,700 --> 00:23:31,000 need to handle all of them. 439 00:23:31,400 --> 00:23:34,600 So how to handle, we'll discuss in later videos, and how to 440 00:23:34,600 --> 00:23:37,300 transfer files from your local server to Windows servers 441 00:23:37,300 --> 00:23:40,400 all those things we are going to discuss in later videos. 442 00:23:41,400 --> 00:23:42,400 Okay. 443 00:23:42,900 --> 00:23:43,900 Okay. 444 00:23:44,800 --> 00:23:47,000 Yeah, okay guys. Thank you for watching this video. 445 00:23:47,590 --> 00:23:51,690 [no audio]