In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to execute Linux scripts and commands remotely". In daily operation, I believe many people have doubts about how to remotely execute Linux scripts and commands. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to execute Linux scripts and commands remotely". Next, please follow the editor to study!
If I now need to execute a series of commands on the Linux server (such as building a LNMP environment), I should immediately think of a way to write a Shell script, and then throw it to execute the following results.
However, as I have always been lazy, I don't want to execute Shell and repeat commands in this way. So I thought there was a way to execute scripts locally and directly on the server side to create heresy. At this time, some big tech bully told me that there was a Python library called paramiko, which opened the door to my new world.
For paramiko installation of direct pip or PyCharm here will not say much, if you see here you feel that you do not know python syntax do not have to worry, you can simply use paramiko to execute Shell commands to view the results and upload and download files, eliminating repetitive work.
Paramiko implements the SSHv2 protocol (underlying uses cryptography) and contains two core components: SSHClient and SFTPClient. SSHClient is the encapsulation of SSH session, which is used to execute remote commands, and SFTPClient is the encapsulation of SFTP client to realize remote file operation.
Here are two examples, you should know how to use it, and finally start the positive film.
The columns of SSHClient:
#-*-coding: utf-8-*-
Import paramiko
Client = paramiko.SSHClient () # instantiate SSHClient
Client.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) # automatically adds policies to save the hostname and key information of the server. If not, hosts that are no longer recorded in the local know_hosts file will not be able to connect
Client.connect (hostname='192.168.23.134', port=22, username='ftoz', password='123456') # connects to the SSH server and authenticates with a user name and password
# Open a Channel and execute the command
Stdin, stdout, stderr = client.exec_command ('ls') # stdout is the correct output, stderr is the error output, and 1 variable has a value
# print execution result
Print (stdout.read () .decode ('utf-8'))
# turn off SSHClient
Client.close ()
Output:
Here's an explanation:
Client = paramiko.SSHClient ()
Client.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) you can understand as a fixed posture.
Client.connect (hostname='192.168.1.105', port=22, username='ftoz',password='123456') here your linux variables are address, port (65535 ports in total, but ssh is 22 by default), login name, password.
Stdin, stdout, stderr = client.exec_command ('df-h') here is the core shell command you need to do, these three variables do not have to follow this posture, you can do whatever you want, but in order you know what data is in it (focus on output and errors).
Connect (): this is used to connect and authenticate remote servers. The parameters are:
Target host for hostname connection
Port=SSH_PORT designated port
User name authenticated by username=None
User password authenticated by password=None
Pkey=None private key is used for authentication
Key_filename=None A file name or list of files that specifies the private key file
Timeout=None optional tcp connection timeout
Allow_agent=True, whether to allow connection to the ssh proxy. Default is True allow.
Whether look_for_keys=True searches for private key files in ~ / .ssh. Default is True allowed.
Compress=False, whether to turn on compression.
Set_missing_host_key_policy (): this is to set the response policy when the remote server is not recorded in the know_hosts file. (it can be understood to avoid reporting errors), the parameters are:
AutoAddPolicy automatically adds the host name and host key to the local HostKeys object, which does not depend on the configuration of load_system_host_key. That is, there is no need to enter yes or no for confirmation when a new ssh connection is established.
WarningPolicy is used to record an python warning of an unknown host key. And accept, functionally similar to AutoAddPolicy, but will prompt for a new connection
RejectPolicy automatically rejects unknown hostnames and keys, depending on the configuration of load_system_host_key. This is the default option
Exec_command (): this is to write the command you need to execute.
Then you can take out the output and do some ke and pa things. Here is a simple list.
Common methods of SFTPClient:
T = paramiko.Transport (('192.168.23.134, 22)) # get Transport instance
T.connect (username='ftoz', password='123456') # connects to the SSH server and uses password
Sftp = paramiko.SFTPClient.from_transport (t)
Sftp.put ("F:\ S12312.txt", "/ home/ftoz/zxc12312.txt") # perform upload action
Sftp.get ("/ home/ftoz/zxc12312.txt", "F:\ S12312.txt") # performs the download action
T.close ()
As a client object of sftp, SFTPCLient implements remote file operations, such as upload, download, permissions and status, according to the sftp session of ssh transport protocol.
From_transport (cls,t) creates a connected SFTP client channel
Put (localpath, remotepath, callback=None, confirm=True) uploads local files to the server parameter confirm: whether to call the stat () method to check the file status and return the result of ls-l
Get (remotepath, localpath, callback=None) downloads files from the server to local
Mkdir () creates a directory on the server
Remove () deletes the directory on the server
Rename () renames the directory on the server
Stat () to view server file status
Listdir () lists the files in the server directory
Finally, form the good habit of closing with client.close ().
At this point, the study on "how to execute Linux scripts and commands remotely" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 0
*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.