Python调用shell的几种方式
1.
cmd = "some unix command" retcode = subprocess.call(cmd,shell=True)
2.
ssh = paramiko.SSHClient() ssh.connect(server, username=username, password=password) ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)
3.
import spur shell = spur.SshShell(hostname="localhost", username="bob", password="password1") result = shell.run(["echo", "-n", "hello"]) print result.output # prints hello
4.
sshProcess = subprocess.Popen([‘ssh‘, <remote client>], ,stdin=subprocess.PIPE, stdout = subprocess.PIPE) sshProcess.stdin.write("ls mydirectory\n") sshProcess.stdin.write("echo END\n") for line in stdout.readlines(): if line == "END\n": break print(line) sshProcess.stdin.write("uptime\n") sshProcess.stdin.write("echo END\n") for line in stdout.readlines(): if line == "END\n": break print(line)
本文出自 “Linux和网络” 博客,转载请与作者联系!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。