1 00:00:00,000 --> 00:00:02,300 [No audio] 2 00:00:02,300 --> 00:00:06,200 Friends, here our concept is, how to transfer a file from local server to 3 00:00:06,200 --> 00:00:10,200 remote server and vice versa, using paramiko of Python. 4 00:00:10,200 --> 00:00:13,400 See, simply you're having one local server and you're 5 00:00:13,400 --> 00:00:15,500 having one remote server, just assume that. 6 00:00:15,500 --> 00:00:18,400 Now, suppose some file is there on your local server, 7 00:00:18,400 --> 00:00:20,800 I need to transfer this file to your remote server, 8 00:00:20,800 --> 00:00:23,000 or some file is there on your remote server and I need 9 00:00:23,000 --> 00:00:25,400 I need to download that file to your local server. 10 00:00:25,400 --> 00:00:27,800 And these all actions I want to perform with the 11 00:00:27,800 --> 00:00:30,000 help of paramiko of your Python, 12 00:00:30,000 --> 00:00:32,200 or simply Paramiko Module. 13 00:00:32,200 --> 00:00:35,500 Right. Let me explain this, with simply a simple code. 14 00:00:35,500 --> 00:00:39,000 So, friends already we know how your Paramiko Module 15 00:00:39,000 --> 00:00:41,000 is going to work from our previous video. 16 00:00:41,000 --> 00:00:46,700 Your Paramiko Module is going to work, by creating a SSH client, right? 17 00:00:46,700 --> 00:00:49,400 Anyway, you are using paramiko, 18 00:00:49,400 --> 00:00:52,100 that's why first of all you need to import that module. 19 00:00:52,100 --> 00:00:55,800 Then, you need to create SSH client, right? 20 00:00:55,800 --> 00:00:57,700 See, you know how to create SSH Client. 21 00:00:57,700 --> 00:00:59,394 There is a simple syntax for that. 22 00:00:59,300 --> 00:01:03,100 Then after that you need to add 'AutoAddPolicy'. 23 00:01:03,100 --> 00:01:11,000 This is just to avoid asking yes or no confirmation 24 00:01:11,000 --> 00:01:13,500 while connecting with your remote server at the very first time, 25 00:01:13,500 --> 00:01:18,200 or simply it is going to add to your known host on your local server. 26 00:01:18,200 --> 00:01:19,410 That's fine. 27 00:01:19,400 --> 00:01:22,500 Then after that, let me try to connect with your server. 28 00:01:22,500 --> 00:01:24,910 So you know how to connect with your server. 29 00:01:24,900 --> 00:01:29,600 So simply, using this SSH we created a connection with your 30 00:01:29,600 --> 00:01:33,200 remote server and whatever the connection we establish that 31 00:01:33,200 --> 00:01:38,500 will be pointed with the help of SSH object or SSH client. 32 00:01:38,500 --> 00:01:41,400 That's it. Now suppose, 33 00:01:41,400 --> 00:01:45,100 guys actually, I forgot to give this point in your previous video. 34 00:01:45,100 --> 00:01:49,500 That is once if your work is done with the help of your SSH client, 35 00:01:49,500 --> 00:01:52,600 finally you can also close your SSH client connection. 36 00:01:52,600 --> 00:01:56,800 Even though if you don't close, by default, it will be closed after some time. 37 00:01:56,800 --> 00:01:59,700 But it is better practice to close your SSH client 38 00:01:59,700 --> 00:02:03,300 once your work is done with your SSH client. That's fine. 39 00:02:04,100 --> 00:02:08,000 So using this SSH client you can execute some commands, 40 00:02:08,000 --> 00:02:11,600 but here our requirement is, we need to transfer a file to your 41 00:02:11,600 --> 00:02:14,900 remote server, or we need to download a file from your remote server. 42 00:02:14,900 --> 00:02:18,800 So for that you need to create from this SSH client 43 00:02:18,800 --> 00:02:23,300 one 'sftp' object or 'sftp' connection. 44 00:02:23,300 --> 00:02:26,200 So for that we have a simple syntax. See that. 45 00:02:26,200 --> 00:02:28,600 [no audio] 46 00:02:28,600 --> 00:02:29,800 Let me open my code. 47 00:02:29,800 --> 00:02:34,100 So, guys here I am going to write simply, 'sftp_client' object, 48 00:02:34,100 --> 00:02:36,578 or simply connection with the help of 'sftp'. 49 00:02:36,500 --> 00:02:40,600 'ssh.open_sftp()'. That's it. 50 00:02:40,698 --> 00:02:42,200 [No audio] 51 00:02:42,200 --> 00:02:45,700 Now, with the help of this fifth line, we are opening 52 00:02:45,700 --> 00:02:48,000 'sftp' connection with your remote server. 53 00:02:48,000 --> 00:02:51,600 Now using this 'sftp_client' object, or connection 54 00:02:51,600 --> 00:02:56,500 we can transfer a file, or we can download a file from your remote server. 55 00:02:56,500 --> 00:02:59,000 Now, see that. I am listing the operations which are 56 00:02:59,000 --> 00:03:00,800 there with your 'sftp_client'. 57 00:03:00,800 --> 00:03:03,800 [No audio] 58 00:03:03,800 --> 00:03:05,800 So, you are having different types of operations. 59 00:03:05,800 --> 00:03:07,800 Suppose if you want to change the ownership of a 60 00:03:07,800 --> 00:03:09,200 file on your remote server, 61 00:03:09,200 --> 00:03:14,000 then you can use 'sftp_client.changeownership'. 62 00:03:14,000 --> 00:03:19,000 Right. Likewise we have, suppose, current working directory. Right. 63 00:03:19,300 --> 00:03:21,000 Now see that? Let me go and show you that. 64 00:03:21,000 --> 00:03:22,300 And one more thing, here also 65 00:03:22,300 --> 00:03:25,300 once if your work is done with your 'sftp_client', 66 00:03:25,300 --> 00:03:29,100 you can also close this 'sftp' connection or session. 67 00:03:29,500 --> 00:03:32,900 So be clear, before 'sftp_client' don't close SSH, 68 00:03:32,900 --> 00:03:37,600 because your 'sftp' is depending on your SSH client object or connection. 69 00:03:38,200 --> 00:03:42,100 Right. See. Now, what I am doing is, first let me open my remote server. 70 00:03:42,100 --> 00:03:43,900 This is my remote server. Here 71 00:03:43,900 --> 00:03:44,900 I have some files. 72 00:03:44,900 --> 00:03:48,400 Let's say I'm having a file called, 'paramiko_download.txt'. 73 00:03:48,400 --> 00:03:49,800 In this we have some content. 74 00:03:49,800 --> 00:03:54,600 I need to download this file on my local server with the help of Python paramiko 75 00:03:54,600 --> 00:03:58,660 Then simply syntax is, see that, 'sftp_client.get', 76 00:03:58,660 --> 00:04:01,900 your remote server file with complete path. 77 00:04:01,900 --> 00:04:03,300 Actually, that is a good practice. 78 00:04:03,300 --> 00:04:07,310 Now, let me take complete path. '/home/ec2-user', 79 00:04:07,300 --> 00:04:08,600 then this file name, right? 80 00:04:08,600 --> 00:04:13,800 See that how I am going to write, source is, 'ec2-user/', your file. 81 00:04:13,800 --> 00:04:15,800 Then on the local host, 82 00:04:15,800 --> 00:04:19,500 so for 'get' you have a source file and then destination. 83 00:04:19,500 --> 00:04:22,736 'get' means downloading a file from remote server. 84 00:04:22,736 --> 00:04:24,300 Now suppose, 85 00:04:24,300 --> 00:04:34,400 I am directly giving, 'paramiko_downloaded_file.txt' 86 00:04:34,400 --> 00:04:37,500 See, I'm not giving complete path. Just I am giving the file name 87 00:04:37,500 --> 00:04:40,100 in which you want to store this remote file content. 88 00:04:41,100 --> 00:04:42,895 So, whenever if you run your Python script, 89 00:04:42,800 --> 00:04:45,000 in which location this Python script is there, 90 00:04:45,000 --> 00:04:46,610 there it is going to download. 91 00:04:46,600 --> 00:04:49,700 Instead of that if you want to store in any required path, 92 00:04:49,700 --> 00:04:52,200 then you have to provide complete path on your Windows Server. 93 00:04:52,200 --> 00:04:55,700 Anyway, as of now I am running this code from my Windows server. 94 00:04:55,700 --> 00:05:00,500 Right. See in our case, local server, remote server 95 00:05:00,500 --> 00:05:02,900 and our remote server is Linux 96 00:05:02,900 --> 00:05:04,705 and local server I am taking as Windows. 97 00:05:04,700 --> 00:05:07,300 Any combination will work. Right. 98 00:05:07,300 --> 00:05:10,300 Now see that. Let me save it and run it. 99 00:05:10,324 --> 00:05:14,810 [no audio] 100 00:05:14,800 --> 00:05:19,300 It's done. Let me go to open my location where we have the server, 101 00:05:19,300 --> 00:05:22,010 I mean this script. 102 00:05:22,000 --> 00:05:24,838 'paramiko_download_file.txt'. 103 00:05:24,800 --> 00:05:27,800 'paramiko_downloaded_file.txt'. 104 00:05:27,800 --> 00:05:29,900 So, this is the file just now we downloaded. 105 00:05:29,900 --> 00:05:31,800 [No audio] 106 00:05:31,800 --> 00:05:34,050 So guys, this is the way we can download. 107 00:05:34,800 --> 00:05:37,000 Right. Now one more thing, 108 00:05:37,600 --> 00:05:42,210 I want to download this file, but without mentioning this path. 109 00:05:42,200 --> 00:05:46,900 See, sometimes you need to move into different location over 'sftp', 110 00:05:46,900 --> 00:05:49,800 then there you have to download the file without mentioning the path. 111 00:05:49,800 --> 00:05:52,300 Yes, we can do that also. Very simple. See that. 112 00:05:53,300 --> 00:05:55,800 Before going to download your file, 113 00:05:56,300 --> 00:06:00,700 what we have to do is, we need to move our 'sftp' connection, 114 00:06:00,700 --> 00:06:04,409 or we need to change the location of your 'sftp_client' object 115 00:06:04,400 --> 00:06:08,200 on your remote server by using 'chdir' operation. 116 00:06:08,100 --> 00:06:12,900 So, now I am going to suppose, '/etc' location, 117 00:06:12,900 --> 00:06:15,400 yeah, let me take simply '/home/ec2-user'. 118 00:06:17,100 --> 00:06:22,800 Right. Now, let me simply print current location pointed by your 'sftp_client'. 119 00:06:22,800 --> 00:06:26,100 So simply you can get current working directory with this syntax. 120 00:06:26,100 --> 00:06:27,300 See the output. 121 00:06:27,300 --> 00:06:30,409 [no audio] 122 00:06:30,400 --> 00:06:33,000 Now your 'sftp' is pointing to this location. 123 00:06:33,000 --> 00:06:35,900 So in this location whatever the file you are having, that file 124 00:06:35,900 --> 00:06:38,100 without mentioning complete path, you can download 125 00:06:38,100 --> 00:06:42,800 because already your 'sftp_client' object is pointing to this path, 126 00:06:42,800 --> 00:06:46,800 that's why now I am going to download 'sftp_client.get'. 127 00:06:46,800 --> 00:06:48,800 Let me take some other file here. 128 00:06:48,800 --> 00:06:52,700 So, let's say I'm having 'demo.txt' file from 'ec2-file', 129 00:06:52,700 --> 00:06:53,787 I won't to download that. 130 00:06:53,700 --> 00:06:57,700 Directly on remote server I have a file called 'demo.txt', 131 00:06:57,700 --> 00:07:02,000 that file I want to download it into let's say, desktop location. 132 00:07:02,000 --> 00:07:04,600 [No audio] 133 00:07:04,600 --> 00:07:05,900 Let me take complete path. 134 00:07:05,900 --> 00:07:09,810 [no audio] 135 00:07:09,800 --> 00:07:12,610 I want to download into desktop location. 136 00:07:12,600 --> 00:07:19,100 let's say, 'downloaded file.txt', with this file name. And 137 00:07:19,100 --> 00:07:20,900 be clear we are working with Windows, 138 00:07:20,900 --> 00:07:21,800 I mean local server. 139 00:07:21,800 --> 00:07:25,300 We are downloading our file to this Windows Server. 140 00:07:25,300 --> 00:07:27,448 That's why whenever if you are mentioning a path, 141 00:07:27,400 --> 00:07:30,000 it is always good to mention, '//' in your path 142 00:07:30,000 --> 00:07:32,200 whenever if you are providing your path manually. 143 00:07:33,200 --> 00:07:34,909 Let me save it and run it. 144 00:07:34,900 --> 00:07:39,600 [No audio] 145 00:07:39,600 --> 00:07:40,909 Yes, it has been done. 146 00:07:40,900 --> 00:07:44,964 Let me go to my desktop location and see there. 147 00:07:44,900 --> 00:07:47,100 Any file called, 'downloaded_file'? 148 00:07:47,100 --> 00:07:50,000 Yeah, see that. That's it. 149 00:07:50,000 --> 00:07:53,700 So, this is the way how we can mention complete path on your local server 150 00:07:53,700 --> 00:07:56,700 or how to mention complete path on your remote server. 151 00:07:56,700 --> 00:07:58,600 Without mentioning your complete path 152 00:07:58,600 --> 00:08:00,077 if you want to download a file, 153 00:08:00,000 --> 00:08:05,200 first change your 'sftp_client' pointing path on your remote server 154 00:08:05,200 --> 00:08:07,600 by using change directory. That's it. 155 00:08:07,600 --> 00:08:09,100 [No audio] 156 00:08:09,100 --> 00:08:12,000 Right. So guys, this is the way how you can download. 157 00:08:12,000 --> 00:08:15,710 Now, I want to transfer a file from local server to remote server. 158 00:08:15,700 --> 00:08:19,100 It's very simple. Now let me comment all these things. 159 00:08:19,100 --> 00:08:24,700 [No audio] 160 00:08:24,700 --> 00:08:27,810 Suppose in the current location I have some file. 161 00:08:27,800 --> 00:08:30,990 Let me transfer our Python script itself. 162 00:08:30,900 --> 00:08:36,600 'sftp_client.put', on your remote server into this location. 163 00:08:36,600 --> 00:08:38,100 [No audio] 164 00:08:38,100 --> 00:08:41,700 Sorry, source is our local server, right. 165 00:08:41,700 --> 00:08:44,700 So, let's say some, any script name called, 166 00:08:45,700 --> 00:08:47,500 yeah, I will take this Python script only. 167 00:08:47,500 --> 00:08:49,299 [no audio] 168 00:08:49,299 --> 00:08:54,909 'transfer_files.py'. So on the current location we have this file. 169 00:08:54,900 --> 00:08:56,299 So, this I want to move, 170 00:08:56,299 --> 00:09:00,900 I mean I want to transfer to your remote server, into this location, 171 00:09:00,900 --> 00:09:06,700 let's say 'transfer_files.py'. Let me run it and 172 00:09:06,700 --> 00:09:07,700 see the result. 173 00:09:07,700 --> 00:09:10,800 [no audio] 174 00:09:10,800 --> 00:09:14,000 Done. Go to your remote server. Just list out. 175 00:09:14,000 --> 00:09:18,200 See that. We got a file to this server. That's it. 176 00:09:19,200 --> 00:09:22,000 So guys, this is the simple way to transfer your 177 00:09:22,000 --> 00:09:23,900 file from local server to remote server, 178 00:09:24,200 --> 00:09:27,800 or how to download a file from remote server to local server. 179 00:09:27,800 --> 00:09:31,100 So, the very, very important point here is, whenever if 180 00:09:31,100 --> 00:09:34,700 you are downloading your file, 181 00:09:34,700 --> 00:09:37,200 then remote server file path is the source 182 00:09:37,200 --> 00:09:39,400 and destination is on your local server. 183 00:09:39,400 --> 00:09:41,200 Whenever if you are transferring, 184 00:09:41,200 --> 00:09:45,500 then local server file is the source and remote server path is the destination. 185 00:09:45,500 --> 00:09:48,000 And one more thing, on your remote server 186 00:09:48,000 --> 00:09:51,000 if you don't want to mention complete path for your file, 187 00:09:51,000 --> 00:09:54,000 then before going to download a file from your remote server, 188 00:09:54,000 --> 00:09:58,800 try to point out your 'sftp_client' location to your required path. 189 00:09:58,800 --> 00:10:01,900 Then from there you can download your required file, 190 00:10:01,900 --> 00:10:04,564 so simply, by using 'chdir' operation. 191 00:10:04,555 --> 00:10:05,500 [No audio] 192 00:10:05,500 --> 00:10:07,600 Right. Okay guys. 193 00:10:07,600 --> 00:10:09,200 Thank you for watching this video. 194 00:10:09,200 --> 00:10:17,400 [no audio]