SFTP Connection Notes:
Windows based code:
import pysftp as sftp
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
myHostname = "secureftp.abc.com"
myUsername = "username"
myPassword = "password"
myPort = 22
myPriKey="C:\test\Privatekey.pem"
myPriKeyPass="passphrase"
s = sftp.Connection(host=myHostname, username=myUsername, port=myPort, private_key=myPriKey, private_key_pass=myPriKeyPass,cnopts=cnopts)
remotedir="./remote_dir_name/"
localdir="C:\\Users\\Test\\Downloads\\"
dir = s.listdir(remotedir+"*csv")
for a in dir:
print(a)
s.get(remotedir+a)
s.close()
Unix based code:
#Code to pull the files from zobble sftp to edgenode local directory
import datetime
def dt_tm():
dttm=datetime.datetime.now().strftime("%x %X")
return dttm
import pysftp as sftp
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
myHostname = "secureftp.abc.com"
myUsername = "username"
myPassword = "password"
myPort = 22
myPriKey="/home/tes/keys/privatekey.pem"
myPriKeyPass="passphrase"
s = sftp.Connection(host=myHostname, username=myUsername, port=myPort, private_key=myPriKey, private_key_pass=myPriKeyPass,cnopts=cnopts)
remotedir="./remote_dir_name/"
myLocalDir="/home/AZFRK/ksumanam/indiasales/ZobbleFiles/"
dir = s.listdir(remotedir+"*csv")
i=0
for a in dir:
print(a)
i+=1
s.get(remotedir+a,myLocalDir+a)
print(dt_tm(), 'copied files from sftp server. Number of files copied :',i)
s.close()
- Below code connects to a sftp server.
- It needs a private key. The private key should be in PEM format.
- If a PPK format key is present, covert it to PEM format using Putty tool.
- The Private key has a passphrase associated with it.
- Two level authentication with password and private key (along with passphrase) might be present in some cases.
- It is observed that, though password is not given the python code authenticates successfully using the private key and passphrase itself.
- Using WINSCP - the two way authentication was successful only if passphrase and password are given. It is a surprise and to be researched still, why python script is able to bypass and make a successful connection with only privatekey + passphrase.
Windows based code:
import pysftp as sftp
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
myHostname = "secureftp.abc.com"
myUsername = "username"
myPassword = "password"
myPort = 22
myPriKey="C:\test\Privatekey.pem"
myPriKeyPass="passphrase"
s = sftp.Connection(host=myHostname, username=myUsername, port=myPort, private_key=myPriKey, private_key_pass=myPriKeyPass,cnopts=cnopts)
remotedir="./remote_dir_name/"
localdir="C:\\Users\\Test\\Downloads\\"
dir = s.listdir(remotedir+"*csv")
for a in dir:
print(a)
s.get(remotedir+a)
s.close()
Unix based code:
#Code to pull the files from zobble sftp to edgenode local directory
import datetime
def dt_tm():
dttm=datetime.datetime.now().strftime("%x %X")
return dttm
import pysftp as sftp
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
myHostname = "secureftp.abc.com"
myUsername = "username"
myPassword = "password"
myPort = 22
myPriKey="/home/tes/keys/privatekey.pem"
myPriKeyPass="passphrase"
s = sftp.Connection(host=myHostname, username=myUsername, port=myPort, private_key=myPriKey, private_key_pass=myPriKeyPass,cnopts=cnopts)
remotedir="./remote_dir_name/"
myLocalDir="/home/AZFRK/ksumanam/indiasales/ZobbleFiles/"
dir = s.listdir(remotedir+"*csv")
i=0
for a in dir:
print(a)
i+=1
s.get(remotedir+a,myLocalDir+a)
print(dt_tm(), 'copied files from sftp server. Number of files copied :',i)
s.close()
No comments:
Post a Comment