Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

python - how to use paramiko to execute remote commands

I wanted to compress a folder on a remote namchine.For that i am using paramiko. But i don't know how to do that using paramiko. Any suggestions??

This is my code:

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()

whats wrong in this??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After

channel.exec_command(...)

You have to wait the termination of the command with:

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

Furthermore, you're zip command is weird... don't you want to say

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...