您的位置:首页 > 运维架构 > Shell

Python调用shell的几种方式

2015-02-15 15:17 344 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell python