2010-10-13 61 views
1

我想压缩远程namchine.For文件夹我使用paramiko。 但我不知道如何使用paramiko做到这一点。 有什么建议?如何使用paramiko执行远程命令

这是我的代码:

dpath = '/var/mysql/5.1/mysql.zip'  
port = 22  
host = '10.88.36.7'  
transport = paramiko.Transport((host, port))  
transport.connect(username=suser, password=spass)  
channel = transport.open_channel(kind="session")  
channel.exec_command('zip -r /var/db/mysql /var/db/mysql')  
transport.close() 

什么是错在这?

+0

到目前为止你有什么,它怎么不工作? – 2010-10-13 13:56:06

+0

如果你正在做一些比这只命令更精细的东西,那么使用Fabric 是值得的。它建立在paramiko之上,隐藏了大部分这种样板代码。 – AndrewF 2010-10-13 18:25:36

回答

3

channel.exec_command(...) 

你必须等待命令与终止:

while not channel.exit_status_ready() 
    ... wait ... (you can read the output with channel.recv, or sleep a bit) 

此外,你zip命令是怪异......难道你想说

zip -r /var/db/mysql.zip /var/db/mysql 
+0

非常感谢!有效... – rushi 2010-10-13 14:32:22