我正在编写一个Python脚本来自动化一些命令行命令。目前,我正在执行类似以下的调用:
cmd = "some unix command"
retcode = subprocess.call(cmd,shell=True)
然而,我需要在远程机器上运行一些命令。手动操作时,我会使用ssh
登录,然后运行命令。如何在Python中自动化这个过程?我需要使用已知的密码登录到远程机器,所以我不能只使用cmd = ssh user@remotehost
,我想知道是否有适合此目的的模块?
我会向你推荐 paramiko
请查看这个问题
ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)
如果您正在使用SSH密钥,请执行以下操作:k = paramiko.RSAKey.from_private_key_file(keyfilename)
# OR k = paramiko.DSSKey.from_private_key_file(keyfilename)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, pkey=k)
def run_cmd(host, cmd):
来实现你想要的功能,但随着你的需求不断发展,你最终需要为新的用例更改帮助函数,从而改变了旧有用法的行为。因此请做好相应的计划。 - Scott Prive保持简单。不需要任何库。
import subprocess
# Python 2
subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd='ls -l'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
# Python 3
subprocess.Popen(f"ssh {user}@{host} {cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
f"echo {password} | ssh {user}@{host} {cmd}"
。当然,只有在提示用户时才是安全的。 - Gulzar或者你可以直接使用commands.getstatusoutput:
commands.getstatusoutput("ssh machine 1 'your script'")
subprocess.check_output
。commands
模块已经在Python 3中被移除。请使用subprocess
模块,正如另一个答案中所提到的那样。 - Dylan Hogg我发现 paramiko 过于底层,而 Fabric 不太适合用作库,因此我编写了自己的库称为 spur,它使用 paramiko 实现了一个稍微更好的接口:
import spur
shell = spur.SshShell(hostname="localhost", username="bob", password="password1")
result = shell.run(["echo", "-n", "hello"])
print result.output # prints hello
如果你需要在 shell 中运行:
shell.run(["sh", "-c", "echo -n hello"])
spur
。你可以生成额外的 shell 命令,最终得到:which 'mkdir' > /dev/null 2>&1 ; echo $?; exec 'mkdir' '-p' '/data/rpmupdate/20130207142923'。我也想要访问一个普通的 exec_command
。还缺少运行后台任务的能力:nohup ./bin/rpmbuildpackages < /dev/null >& /dev/null &
。例如,我使用模板生成一个 zsh 脚本(rpmbuildpackages),然后只需在机器上让它保持运行状态。也许监视这些后台作业的能力也很不错(将 PID 保存在某个 ~/.spur 文件中)。 - davidltSshShell
时,现在有设置shell类型的选项。如果使用最小化shell通过传递shell_type=spur.ssh.ShellTypes.minimal
,那么只会发送原始命令。直接实现后台任务可能有点超出Spur的范围,但是您应该能够通过调用shell来运行您描述的命令,例如 shell.run(["sh", "-c", "nohup ./bin/rpmbuildpackages < /dev/null >& /dev/null &"])
。 - Michael Williamson所有人已经提到(推荐)使用 paramiko ,我只是分享一个Python代码(可以称之为API),它将允许您一次执行多个命令。
要在不同的节点上执行命令,请使用:Commands().run_cmd(host_ip,list_of_commands)
您会看到一个TODO,我保留它以防止任何命令执行失败时停止执行,我不知道该如何实现,请分享您的知识。
#!/usr/bin/python
import os
import sys
import select
import paramiko
import time
class Commands:
def __init__(self, retry_time=0):
self.retry_time = retry_time
pass
def run_cmd(self, host_ip, cmd_list):
i = 0
while True:
# print("Trying to connect to %s (%i/%i)" % (self.host, i, self.retry_time))
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host_ip)
break
except paramiko.AuthenticationException:
print("Authentication failed when connecting to %s" % host_ip)
sys.exit(1)
except:
print("Could not SSH to %s, waiting for it to start" % host_ip)
i += 1
time.sleep(2)
# If we could not connect within time limit
if i >= self.retry_time:
print("Could not connect to %s. Giving up" % host_ip)
sys.exit(1)
# After connection is successful
# Send the command
for command in cmd_list:
# print command
print "> " + command
# execute commands
stdin, stdout, stderr = ssh.exec_command(command)
# TODO() : if an error is thrown, stop further rules and revert back changes
# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
# Only print data if there is data to read in the channel
if stdout.channel.recv_ready():
rl, wl, xl = select.select([ stdout.channel ], [ ], [ ], 0.0)
if len(rl) > 0:
tmp = stdout.channel.recv(1024)
output = tmp.decode()
print output
# Close SSH connection
ssh.close()
return
def main(args=None):
if args is None:
print "arguments expected"
else:
# args = {'<ip_address>', <list_of_commands>}
mytest = Commands()
mytest.run_cmd(host_ip=args[0], cmd_list=args[1])
return
if __name__ == "__main__":
main(sys.argv[1:])
被接受的答案对我没用,这是我使用的替代方案:
import paramiko
import os
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# ssh.load_system_host_keys()
ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
ssh.connect("d.d.d.d", username="user", password="pass", port=22222)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("ls -alrt")
exit_code = ssh_stdout.channel.recv_exit_status() # handles async exit error
for line in ssh_stdout:
print(line.strip())
total 44
-rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc
-rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc
-rw-r--r--. 1 root root 176 Dec 28 2013 .bashrc
...
或者,您可以使用sshpass:
import subprocess
cmd = """ sshpass -p "myPas$" ssh user@d.d.d.d -p 12345 'my command; exit' """
print( subprocess.getoutput(cmd) )
参考资料:
注意事项:
ssh root@ip
),并接受公共密钥,这往往是无法使用paramiko
或其他自动化ssh
脚本进行连接的原因。paramiko 终于在我添加了关于允许缺失主机密钥策略的额外行后成功运行,这是非常重要的一步(第5行):
import paramiko
p = paramiko.SSHClient()
# This script doesn't work for me unless the following line is added!
p.set_missing_host_key_policy(paramiko.AutoAddPolicy())
p.connect("server", port=22, username="username", password="password")
stdin, stdout, stderr = p.exec_command("your command")
opt = stdout.readlines()
opt = "".join(opt)
print(opt)
请确保已安装paramiko包。 解决方案的原始来源:源
首先:我惊讶地发现还没有人提到 fabric
。
其次:为了满足你所描述的这些要求,我实现了一个名叫 jk_simpleexec
的 Python 模块。它的目的是让运行命令更加容易。
让我简单地为您解释一下。
我的 Python 模块 jk_simpleexec
提供了一个名为 runCmd(..)
的函数,可以在本地或远程执行 shell 命令。这非常简单。以下是本地执行命令的示例:
import jk_simpleexec
cmdResult = jk_simpleexec.runCmd(None, "cd / ; ls -la")
注意:默认情况下,返回的数据会自动裁剪,以删除STDOUT和STDERR中多余的空行。(当然,此行为可以被禁用,但是针对您的目的,正是这种行为您想要的。)
您将收到一个包含返回代码、STDOUT和STDERR的对象。因此,处理结果非常容易。
在执行的命令可能存在并被启动,但可能无法完成其预期操作的情况下,这就是您想要做的。在最简单的情况下,如果您不关心STDOUT和STDERR,则您的代码可能看起来像这样:
cmdResult.raiseExceptionOnError("Something went wrong!", bDumpStatusOnError=True)
出于调试目的,您希望在某个时间将结果输出到 STDOUT,因此您可以这样做:
cmdResult.dump()
如果您想处理标准输出STDOUT,这同样也很简单。例如:
for line in cmdResult.stdOutLines:
print(line)
当然,我们可能希望在另一个系统上远程执行此命令。为此,我们可以使用相同的函数runCmd(..)
,但是需要先指定一个fabric
连接对象。可以通过以下方式完成:
from fabric import Connection
REMOTE_HOST = "myhost"
REMOTE_PORT = 22
REMOTE_LOGIN = "mylogin"
REMOTE_PASSWORD = "mypwd"
c = Connection(host=REMOTE_HOST, user=REMOTE_LOGIN, port=REMOTE_PORT, connect_kwargs={"password": REMOTE_PASSWORD})
cmdResult = jk_simpleexec.runCmd(c, "cd / ; ls -la")
# ... process the result stored in cmdResult ...
c.close()
所有内容保持完全一致,但这次我们在另一台主机上运行此命令。这是有意的:我希望拥有一个统一的API,在将来决定从本地主机移动到另一台主机时,不需要修改软件。
当然,还有密码问题。一些用户在上面提到了这个问题:我们可能想要请求执行此Python代码的用户输入密码。
为解决这个问题,我创建了一个自己的模块,名为jk_pwdinput
,相当长一段时间以来都在使用。与普通的密码输入不同的是,jk_pwdinput
会输出一些星号而不是什么都不打印。因此,对于您输入的每个密码字符,您都将看到一个星号。这样,您就更容易输入密码了。
下面是代码:
import jk_pwdinput
# ... define other 'constants' such as REMOTE_LOGIN, REMOTE_HOST ...
REMOTE_PASSWORD = jk_pwdinput.readpwd("Password for " + REMOTE_LOGIN + "@" + REMOTE_HOST + ": ")
readpwd(..)
返回None
,则表示用户通过Ctrl+C取消了密码输入。在真实世界的情境中,你可能需要根据实际情况采取相应的行动。)
以下是完整示例:
import jk_simpleexec
import jk_pwdinput
from fabric import Connection
REMOTE_HOST = "myhost"
REMOTE_PORT = 22
REMOTE_LOGIN = "mylogin"
REMOTE_PASSWORD = jk_pwdinput.readpwd("Password for " + REMOTE_LOGIN + "@" + REMOTE_HOST + ": ")
c = Connection(host=REMOTE_HOST, user=REMOTE_LOGIN, port=REMOTE_PORT, connect_kwargs={"password": REMOTE_PASSWORD})
cmdResult = jk_simpleexec.runCmd(
c = c,
command = "cd / ; ls -la"
)
cmdResult.raiseExceptionOnError("Something went wrong!", bDumpStatusOnError=True)
c.close()
我们拥有完整的解决方案:
以上代码对我来说解决了问题(希望对你也有帮助)。而且所有内容都是开源的:Fabric采用BSD-2-Clause许可,我的模块采用Apache-2许可。
使用的模块:
愉快地编程吧!;-)
import paramiko
import time
ssh = paramiko.SSHClient()
#ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.106.104.24', port=22, username='admin', password='')
time.sleep(5)
print('connected')
stdin, stdout, stderr = ssh.exec_command(" ")
def execute():
stdin.write('xcommand SystemUnit Boot Action: Restart\n')
print('success')
execute()
ssh
的subprocess
:https://gist.github.com/mamaj/a7b378a5c969e3e32a9e4f9bceb0c5eb - mamaj