Several Methods for executing shell

Source: Internet
Author: User

Several Methods for executing shell

This section describes four methods for python to execute shell commands:

1. the OS. system () function in the OS module executes shell commands.

Note: This method cannot be output by shell commands.

2. popen () # The result of executing the command is a string. You need to process it yourself to obtain the desired information.

The result is the same as that of the first method.

3. commands Module # It is convenient to obtain command output (including standard and error output) and execution status bit.

Commands. getstatusoutput (cmd) returns (status, output)

Commands. getoutput (cmd) returns only output results

Commands. getstatus (file) returns the execution result string of the ls-ld file and calls getoutput. This method is not recommended.

4. subprocess Module

The subprocess module can be used to create a new process. It can connect to the input, output, and error pipelines of the new process, and obtain the return status of the new process execution. The subprocess module is used to replace OS. system () and OS. popen.(), Commands.And other old functions or modules.

Import subprocess 1, subprocess. call (command, shell = True) # The result is printed directly. 2. subprocess. popen (command, shell = True) ### it can also be subprocess. popen (command, stdout = subprocess. PIPE, shell = True) # You can output the result.

If the command is not an executable file, shell = True cannot be omitted.

Shell = True: Execute command in shell

You can execute shell commands in all four methods.

Pysftp-friendly command execution and interaction tools

  
  
  1. import pysftp with pysftp.Connection('hostname', username='me', password='secret') as sftp: with sftp.cd('public'):
  2. # temporarily chdir to public
  3. sftp.put('/my/local/filename') # upload file to public/ on remote
  4. sftp.get('remote_file')
  5. # get a remote file

All methods

  

  
  
  1. dir(pysftp.Connection) ['class', 'del', 'delattr', 'dict', 'doc', 'enter', 'exit', 'format',
  2. 'getattribute', 'hash', 'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof',
  3. 'str', 'subclasshook', 'weakref', 'set_authentication', 'set_logging', 'set_username', 'sftp_connect', '_start_transport',
  4. 'active_ciphers', 'active_compression', 'cd', 'chdir', 'chmod', 'chown', 'close', 'cwd', 'execute', 'exists', 'get', 'get_d',
  5. 'get_r', 'getcwd', 'getfo', 'isdir', 'isfile', 'lexists', 'listdir', 'listdir_attr', 'logfile', 'lstat', 'makedirs', 'mkdir',
  6. 'normalize', 'open', 'put', 'put_d', 'put_r', 'putfo', 'pwd', 'readlink', 'remote_server_key', 'remove', 'rename', 'rmdir',
  7. 'security_options', 'sftp_client', 'stat', 'symlink', 'timeout', 'truncate', 'unlink', 'walktree']
Https://bitbucket.org/dundeemt/pysftp official link

Download https://pypi.python.org/pypi/pysftp/

Http://pysftp.readthedocs.io/en/release_0.2.9/ Documentation Center

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.