Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82507

Re: how to delete my file if exits in the remote server with paramiko?

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: how to delete my file if exits in the remote server with paramiko?
Date 2014-12-14 10:58 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-0F10F8.10582514122014@news.panix.com> (permalink)
References <mailman.16944.1418561416.18130.python-list@python.org>

Show all headers | View raw


In article <mailman.16944.1418561416.18130.python-list@python.org>,
 "????" <1248283536@qq.com> wrote:

> I want to delete the file "names.txt" if it exits in "/home/names.txt" in my 
> remote vps server.
>  import paramiko
> host = "vps ip"
> port = 22
> transport = paramiko.Transport((host, port))
> password = "key"
> username = "root"
> transport.connect(username = username, password = password)
> sftp = paramiko.SFTPClient.from_transport(transport)
> if "names.txt" in sftp.listdir("/home") : sftp.remove("/home/names.txt")
>   
>   
>  Is there more elegant way to do the same work?

I generally try to avoid using raw paramiko.  Fabric gives you a much 
nicer interface to the same basic functionality (and is indeed layered 
on top of paramiko).  The above functionality can be expressed in fabric 
as simply as:


---------------------------------------------
from fabric.api import env, run

env.host_string = "my-remote-hostname"
run("rm -f /tmp/roy-temp")
---------------------------------------------

Of course, I cheated a little bit.  I'm assuming you have ssh access to 
your remote host; the above code depends on that.  Going through sftp 
makes everything more difficult.  You can drive sftp through fabric, but 
it's a lot more painful.  I also assume that you can use ssh key 
authentication, which simplifies everything compared to passing 
usernames and passwords around.

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

how to delete my file if exits in the remote server with paramiko? "水静流深" <1248283536@qq.com> - 2014-12-14 20:49 +0800
  Re: how to delete my file if exits in the remote server with paramiko? Roy Smith <roy@panix.com> - 2014-12-14 10:58 -0500

csiph-web