擅长:python、mysql、java
<p>您可以使用<code>paramiko</code>,这是一个python <a href="http://www.lag.net/paramiko/" rel="noreferrer">library</a>,它通过ssh抽象出远程shell连接,有几个选项允许用户使用rsa密钥等进行身份验证。这是一个示例代码,您可以重用它来解决您的问题:
<br/><br/></p>
<pre><code>import paramiko
ssh = paramiko.SSHClient()
ssh.connect( 'hostname', username = 'username', password = 'password' )
ssh.exec_command( 'ls -al' )
</code></pre>
<p><br/>
顺便说一下,如果从虚拟环境(<a href="http://www.virtualenv.org/en/latest/" rel="noreferrer">virtualenv</a>)运行脚本,paramiko可以很容易地添加到python环境中。</p>