agent test_is_dir
This commit is contained in:
parent
eb3b23e30f
commit
b96ed81842
Binary file not shown.
@ -1,4 +1,7 @@
|
||||
import stat
|
||||
|
||||
import paramiko
|
||||
import paramiko.sftp_client as _sftp_client
|
||||
|
||||
HOSTS_KEYS_PATH = '/home/luca/.ssh/known_hosts'
|
||||
PKEY_PATH = '/home/luca/.ssh/notanamber_rsa'
|
||||
@ -6,6 +9,18 @@ PKEY_PATH = '/home/luca/.ssh/notanamber_rsa'
|
||||
HOSTNAME = '107.152.32.78'
|
||||
USERNAME = 'notanamber'
|
||||
|
||||
|
||||
'''
|
||||
check if a remote file is dir or not
|
||||
|
||||
import stat
|
||||
|
||||
def downLoadFile(sftp, remotePath, localPath):
|
||||
for fileattr in sftp.listdir_attr(remotePath):
|
||||
if stat.S_ISDIR(fileattr.st_mode):
|
||||
sftp.get(fileattr.filename, os.path.join(localPath, fileattr.filename))
|
||||
'''
|
||||
|
||||
class SyncAgent():
|
||||
|
||||
def __init__(self):
|
||||
@ -14,6 +29,9 @@ class SyncAgent():
|
||||
self.client.load_host_keys(filename=HOSTS_KEYS_PATH)
|
||||
|
||||
def connect(self):
|
||||
'''
|
||||
this method generates the underlying `Trasport`
|
||||
'''
|
||||
self.client.connect(hostname=HOSTNAME, username=USERNAME, pkey=self.pkey, look_for_keys=False)
|
||||
|
||||
def close(self):
|
||||
@ -29,3 +47,21 @@ class SyncAgent():
|
||||
if estream:
|
||||
print("Command failed for this reason: %s" % (estream))
|
||||
return ostream.strip()
|
||||
|
||||
def get_sftp_client(self):
|
||||
if not self.client.get_transport():
|
||||
print('Transport not present proceed to create it... ')
|
||||
self.connect()
|
||||
print('--> OK!')
|
||||
return self.client.open_sftp()
|
||||
|
||||
def test_is_dir(self, path='.'):
|
||||
sftpc = self.get_sftp_client()
|
||||
|
||||
for attr in sftpc.listdir_attr(path):
|
||||
if stat.S_ISDIR(attr.st_mode):
|
||||
print('name: %s is DIR' % (attr.filename))
|
||||
continue
|
||||
print('name: %s is regular FILE' % (attr.filename))
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user