Friends here
our concept is 'Working with remote Linux Server from Linux
or Windows as your localhost by using paramiko of Python'.
See you're having one remote server, and there is a Linux
server, and you are having a local server and this local
server maybe windows or maybe Linux. And from here
I need to work with your remote server by using paramiko
of Python. Right.
This is our concept here.
But before going to work with that, let me give some introduction
to your Paramiko Module.
See first of all Paramiko is your Python module or Library,
which is useful to work with your remote servers. See working
with your remote servers means sometimes you need to execute
some commands on your remote server without logging into
that from your local server, or sometimes you need to transfer a
file from local server to remote server, or sometimes you
need to download a file from remote server to local server.
So all these actions we can perform with the help of Python
Paramiko Module, but before going to use that you should
have a SSH or SSH V2 protocol on your local server, or simply
from where you are running your Python code
there you should have, on that operating system
you should have SSH V2 protocol. That's it.
And guys we know that by default we have SSH protocol on
your Unix like systems.
If your local server is Windows you need to check for your
SSH. If it is not there you have to install it.
Right. That's fine.
Then how it is going to work? How your Python Paramiko
Module is going to work?
See your Python Paramiko Module is going to create
a SSH client first with the help of your SSH 2 protocol,
I mean SSH V2 protocol. And by using that it is going to connect
with your remote servers and it is going to work.
Working means executing commands and transferring files.
That's fine. Now for authentication purpose your paramiko
is using two ways. One is using 'username' and 'password', and
second one is using 'username' or your 'keys',
I mean 'username' and your 'keys'.
[no audio]
And guys finally the last point for your introduction is
we can connect from Linux to Linux, or Linux to Windows, or
Windows to Linux, or Windows to Windows using your
Paramiko Module.
So we have four combinations, right?
Let me write all combinations here. Suppose local,
let me write here in this way. "Local server", then your
"Remote server".
So local server maybe Windows, and your remote server,
let me give all the combinations - Windows servers,
or local server is Windows and remote servers maybe Linux
servers. Now local server, maybe Linux or Windows Server
maybe Linux, or it is Linux and Windows,
remote server is Windows.
[no audio]
So these four combination will works with your Paramiko
Module, and here for time being we are going to work with
by conserving your local server as Windows or Linux and your
remote server is only Linux.
Right. So first I am going to consider
local server as Linux
[no audio]
and remote server also Linux.
[no audio]
Second we will work by considering local as Windows and remote
server is Linux. That's fine.
Now guys before going to use your paramiko
the first thing is you should have a SSH on your localhost.
Let me check. See suppose
this is my Local server.
[no audio]
from my VMware.
Let me remove this. Fine. Now see here.
[no audio]
Yes, I have SSH. Then no problem.
We have already SSH, then we can work with your Python paramiko.
And one more thing just check it whether we have paramiko or not.
So here I have paramiko.
Anyway, this is not the default module
so if it is not there on your host, then you need to install.
See let me show you on my Windows suppose
see I tried to 'import paramiko'. Here
there is no name called 'paramiko', I mean "No module named
paramiko". Then that's why what I did here is
simply I installed with the help of 'pip', 'pip install paramiko'.
Same thing you can follow on your Linux system as well to install your
paramiko. That's fine.
So after installing I imported, then there is no error. That's fine.
So if it is not there you have to install your Paramiko
Module. So I am giving you simply here steps, 'pip install paramiko'
to install your Paramiko Module. That's fine.
Then second thing is, how your,
yeah, now let me start with code to use your paramiko to work
with your remote servers.
Let me write a simple code here.
Sorry, not this, this is my local server.
Let me exit from here.
[no audio]
'working_with_remote_linux', maybe any server, remote, that
that actually any server, you can work with any remote server.
Of course, but time being we are going to work with only remote
server as Linux. And one more thing, let me take
my 'python' path,
[no audio]
see that. The very first thing, you need to
import Paramiko Module.
[no audio]
Right. That's fine.
So after that your paramiko is going to work with your
remote servers by creating a client.
Actually, we need to create that client.
So to create your client, this is the syntax. So here
[no audio]
guys, I will give all these steps on the below of this video,
then you can go with that and you can practice. So this is
simply any variable, but this is your client, 'SSHClient'.
Simply you can assume 'ssh' of your operating system.
Now I created from paramiko this 'SSHClient'. Be clear
this is a variable, you can take any name, but I have
taken suitable name.
So after that
let me try to connect with my remote server.
So to connect with your remote server
we have a syntax called,
I mean anyway we have two procedures -One is using 'username'
and 'password', second one is using 'username' and 'keys'.
So for time being I am going to use 'username' and 'password'.
So let me take my remote server 'hostname' first.
So guys, this is the syntax simply, 'ssh.connect', your remote
server 'hostname', with what user and what is the password, and
by default it is going to use port 22 only, you no need to
mention, but if you want to mention you can mention it. Now
let me open my remote server.
[no audio]
So for this remote server, this is a public IP,
no, I mean 'hostname'.
You can also mention in your real-time FQDN name of your server.
That's fine. 'username' is this one. And I have given already 'password' as
'paramiko123' I think. Let me check it. 'paramiko123'.
Let me save it and run this. So before going to run
let me provide execution permission because we are on Linux.
[no audio]
See that, you are getting an error called
'paramiko.ssh_exception',
and if you observe here, "not found in known hosts". See whenever
if you try to connect with any remote server using your SSH at
very first time, so let me show you that, 'ssh' suppose
this is my 'hostname',
see it is going to ask 'yes/no' permission, right?
So manually you are entering here,
but in Python script we are not entering that 'yes/no' option.
If you say 'yes' then only you can connect. If you say 'no' you
are going to reject.
Now if you don't want to ask this, right, in your SSH you are
providing some strict host key checking as 'no'.
[no audio]
Right. But in Python how we can avoid that checking this, host key checking,
right? Simple.
You need to add a 'AutoAddPolicy' to avoid that.
I don't want to check 'yes/no' confirmation. By default
I want to work with that given server.
So for that you need to add this line before going to connect
with your remote server. That's it.
[no audio]
Right. Now if I run this, now you're not going to get any
error. Just let me run this and see the result.
That's it. You're not getting any error means now your script has been
connected with the, I mean through script you have been connected
with your remote server, this server.
So guys the connection, whatever the connection you established
with your remote server will be there with this 'ssh' object
only. Now to execute any commands, or to transfer a file, for
all those things you have to use this SSH.
Now, let me use this to execute some commands.
So simply to execute your commands
you have to write 'ssh.',
yeah, one more thing. You can write, I mean you can execute
any command here, 'exec', execute, 'command', but before that
if you are running some commands on your remote server sometimes
you may need some input to provide that, I mean to provide
input for that command, or some, forget about input, sometimes
you're going to get success output, sometimes you're going
to get error output while running your command, while executing
your command on your remote server. That's why here we are
having, to execute our command this is the syntax. You can
take any command. Suppose I am running, see guys
I am using 'ssh' to execute our commands on your remote server.
[no audio]
Right. And if you observe here, this command, I mean after
running this command you may get some success output. If you
got success that is going to store into this variable.
If you get an error while running this command that output
will store into this. If you get output, error is null, empty.
If you get error, output is null. Then what is the use of this
'stdin'. See while running your commands on your remote server
sometimes that command may need input.
Let's say you are running some 'sudo' command,
and 'sudo' command
sometimes may need password, that password we can provide with
the help of 'stdin'.
Guys working with 'stdin' we will see in the later
videos, as of now
I am going to take only 'stdout' and 'stderr'. Nothing is there.
I am running my command in this way.
Then let me run first very simple command. Suppose
I want to get, 'whoami'?
First let me give invalid command.
Right. See actually my intention is I'm trying to find with
what user we connected.
We know that we connected with this user only. That output
actually we are expecting. But this is a invalid command
that's why you're going to get an error. That error will be
there in this. And these two 'stdout' and 'stdin', they are like
file objects.
So if you know, if you remember how to read a content from
file object, by using '.read', or '.readlines'. Let me write
first. "The output is: ", in case if you have a output, and let me print.
[no audio]
"The error is: ". Now I am going to print here the success output
in case if we have that output, 'stdout.read'.
Then in case if you get an error that we are going to print
here, 'stderr.read'
Let me save it.
And let me run this.
See the output. What happened? "invalid syntax". 'print(stderr.read()))'
Yeah, we have given one extra parentheses I think. Now it's fine.
See, output you are getting an empty but error you
are getting something because the command whatever we have
given that is invalid command. That's why it's saying, "command
not found". That's why we are getting an error. And if you observe
you're also getting an empty, empty line because this is the
file content, right. If you read a file content by default
for each and every line at the end you have a '\n', so
for this line also you're having at the end '\n', and that's
why we are getting this. Now you can do something. Not only 'read'
you can also write, 'readlines' information.
[no audio]
But if you remember while working with files, 'readlines' is
a list of values.
I mean simply your file content will be converted into number
of values in your list.
Each line is one value in your list.
[no audio]
That's it. See that '\n' is there.
[no audio]
Right. Anyway, that's fine. Based on your requirement
you can go with 'read' or 'readlines'.
Now, let me give a valid command.
Let's say 'whoami'. 'whoami', now this is a valid command, right?
Now see the result.
[no audio]
Easy to user. But output is, I mean error output is empty. Because
you got success output that’s why error is empty, right?
Guys forget about input 'stdin', but syntax is you have to
use 'stdin, stdout, stderr'.
That's fine. Now, let me run one more command.
You can run any command.
Let me comment this.
[no audio]
Let me write same syntax.
[no audio]
Suppose I am running,
let's say 'uptime'.
Save it, and run it, and see the result.
[no audio]
That's it.
Right. So not only that,
let me execute one more thing like 'free - m'.
[no audio]
See the result.
[no audio]
That's it.
[no audio]
Right. Yeah, what you are getting here?
[no audio]
Okay, 'stdout.readline', not 'lines', right?
So that's why you got a, I mean you're not getting perfect
output. 'readlines' is going to give all the lines from your
content. So previously I used only 'readline'.
Now see that, 'readlines'.
You're going to get complete output for your 'free -m'.
That's fine. So guys, this is the simple way to execute your commands
on your remote server.
And here we are using 'username' and 'password'. And one more
thing, our local server is Linux and remote server is also
Linux. Now, let me execute same thing with the help of 'keys'.
[no audio]
First let me comment this,
working with your 'username' and 'password'.
So if you want to work with 'keys', very simple.
[no audio]
First thing, this is the syntax.
[no audio]
Right. Simply your 'hostname', 'username', instead of 'password'
we are going to use 'key_filename', then port, that is your
wish, if you want to use you can use it
otherwise you can also skip that. No problem.
And one more thing. What
is this 'key'? From where you can get this 'key'?
[no audio]
Right. See that.
[no audio]
See, you can create your 'keys' using 'ssh', and you can exchange your 'key'
then the private key path you have to mention in this file,
I mean in your code.
The private 'key_filename' you have to mention here.
Let me do one thing.
I'm going to do, '.ssh'.
[no audio]
Let me, no files here, right?
Let me create some keys.
'ssh-keygen'.
So I am going to create my SSH key values,
I mean keys. See that. We have private key and public key.
First we have to share this key with your suppose your remote server.
Let me share that with my remote server.
[no audio]
What is the remote server IP?
Let me get it.
[no audio]
And be clear, here I am sharing
[no audio]
with 'ec2-user'. 'ec2-user'
I'm sharing my key.
[no audio]
Now 'ec2-user'. Sorry
what is the 'password' we've given for that? 'paramiko123'.
[no audio]
That's it.
Now, what is your private key?
This is your private key. In this part
we have your private key.
[no audio]
Simply.
[no audio]
Right. Now this file we have to use in our code. That's it.
[no audio]
So in case by default anyway you are having now password-less
authentication, you can use it.
But if you want to use key or just provide your 'key_filename'
path here completely. If your key is there in the current
location directly
you can write your only key name.
[no audio]
That's it. Now,
let me save it and run it. Be clear now
we are using username and your cryptographic key, SSH private
key, right. Now,
Let me run,
and see the result.
[no audio]
What happened?
[no audio]
'Authentication failed'.
Oh I'm using 'ansadmin' user. That's not correct.
[no audio]
Yeah.
Username is 'ec2-user'. With 'ec2-user'
we exchanged our key, right. So be clear about user and
your keys.
[no audio]
Yeah, now let me run this and see the result.
[no audio]
That's it.
So guys this is the way how we can work with 'username' and 'password',
and how we can work with your 'SSH keys'
using your paramiko, okay.
I will share this key below this video just go through that.
And just now we worked with Linux with Linux. Now,
my requirement is I want to work from my local server, from
sorry, my local server as Windows. Let's say this is my
Windows Server, right?
[no audio]
Now, let me write a new code on my Windows Server, save it.
[no audio]
Some key, some script name as,
'working_with_remote_Linux_from_windows.py'
And before going to work with that, you have to check it.
Do we have SSH? Yes. On my Windows
I already installed SSH. If it is there, okay.
If it is not there, please install them first.
Right. Then after that
let me take step by step.
We know we are already having.
First we need to install, we need to install 'paramiko',
then we have to import that in our code, then we need to add,
we need to create SSH client, then 'AutoAddPolicy'.
[no audio]
Just now I added 'AutoAddPolicy'. Just to avoid yes or
no confirmation like that, right. Then after that you need
to connect with your server.
So to connect with your server
[no audio]
this the syntax,
[no audio]
'ssh.connect' your 'hostname',
maybe this is 'hostname'.
Let me verify it.
392, yeah, 'hostname'. Then 'username' is as of now
we are having our username as 'ec2-user'. Then we are not
using 'key_filename'.
So what we are using? We are using simply 'password'.
Right. So what is the 'password'? Did we mention here? No.
Yeah, 'password' is 'paramiko123'.
Save it. First let me run this. If it is fine
it won't give any exception.
[no audio]
Yeah, it's fine. That's why
it has been executed without any errors. Now
I want to execute some command, any command you can execute
on your remote server.
So guys now I'm working with from my local server.
[no audio]
Yes. Yeah, let me print the output, then only we can see the output,
right. 'stdout.'. Sorry not 'stderr', 'stdout.read', maybe 'readlines'
That's fine.
[no audio]
That's it.
[no audio]
You're getting your output, right?
That's fine. Now on Windows,
I mean from my local server as Windows
I connected with my remote server Linux, and I executed
some command with the 'username' and 'password', now if you want
to work with some keys,
yes, you can also generate your keys on your local server
the same way how we worked on Linux server.
So in Linux server, I generated some keys and I shared with
my remote server, and I then used this in this way, right. The
same way on Windows
also you can generate your keys and you can work with your
remote server from your local server as Windows. That's it.
[no audio]
Guys, not only this, this is just basic way to execute your
commands on your remote server, but while working with your
remote servers you will get different exceptions and you
need to handle all of them.
So how to handle, we'll discuss in later videos, and how to
transfer files from your local server to Windows servers
all those things we are going to discuss in later videos.
Okay.
Yeah, okay guys. Thank you for watching this video.
[no audio]