agent test_is_dir

This commit is contained in:
Amber 2023-11-30 11:29:30 +01:00
parent eb3b23e30f
commit b96ed81842
2 changed files with 36 additions and 0 deletions

View File

@ -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))