Question

I've a package with .gz and filename as (xxxxxxxxxxxx_PARSERxxxxx.tar.gz) extension placed in a remote directory say 1.1.1.1(Should be a input as a variable), i'm currently running the script from machine say 2.2.2.2, i need to place the package in machine 3.3.3.3 (Should be input as avariable), prepared a script for the same but its not working, pls help me out on the same,

Here is the code which i scripted so far(Not working):

#!/usr/bin/python 
#ftp.py 

import sys 
import telnetlib 
from ftplib import FTP 
import os 

def handleDownload(block): 
    file.write(block) 
    print ".", 
hstip= raw_input('Enter the hstip: ')      #ip of the machine where packages ae placed
uid = raw_input('Enter the username: ')   #uid os the machine whr packages are placed
pwd = raw_input('Enter the user password: ') #pwd of hte machine where packages are palced
path = raw_input('Enter the path where packages are present: ') #path for the packages.
rmthstip= raw_input('Enter the hstip: ')      #ip of the machine where packages to be placed
rmtuid = raw_input('Enter the username: ')   #uid os the machine whr packages to be placed
rmtpwd = raw_input('Enter the user password: ') #pwd of hte machine where packages to be palced
cwd = os.getcwd() 
os.chdir(cwd) 
logout = 'parser files downloaded succesfully' 
tn = telnetlib.Telnet(rmtip) 
tn.read_until("login: ") 
tn.write(rmtuid + "\n") 
tn.read_until("Password:") 
tn.write(rmtpwd + "\n") 
ftp=FTP(hstip,uid,pwd) 
print 'Logging in.' 
ftp.login(uid,pwd) 
ftp.cwd(path) 
parserfile = 'find . -name "*PARSER*.gz"' 
filenm = os.system(parserfile) 
print filenm 
ftp.retrbinary(' RETR ', 'filenm', logout ) 
ftp.quit() 
tn.close() 
Was it helpful?

Solution

It would really help if you told us how the program is not working.

By glancing quickly over it, I'd say you perserfile bit and filenm usage are wrong.

  1. os.system returns an integer, so filenm is not a file name, but the exist status of the find command. You want to use pipe = subprocess.Popen(['find', '.', '-name', '*PARSER*.gz'], stdout=subprocess.PIPE and a for loop over pipe.stdout to read all the lines produced by the command

  2. the ftp.retrbinary line looks suspicious: you probably want to remove the quotes around 'filenm' to use the path found above, and to use your callback function which needs fixing (see below)

  3. in handleDownload, the call to file.write will fail because you are calling on the file class and not on a file object

  4. Finally, it looks strange to me to perform a search on local file system to find a path on the remote ftp server which looks like what your code is doing, unless you have some specificities to your local path. Or are you trying to use the telnet connection to perform a remote search? IMO, if the FTP server allows you to do so and you know the remote path, you'd better set the CWD on the FTP server to that dir, retrieve the contents and look for a path that matches your requirements.

The downloading per se should be rewritten as:

local_fobj = file('downloaded_file.gz', 'wb')
def download_cb(data):
    local_fobj.write(data)
ftp.retrbinary('RETR %s' % filenm, download_cb)
local_fobj.close()

Other misc stuff:

  1. cwd = os.getwd() followed by os.chdir(cwd) can be safely removed
  2. rmtip is not defined, should by rmthstip
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top