码迷,mamicode.com
首页 > 编程语言 > 详细

Python调用shell的几种方式

时间:2015-02-15 23:16:20      阅读:470      评论:0      收藏:0      [点我收藏+]

标签: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和网络” 博客,转载请与作者联系!

Python调用shell的几种方式

标签:python shell

原文地址:http://haohaozhang.blog.51cto.com/9176600/1614564

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!