Python Network Programming 简明教程
Python - SSH
SSH 或安全套接字外壳层是一种网络协议,可提供一种安全的方式来访问远程计算机。安全外壳在连接到不安全网络(如 Internet)的两台计算机之间提供强身份验证和安全加密数据通信。网络管理员广泛地使用 SSH 来远程管理系统和应用程序,允许他们通过网络登录到另一台计算机,执行命令并将文件从一台计算机移动到另一台计算机。
SSH or Secure Socket Shell, is a network protocol that provides a secure way to access a remote computer. Secure Shell provides strong authentication and secure encrypted data communications between two computers connecting over an insecure network such as the Internet. SSH is widely used by network administrators for managing systems and applications remotely, allowing them to log in to another computer over a network, execute commands and move files from one computer to another.
随着云服务器变得更实惠,SSH 是在云服务器上执行各种任务的最常用工具。我们需要它进行;
AS cloud servers become more affordable, SSH is the most commonly used tool to perform various tasks on cloud server. We need it for &;minus
-
Setup a web server for a client’s website
-
Deploy source code to a production server
在 Python 中,通过使用名为 fabric 的 Python 库来实现 SSH。它可用于通过 SSH 远程发出命令。
In python SSH is implemented by using the python library called fabric. It can be used to issue commands remotely over SSH.
Example
在以下示例中,我们连接到一个主机并发出命令来识别主机类型。我们会捕获结果,并将它作为格式化文本显示出来。
In the below example we connect to a host and issue the command to identify the host type. We capture the result in and display it as a formatted text.
from fabric import Connection
result = Connection('xyz.com').run('uname -s')
msg = "Ran {.command!r} on {.connection.host}, got stdout:\n{.stdout}"
print(msg.format(result))
当我们运行以上程序时,我们得到以下输出:
When we run the above program, we get the following output −
Linux
这是一个示例结果,它将取决于服务器。
This is a sample result which will depend on the server.