In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to remotely execute Linux scripts and commands, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
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 add policies to save the hostname and key information of the server, if you do not add Then hosts that are no longer recorded in the local know_hosts file will not be able to connect to client.connect (hostname='192.168.23.134', port=22, username='ftoz', password='123456') # connect to the SSH server, authenticate with username and password # open a Channel and execute the command stdin, stdout, stderr = client.exec_command ('ls') # stdout as the correct output, and stderr as the error output At the same time, there is a variable with 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 implements remote server connection and authentication. The parameters are: hostname connection target host port=SSH_PORT specified port username=None authenticated user name password=None authenticated user password pkey=None private key method for authentication key_filename=None a file name or file list, specify the private key file timeout=None optional tcp connection timeout allow_agent=True, whether to allow connection to the ssh agent The default is True to allow look_for_keys=True to search for private key files in ~ / .ssh, True to allow compress=False by default, and 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. The parameters include: 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, when a new ssh connection is established, it is no longer necessary to enter yes or no to confirm the python warning that WarningPolicy is used to record an unknown host key. And accept, functionally similar to AutoAddPolicy, but will prompt that the 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') # connect to SSH server Use password sftp = paramiko.SFTPClient.from_transport (t) sftp.put ("F:\ S12312.txt", "/ home/ftoz/zxc12312.txt") # to execute the upload action sftp.get ("/ home/ftoz/zxc12312.txt", "F:\ S12312.txt") # perform 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) to upload local files to the server parameter confirm: whether to call the stat () method to check the file status Return the result of ls-l get (remotepath, localpath, callback=None) download files from the server to the local mkdir () create a directory on the server remove () delete the directory on the server rename () rename the directory on the server stat () check the server file status listdir () list the files under the server directory above is "how to remotely execute Linux scripts and commands" all the contents of this article, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.