In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to log on to the server remotely in Python? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
When writing some scripts using Python, in some cases, we need to log in to the remote service frequently to execute the command and return some results.
In a shell environment, this is what we do.
`
$sshpass-p ${passwd} ssh-p ${port}-l ${user}-o StrictHostKeyChecking=no xx.xx.xx.xx "ls-l"
Copy the code
`
In this issue, the editor pushes 2021 Python materials that will be used by beginners, including free books / videos / online documents and editor / source code compiled by the editor himself, and the installation qun:850973621 of `Python`
Then you will find that your output has a lot of information that you don't need but can't get away (maybe there is a way, please leave a message to communicate), like this.
`
Host: xx.xx.xx.xx, port: xx
Warning: Permanently added'[xx.xx.xx.xx]: xx' (RSA) to the list of known hosts.
Login failure: [Errno 1] This server is not registered to rmp platform, please confirm whether cdn server.
Total 4
-rw-r--r-- 1 root root 239 Mar 30 2018 admin-openrc
Copy the code
`
For those who directly use the shell command to execute the command, you can directly use the pipe or redirect the standard output to a file to obtain the result returned by executing the command.
# # 1\. Use subprocess
If we use Python to do this, we usually immediately think of using some commands such as os.popen,os.system,commands,subprocess to execute the library to get it indirectly.
But as far as I know, the output obtained by these libraries is not only standard output, but also standard error (that is, the redundant information above).
So we have to clean the data of output every time, and then organize and format it in order to get the data we want.
Take subprocess, for example, like this.
`
Import subprocess
Ssh_cmd = "sshpass-p ${passwd} ssh-p 22-l root-o StrictHostKeyChecking=no xx.xx.xx.xx'ls-l'"
Status, output = subprocess.getstatusoutput (ssh_cmd)
# data cleaning, formatted ones will not be displayed
Copy the code
`
Through the above text + code display, you can feel several major pain points of ssh login.
* * pain Point 1 * *: additional sshpass installation is required (if it is not secret-free)
* pain point 2 *: too much interference makes data cleaning and formatting quite troublesome
* pain Point 3 *: the code implementation is not elegant (a little corny) and its readability is too poor.
* * pain point 4 * *: ssh connections cannot be reused and can only be performed once at a time
* * pain point 5 * *: the code cannot be used on the whole platform and can only be used on Linux and OSX
In order to solve these problems, I searched the articles about Python ssh all over the network and did not see any full introduction of these skills.
To this end, I flipped through a very popular Github project: awesome-python-cn ([github.com/BingmingWon …] (https://github.com/BingmingWong/awesome-python-cn%EF%BC%89%E3%80%82)
Look forward to finding some useful libraries about remote connections here.
I really found two.
* sh.ssh
* Paramiko
# # 2\. Use sh.ssh
First, let's introduce the first one, `sh.ssh`.
`sh` is a library that allows you to complete Linxu/OSX system commands through function calls. It is very easy to use, and you can write an introduction to it if you have the opportunity.
`
$python3-m pip install sh
Copy the code
`
Today I will only introduce one of its functions: `ssh`
Usually two machines visit each other, for convenience, can be set secret-free login, so that there is no need to enter a password.
This code can realize secret-free login and execute our command `secret-l`.
`
From sh import ssh
Output=ssh ("root@xx.xx.xx.xx", "- p 22", "ls-l")
Print (output)
Copy the code
`
But it is possible that we do not want to set up mutual trust immunity, in order to make this code more general, I assume that we do not set it, we can only log in with a password.
Here comes the problem: if you want to enter a password, you have to use an interactive method to enter it. How to achieve it in Python?
The original ssh method receives a parameter `_ out`, which can be a string indicating the file path, a file object (or a class file object), or a callback function, which means that when there is a standard output, the output will be called to this function.
It's easy to do.
As long as I recognize the word 'password:', I will write my password to the standard input.
The complete code is as follows:
`
Import sys
From sh import ssh
Aggregated = ""
Def ssh_interact (char, stdin):
Global aggregated
Sys.stdout.write (char.encode ())
Sys.stdout.flush ()
Aggregated + = char
If aggregated.endswith ("password:"):
Stdin.put ("you_password\ n")
Output=ssh ("root@xx.xx.xx.xx", "- p 22", "ls-l", _ tty_in=True, _ out_bufsize=0, _ out=ssh_interact)
Print (output)
Copy the code
`
This is the official document ([amoffat.github.io/sh/tutorial...] (http://amoffat.github.io/sh/tutorials/interacting_with_processes.html?highlight=ssh%EF%BC%89%E7%BB%99%E7%9A%84%E4%B8%80%E4%BA%9B%E4%BF%A1%E6%81%AF%EF%BC%8C%E5%86%99%E7%9A%84%E4%B8%80%E4%B8%AAdemo%E3%80%82)
After trying to run, it is found that the program will always be running, will never return, will not exit, and the callback function will never enter.
After debugging and checking the source code, I still couldn't find the problem, so I went to [Github] (https://github.com/amoffat/sh/issues/393)) and found that the problem already existed in 2017, but it hasn't been fixed since 2020. It seems that not many people use `sh.ssh`, so I "ask" questions "again, hoping to get a reply.
[picture uploaded... (image-f1bf05-1610519266-2)]
The above problem occurs only when you need to enter a password, and there is no problem if you set up machine mutual trust.
In order to feel the effect of `sh.ssh`, I set the machine trust-free, and then use the following code.
`
From sh import ssh
My_server=ssh.bake ("root@xx.xx.xx.xx", "- p 22")
# is equivalent to executing the login one command at a time, and then exiting the login after execution
Print (my_server.ls ())
# you can log in to the server manually during sleep and use top to see how many terminals are currently connected
Time.sleep (5)
# when this command is executed again, the number of login terminals will be + 1, and after execution, it will be-1
Print (my_server.ifconfig ())
Copy the code
`
It is surprising to find that using `bake`, `bake` and `my_server.ifconfig () `seem to execute commands twice through the same ssh connection, but in fact, you can see the changes of connected terminals by executing top commands on remote machines, first` + 1` and then `- 1`, indicating that the execution of the two commands is achieved through two connections.
From this point of view, the use of `sh.ssh` can solve pain point 1 (if the above problems can be solved), pain point 2, pain point 3.
But it is still unable to reuse ssh connections, it is still not very convenient, and it is not my ideal best solution.
The most important thing is that the `sh` module only supports Linxu/OSX. In Windows, you have to use its brother library-`pbs`, and then I went to pypi to have a look at [pbs] (https://pypi.org/project/pbs/), which is "out of repair" and no one has maintained it.
[picture uploaded... (image-1fa764-1610519265-1)]
At this point, from the "pawn", I am short of the last straw.
# # 3\. Use paramiko
With the last glimmer of hope, I tried to use the library `paramiko`, and finally found the elegance that should belong to Python here.
You can install it with the following command
`
$python3-m pip install paramiko
Copy the code
`
Then, several commonly used methods of ssh login are introduced.
# method 1: login with sshclient based on username and password
Then you can refer to the following code to make a remote connection under the Linux/OSX system
`
Import paramiko
Ssh = paramiko.SSHClient ()
# allow connections to hosts that are not in the know_hosts file
Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())
# establish a connection
Ssh.connect ("xx.xx.xx.xx", username= "root", port=22, password= "you_password")
# use this connection to execute commands
Ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command ("ls-l")
# get output
Print (ssh_stdout.read ())
# close the connection
Ssh.close ()
Copy the code
`
# method 2: login with transport based on user name and password
Method 1 is a traditional operation of connecting to the server, executing commands, and closing. Multiple operations require multiple connections and cannot be reused [* pain point four *].
Sometimes you need to log in to the server to perform multiple operations, such as executing commands and uploading / downloading files, but method 1 cannot be implemented, so you can use the transport method.
`
Import paramiko
# establish a connection
Trans = paramiko.Transport ("xx.xx.xx.xx", 22))
Trans.connect (username= "root", password= "you_passwd")
# specify the transport of the object of sshclient as the above trans
Ssh = paramiko.SSHClient ()
Ssh._transport = trans
# the rest is the same as above
Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())
Ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command ("ls-l")
Print (ssh_stdout.read ())
# close the connection
Trans.close ()
Copy the code
`
# method 3: login with SSHClient based on public key
`
Import paramiko
# specify the local RSA private key file
# if a password is set when establishing a key pair, password is the password set. If you do not need to specify the password parameter
Pkey = paramiko.RSAKey.from_private_key_file ('/ home/you_username/.ssh/id_rsa', password='12345')
# establish a connection
Ssh = paramiko.SSHClient ()
Ssh.connect (hostname='xx.xx.xx.xx'
Port=22
Username='you_username'
Pkey=pkey)
# execute command
Stdin, stdout, stderr = ssh.exec_command ('ls-l')
# the result is put in stdout, and if there are any errors, it will be put in stderr.
Print (stdout.read ())
# close the connection
Ssh.close ()
Copy the code
`
# method 4: login based on Transport key
`
Import paramiko
# specify the local RSA private key file
# if a password is set when establishing a key pair, password is the password set. If you do not need to specify the password parameter
Pkey = paramiko.RSAKey.from_private_key_file ('/ home/you_username/.ssh/id_rsa', password='12345')
# establish a connection
Trans = paramiko.Transport (('xx.xx.xx.xx', 22))
Trans.connect (username='you_username', pkey=pkey)
# specify the transport of the object of sshclient as the above trans
Ssh = paramiko.SSHClient ()
Ssh._transport = trans
# execute commands, just like the traditional method
Stdin, stdout, stderr = ssh.exec_command ('df-hl')
Print (stdout.read () .decode ())
# close the connection
Trans.close ()
Copy the code
`
The above four methods can help you to remotely log in to the server to execute commands. If you need to reuse the connection, you can use * * method 2 * * and * * method 4 * * if you need to reuse the connection to execute multiple commands.
Remember to close the connection after you use it.
# transfer sftp files
At the same time, paramiko as the perfect solution for ssh, it is very professional, it can also be used to achieve sftp file transfer.
`
Import paramiko
# instantiate a trans object # instantiate a transport object
Trans = paramiko.Transport (('xx.xx.xx.xx', 22))
# establish a connection
Trans.connect (username='you_username', password='you_passwd')
# instantiate a sftp object and specify the connection channel
Sftp = paramiko.SFTPClient.from_transport (trans)
# send a file
Sftp.put (localpath='/tmp/11.txt', remotepath='/tmp/22.txt')
# download files
Sftp.get (remotepath='/tmp/22.txt', localpath='/tmp/33.txt')
Trans.close ()
Copy the code
`
At this point, Paramiko has won a complete victory, but there is still a pain point that we have not mentioned, that is, multi-platform, that is, Windows, there is a good thing and a bad thing.
The good thing is: paramiko supports windows
The bad thing is: you need to do a lot of complicated preparation, you can google to solve, but I suggest you just give up, the hole is too deep.
[picture uploaded... (image-1cb938-1610519263-0)]
This is the answer to the question about how to log on to the server remotely in Python. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 216
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.