Re: [Tutor] sftp get single file

2011-06-22 Thread Peter Lavelle
You could use the subprocess module to run the relevant system commands. 
More info on running sftp non-interactively (i.e from a script) can be 
found here: http://fixunix.com/ssh/238284-non-interactive-sftp-put.html


Regards

Peter Lavelle
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2011-06-21 Thread Johan Geldenhuys
Hi all,
This topic is old, but I have a similar issue and I know everybody will say I 
should use piramiko, but it is not that simple.

The device I have to run my python scripts on is a simple, small, scaled down 
version of Suse linux and I can't install other packages like piramiko.
All the files I need to use that is not part of Python 2.7 must be included in 
my package.

With the above in mind, what can I use to open SFTP to a server and transfer a 
file?

Thanks

Johan


-Original Message-
From: tutor-bounces+johan=accesstel.com...@python.org 
[mailto:tutor-bounces+johan=accesstel.com...@python.org] On Behalf Of Sander 
Sweers
Sent: Saturday, 18 July 2009 1:43 AM
To: Matt Herzog
Cc: Python List
Subject: Re: [Tutor] sftp get single file

2009/7/17 Matt Herzog :
> Hello All.
>
> I need to use paramiko to sftp get a single file from a remote server.
> The remote file's base name will be today's date (%Y%m%d) dot tab.
> I need help joining the today with the .tab extension. Do I need globbing?
>
> example: 20090716.tab
>
> #!/usr/bin/env python
> import paramiko
> import glob
> import os
> import time
> hostname = 'sftp.booboo.com'
> port = 22
> username = 'booboo'
> password = '07N4219?'
> # glob_pattern='*.tab'
> today = time.strftime("%Y%m%d")
> remotepath = today.tab
> localpath = '/home/data/text'
>
> if __name__ == "__main__":
>t = paramiko.Transport((hostname, port))
>t.connect(username=username, password=password)
>sftp = paramiko.SFTPClient.from_transport(t)
>sftp.get(remotepath, localpath)
>t.close()
> --

You don't need glob if you know in advance what the filename is. Print
example below.

---
import time

today = time.localtime()
datestr = time.strftime("%Y%m%d",today)
ext = ".tab"

print datestr + ext
---

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Checked by AVG - www.avg.com 
Version: 8.5.387 / Virus Database: 270.13.17/2242 - Release Date: 07/16/09 
18:00:00

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Mon, Jul 20, 2009 at 05:26:09PM -0500, Wayne wrote:
> On Mon, Jul 20, 2009 at 5:18 PM, Matt Herzog  wrote:
> 
> > On Mon, Jul 20, 2009 at 11:57:57PM +0200, Sander Sweers wrote:
> > > Please reply to the list.
> > >
> > > 2009/7/20 Matt Herzog :
> > > > Yeah. I have no idea if I am able to do this. The jail makes it
> > ambiguous.
> > >
> > > There is no difference, the jail just moves the root directory as seen
> > > by the client.
> > >
> > > Did you do as suggested earlier to use listdir()? This will tell you
> > > how paramiko sees the server.
> >
> > Yeah, I managed to get that to work like so:
> >
> > if __name__ == "__main__":
> >t = paramiko.Transport((hostname, port))
> >t.connect(username=username, password=password)
> >sftp = paramiko.SFTPClient.from_transport(t)
> > print sftp.listdir()
> >t.close()
> >
> > It displays the contents of the dir no problem.
> 
> 
> Is the file present in the dir? What is it listed as? Is it just
> 07232009.tab (or similar) or is it /home/user/jail/07232009.tab? That may
> make a difference.

Yes, the file 20090720.tab (today's date dot tab) is present in the dir 
listing. The dir contents are printed out in a comma delimited list from left 
to right that wraps several times. There are probably 30 subdirs and a dozen 
files in that dir. If you use the sftp cli binary and type, 'pwd' it tells you, 
'/'.

> 
> 
> >
> >
> > >
> > > I get the feeling you are in on over your head on this.
> >
> > Yes, and I have a deadline.
> >
> > > Maybe you need
> > > to read the paramiko docs and play around in idle or other interactive
> > > python interpreter. The SFTPClient has all the basic commands any ssh
> > > client has (listdir, chdir etc). See [1].
> >
> > I should not need to chdir, right? In fact, the server's policy forbids it.
> > Perhaps I need to chdir on the local host?
> 
> 
> That shouldn't make a difference as far as getting the file is concerned -
> unless you're trying to put it in a file on your computer that doesn't
> exist!

I'm trying to put a file in a dir called /tmp/testor. The user the script runs 
as owns the dir.

> 
> I'd really recommend playing around in the interpreter - especially if you
> have ipython.

I have ipython. I  like ipython. I just don't get enough opportunities to do 
python scripting.

> 
> Good luck, HTH,
> Wayne

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

- William Shakespeare
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Wayne
On Mon, Jul 20, 2009 at 5:18 PM, Matt Herzog  wrote:

> On Mon, Jul 20, 2009 at 11:57:57PM +0200, Sander Sweers wrote:
> > Please reply to the list.
> >
> > 2009/7/20 Matt Herzog :
> > > Yeah. I have no idea if I am able to do this. The jail makes it
> ambiguous.
> >
> > There is no difference, the jail just moves the root directory as seen
> > by the client.
> >
> > Did you do as suggested earlier to use listdir()? This will tell you
> > how paramiko sees the server.
>
> Yeah, I managed to get that to work like so:
>
> if __name__ == "__main__":
>t = paramiko.Transport((hostname, port))
>t.connect(username=username, password=password)
>sftp = paramiko.SFTPClient.from_transport(t)
> print sftp.listdir()
>t.close()
>
> It displays the contents of the dir no problem.


Is the file present in the dir? What is it listed as? Is it just
07232009.tab (or similar) or is it /home/user/jail/07232009.tab? That may
make a difference.


>
>
> >
> > I get the feeling you are in on over your head on this.
>
> Yes, and I have a deadline.
>
> > Maybe you need
> > to read the paramiko docs and play around in idle or other interactive
> > python interpreter. The SFTPClient has all the basic commands any ssh
> > client has (listdir, chdir etc). See [1].
>
> I should not need to chdir, right? In fact, the server's policy forbids it.
> Perhaps I need to chdir on the local host?


That shouldn't make a difference as far as getting the file is concerned -
unless you're trying to put it in a file on your computer that doesn't
exist!

I'd really recommend playing around in the interpreter - especially if you
have ipython.

Good luck, HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Mon, Jul 20, 2009 at 11:57:57PM +0200, Sander Sweers wrote:
> Please reply to the list.
> 
> 2009/7/20 Matt Herzog :
> > Yeah. I have no idea if I am able to do this. The jail makes it ambiguous.
> 
> There is no difference, the jail just moves the root directory as seen
> by the client.
> 
> Did you do as suggested earlier to use listdir()? This will tell you
> how paramiko sees the server.

Yeah, I managed to get that to work like so:

if __name__ == "__main__":
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
print sftp.listdir()
t.close()

It displays the contents of the dir no problem.

> 
> I get the feeling you are in on over your head on this. 

Yes, and I have a deadline.

> Maybe you need
> to read the paramiko docs and play around in idle or other interactive
> python interpreter. The SFTPClient has all the basic commands any ssh
> client has (listdir, chdir etc). See [1].

I should not need to chdir, right? In fact, the server's policy forbids it.
Perhaps I need to chdir on the local host?

> Also have a look at [2] which is a good example of what can be done.

Yeah I saw that page. The code seemed bloated to me.

> 
> Greets
> Sander
> 
> [1] http://www.lag.net/paramiko/docs/paramiko.SFTPClient-class.html
> [2] http://code.activestate.com/recipes/576810/
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

- William Shakespeare
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
Please reply to the list.

2009/7/20 Matt Herzog :
> Yeah. I have no idea if I am able to do this. The jail makes it ambiguous.

There is no difference, the jail just moves the root directory as seen
by the client.

Did you do as suggested earlier to use listdir()? This will tell you
how paramiko sees the server.

I get the feeling you are in on over your head on this. Maybe you need
to read the paramiko docs and play around in idle or other interactive
python interpreter. The SFTPClient has all the basic commands any ssh
client has (listdir, chdir etc). See [1].

Also have a look at [2] which is a good example of what can be done.

Greets
Sander

[1] http://www.lag.net/paramiko/docs/paramiko.SFTPClient-class.html
[2] http://code.activestate.com/recipes/576810/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
2009/7/20 Matt Herzog :
> remotepath = 'datestr'

Ok, you are now making a string.

> remotepath = datestr

Like Kent wrote the datestr can include other characters. So I would
try "/%Y%m%d.tab".

> sftp.get(remotepath, localpath)
>  File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 587, in get
>  IOError: [Errno 21] Is a directory: '/tmp/testor/'

paramiko still can not find the file.

> So remotepath is really more like a path + filname.

Yes.

> So I need to do something like os.join the two?

You probably mean os.path.join(), you can use it but imho it is
overkill in your case. Just make sure you get the full path to the
file in your remotepath.

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
If I change

remotepath = 'datestr'

to 

remotepath = datestr

I get:

sftp.get(remotepath, localpath)
  File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 587, in get
  IOError: [Errno 21] Is a directory: '/tmp/testor/'


So remotepath is really more like a path + filname.

So I need to do something like os.join the two?


-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

- William Shakespeare
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
2009/7/20 Matt Herzog :
> The file is there. I can sftp it using fugu.

I am sure it is but paramiko can't find it.

> The path is /20090720.tab since the file lives in a jail.

Issue a sftp.listdir() and see what paramiko sees on the remote server.

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Mon, Jul 20, 2009 at 11:02:52PM +0200, Sander Sweers wrote:
> 2009/7/20 Matt Herzog :
> > Traceback (most recent call last):
> > ??File "./scpgetter.py", line 20, in ?
> > ?? ?? ??sftp.get(remotepath, localpath)
> > ?? ?? ?? ??File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", 
> > line 584, in get
> > ?? ?? ?? ?? ??File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", 
> > line 240, in open
> > ?? ?? ?? ?? ?? ??File 
> > "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 609, in 
> > _request
> > ?? ?? ?? ?? ?? ?? ??File 
> > "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 656, in 
> > _read_response
> > ?? ?? ?? ?? ?? ?? ?? ??File 
> > "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 682, in 
> > _convert_status
> > ?? ?? ?? ?? ?? ?? ?? ??IOError: (2, '')
> 
> Ah, now it get interesting. Genrally IOError means the file is not
> found. And looking at the source for _convert_status it is axactly
> that. The file you are trying to "get" is not found on the server so
> check your remotepath.

The file is there. I can sftp it using fugu.

The path is /20090720.tab since the file lives in a jail.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
2009/7/20 Matt Herzog :
> Traceback (most recent call last):
>  File "./scpgetter.py", line 20, in ?
>      sftp.get(remotepath, localpath)
>        File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 584, 
> in get
>          File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 
> 240, in open
>            File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 
> 609, in _request
>              File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", 
> line 656, in _read_response
>                File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", 
> line 682, in _convert_status
>                IOError: (2, '')

Ah, now it get interesting. Genrally IOError means the file is not
found. And looking at the source for _convert_status it is axactly
that. The file you are trying to "get" is not found on the server so
check your remotepath.

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Mon, Jul 20, 2009 at 10:22:37PM +0200, Sander Sweers wrote:
> I do not know paramiko but looking over the client documentations...
> 
> 2009/7/20 Matt Herzog :
> > if __name__ == "__main__":
> >t = paramiko.Transport((hostname, port))
> >t.connect(username=username, password=password)
> >sftp = paramiko.SFTPClient.from_transport(t)
> 
> Up to here it looks fine.
> 
> >sftp(remotepath,localpath)
> 
> If I understand it correctly you need to use sftp.get(remotepath, localpath).

Yeah that's exactly what I have tried, and the error is:

Traceback (most recent call last):
  File "./scpgetter.py", line 20, in ?
  sftp.get(remotepath, localpath)
File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 584, 
in get
  File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 
240, in open
File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 
609, in _request
  File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", line 
656, in _read_response
File "build/bdist.linux-x86_64/egg/paramiko/sftp_client.py", 
line 682, in _convert_status
IOError: (2, '')

> 
> Greets
> Sander
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

- William Shakespeare
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Sander Sweers
I do not know paramiko but looking over the client documentations...

2009/7/20 Matt Herzog :
> if __name__ == "__main__":
>t = paramiko.Transport((hostname, port))
>t.connect(username=username, password=password)
>sftp = paramiko.SFTPClient.from_transport(t)

Up to here it looks fine.

>sftp(remotepath,localpath)

If I understand it correctly you need to use sftp.get(remotepath, localpath).

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-20 Thread Matt Herzog
On Fri, Jul 17, 2009 at 12:12:52PM -0400, Kent Johnson wrote:
> On Fri, Jul 17, 2009 at 11:42 AM, Sander Sweers 
> wrote:
> 
> > import time
> >
> > today = time.localtime()
> > datestr = time.strftime("%Y%m%d",today)
> > ext = ".tab"
> >
> > print datestr + ext
> 
> You can include literal characters in the format string:
> 
> In [4]: time.strftime("%Y%m%d.tab",today)
> Out[4]: '20090717.tab'

That does work and is compact while being intelligible.
I'm still not getting the file. I thought this would work:

if __name__ == "__main__":
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp(remotepath,localpath)
t.close()

 . . . but obviously something is missing. 

Traceback (most recent call last):
  File "./scpgetter.py", line 20, in ?
  sftp(remotepath,localpath)
  TypeError: 'SFTPClient' object is not callable

I tried using the paramiko.SFTP.get method too. Failed.

-- Matt H


> 
> Kent

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

- William Shakespeare
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-17 Thread Kent Johnson
On Fri, Jul 17, 2009 at 11:42 AM, Sander Sweers wrote:

> import time
>
> today = time.localtime()
> datestr = time.strftime("%Y%m%d",today)
> ext = ".tab"
>
> print datestr + ext

You can include literal characters in the format string:

In [4]: time.strftime("%Y%m%d.tab",today)
Out[4]: '20090717.tab'

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp get single file

2009-07-17 Thread Sander Sweers
2009/7/17 Matt Herzog :
> Hello All.
>
> I need to use paramiko to sftp get a single file from a remote server.
> The remote file's base name will be today's date (%Y%m%d) dot tab.
> I need help joining the today with the .tab extension. Do I need globbing?
>
> example: 20090716.tab
>
> #!/usr/bin/env python
> import paramiko
> import glob
> import os
> import time
> hostname = 'sftp.booboo.com'
> port = 22
> username = 'booboo'
> password = '07N4219?'
> # glob_pattern='*.tab'
> today = time.strftime("%Y%m%d")
> remotepath = today.tab
> localpath = '/home/data/text'
>
> if __name__ == "__main__":
>    t = paramiko.Transport((hostname, port))
>    t.connect(username=username, password=password)
>    sftp = paramiko.SFTPClient.from_transport(t)
>    sftp.get(remotepath, localpath)
>    t.close()
> --

You don't need glob if you know in advance what the filename is. Print
example below.

---
import time

today = time.localtime()
datestr = time.strftime("%Y%m%d",today)
ext = ".tab"

print datestr + ext
---

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sftp get single file

2009-07-17 Thread Matt Herzog
Hello All.

I need to use paramiko to sftp get a single file from a remote server.
The remote file's base name will be today's date (%Y%m%d) dot tab. 
I need help joining the today with the .tab extension. Do I need globbing?

example: 20090716.tab

#!/usr/bin/env python
import paramiko
import glob
import os
import time
hostname = 'sftp.booboo.com'
port = 22
username = 'booboo'
password = '07N4219?'
# glob_pattern='*.tab'
today = time.strftime("%Y%m%d")
remotepath = today.tab
localpath = '/home/data/text'

if __name__ == "__main__":
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(remotepath, localpath)
t.close()


-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

- William Shakespeare
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor