Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

ssh - Python check if remote directory exist

I need to know if a remote directory exist with a Pyhton script. I try with Paramiko and stat and it's OK BUT.... it's not ok for all directories I have to check. Indeed, I have problem with permissions. Some directories, I need to check, need root permission.

The connected user is in sudo group.

How can I check the directories which need to be root ?

Thanks

Update :

Here the code :

# work
def test_ssh_dir(_server, _dir, _type='Tablespace'):
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy())
    client.connect(_server, username='*********', password='*********')
    execstr = 'sudo bash -c "test -d ' + _dir + ' && echo Yes || echo No"'
    stdin, stdout, stderr = client.exec_command(execstr)
    for line in stdout.read().splitlines():
        if line.decode() == 'Yes':
            print('[OK] {} directory exist on File System'.format(_type))
        else:
            print('[KO] !!! {} not exist on File System'.format(_type))
    client.close()

# doen't work
 def ssh_check_directory_exist(_server, _dir):
     client = SSHClient()
     client.set_missing_host_key_policy(AutoAddPolicy())
     client.connect(_server, username='********', password='**********')
     sftp = client.open_sftp()
     try:
         sftp.stat(_dir)
         print('[OK] Tablespace directory exist on File System')
     except IOError as e:
         print(e)
         print('[KO] !!! Tablespace not exist on File System')
     client.close()
question from:https://stackoverflow.com/questions/65846633/python-check-if-remote-directory-exist

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...