[No audio]
Friends, here our concept is, how to transfer a file from local server to
remote server and vice versa, using paramiko of Python.
See, simply you're having one local server and you're
having one remote server, just assume that.
Now, suppose some file is there on your local server,
I need to transfer this file to your remote server,
or some file is there on your remote server and I need
I need to download that file to your local server.
And these all actions I want to perform with the
help of paramiko of your Python,
or simply Paramiko Module.
Right. Let me explain this, with simply a simple code.
So, friends already we know how your Paramiko Module
is going to work from our previous video.
Your Paramiko Module is going to work, by creating a SSH client, right?
Anyway, you are using paramiko,
that's why first of all you need to import that module.
Then, you need to create SSH client, right?
See, you know how to create SSH Client.
There is a simple syntax for that.
Then after that you need to add 'AutoAddPolicy'.
This is just to avoid asking yes or no confirmation
while connecting with your remote server at the very first time,
or simply it is going to add to your known host on your local server.
That's fine.
Then after that, let me try to connect with your server.
So you know how to connect with your server.
So simply, using this SSH we created a connection with your
remote server and whatever the connection we establish that
will be pointed with the help of SSH object or SSH client.
That's it. Now suppose,
guys actually, I forgot to give this point in your previous video.
That is once if your work is done with the help of your SSH client,
finally you can also close your SSH client connection.
Even though if you don't close, by default, it will be closed after some time.
But it is better practice to close your SSH client
once your work is done with your SSH client. That's fine.
So using this SSH client you can execute some commands,
but here our requirement is, we need to transfer a file to your
remote server, or we need to download a file from your remote server.
So for that you need to create from this SSH client
one 'sftp' object or 'sftp' connection.
So for that we have a simple syntax. See that.
[no audio]
Let me open my code.
So, guys here I am going to write simply, 'sftp_client' object,
or simply connection with the help of 'sftp'.
'ssh.open_sftp()'. That's it.
[No audio]
Now, with the help of this fifth line, we are opening
'sftp' connection with your remote server.
Now using this 'sftp_client' object, or connection
we can transfer a file, or we can download a file from your remote server.
Now, see that. I am listing the operations which are
there with your 'sftp_client'.
[No audio]
So, you are having different types of operations.
Suppose if you want to change the ownership of a
file on your remote server,
then you can use 'sftp_client.changeownership'.
Right. Likewise we have, suppose, current working directory. Right.
Now see that? Let me go and show you that.
And one more thing, here also
once if your work is done with your 'sftp_client',
you can also close this 'sftp' connection or session.
So be clear, before 'sftp_client' don't close SSH,
because your 'sftp' is depending on your SSH client object or connection.
Right. See. Now, what I am doing is, first let me open my remote server.
This is my remote server. Here
I have some files.
Let's say I'm having a file called, 'paramiko_download.txt'.
In this we have some content.
I need to download this file on my local server with the help of Python paramiko
Then simply syntax is, see that, 'sftp_client.get',
your remote server file with complete path.
Actually, that is a good practice.
Now, let me take complete path. '/home/ec2-user',
then this file name, right?
See that how I am going to write, source is, 'ec2-user/', your file.
Then on the local host,
so for 'get' you have a source file and then destination.
'get' means downloading a file from remote server.
Now suppose,
I am directly giving, 'paramiko_downloaded_file.txt'
See, I'm not giving complete path. Just I am giving the file name
in which you want to store this remote file content.
So, whenever if you run your Python script,
in which location this Python script is there,
there it is going to download.
Instead of that if you want to store in any required path,
then you have to provide complete path on your Windows Server.
Anyway, as of now I am running this code from my Windows server.
Right. See in our case, local server, remote server
and our remote server is Linux
and local server I am taking as Windows.
Any combination will work. Right.
Now see that. Let me save it and run it.
[no audio]
It's done. Let me go to open my location where we have the server,
I mean this script.
'paramiko_download_file.txt'.
'paramiko_downloaded_file.txt'.
So, this is the file just now we downloaded.
[No audio]
So guys, this is the way we can download.
Right. Now one more thing,
I want to download this file, but without mentioning this path.
See, sometimes you need to move into different location over 'sftp',
then there you have to download the file without mentioning the path.
Yes, we can do that also. Very simple. See that.
Before going to download your file,
what we have to do is, we need to move our 'sftp' connection,
or we need to change the location of your 'sftp_client' object
on your remote server by using 'chdir' operation.
So, now I am going to suppose, '/etc' location,
yeah, let me take simply '/home/ec2-user'.
Right. Now, let me simply print current location pointed by your 'sftp_client'.
So simply you can get current working directory with this syntax.
See the output.
[no audio]
Now your 'sftp' is pointing to this location.
So in this location whatever the file you are having, that file
without mentioning complete path, you can download
because already your 'sftp_client' object is pointing to this path,
that's why now I am going to download 'sftp_client.get'.
Let me take some other file here.
So, let's say I'm having 'demo.txt' file from 'ec2-file',
I won't to download that.
Directly on remote server I have a file called 'demo.txt',
that file I want to download it into let's say, desktop location.
[No audio]
Let me take complete path.
[no audio]
I want to download into desktop location.
let's say, 'downloaded file.txt', with this file name. And
be clear we are working with Windows,
I mean local server.
We are downloading our file to this Windows Server.
That's why whenever if you are mentioning a path,
it is always good to mention, '//' in your path
whenever if you are providing your path manually.
Let me save it and run it.
[No audio]
Yes, it has been done.
Let me go to my desktop location and see there.
Any file called, 'downloaded_file'?
Yeah, see that. That's it.
So, this is the way how we can mention complete path on your local server
or how to mention complete path on your remote server.
Without mentioning your complete path
if you want to download a file,
first change your 'sftp_client' pointing path on your remote server
by using change directory. That's it.
[No audio]
Right. So guys, this is the way how you can download.
Now, I want to transfer a file from local server to remote server.
It's very simple. Now let me comment all these things.
[No audio]
Suppose in the current location I have some file.
Let me transfer our Python script itself.
'sftp_client.put', on your remote server into this location.
[No audio]
Sorry, source is our local server, right.
So, let's say some, any script name called,
yeah, I will take this Python script only.
[no audio]
'transfer_files.py'. So on the current location we have this file.
So, this I want to move,
I mean I want to transfer to your remote server, into this location,
let's say 'transfer_files.py'. Let me run it and
see the result.
[no audio]
Done. Go to your remote server. Just list out.
See that. We got a file to this server. That's it.
So guys, this is the simple way to transfer your
file from local server to remote server,
or how to download a file from remote server to local server.
So, the very, very important point here is, whenever if
you are downloading your file,
then remote server file path is the source
and destination is on your local server.
Whenever if you are transferring,
then local server file is the source and remote server path is the destination.
And one more thing, on your remote server
if you don't want to mention complete path for your file,
then before going to download a file from your remote server,
try to point out your 'sftp_client' location to your required path.
Then from there you can download your required file,
so simply, by using 'chdir' operation.
[No audio]
Right. Okay guys.
Thank you for watching this video.
[no audio]