Verify a file exists over ssh

后端 未结 6 1960
耶瑟儿~
耶瑟儿~ 2021-01-03 03:01

I am trying to test if a file exists over SSH using pexpect. I have got most of the code working but I need to catch the value so I can assert whether the file exists. The c

6条回答
  •  粉色の甜心
    2021-01-03 03:34

    I don't have any pexpect experience but looking at their web page it looks like you can call the expect method with multiple values and it returns the index of the one that it matches (this is based purely on me just looking at this example).

    child.expect('password:')
    child.sendline (my_secret_password)
    # We expect any of these three patterns...
    i = child.expect (['Permission denied', 'Terminal type', '[#\$] '])
    if i==0:
        print 'Permission denied on host. Can't login'
        child.kill(0)
    elif i==2:
        print 'Login OK... need to send terminal type.'
        child.sendline('vt100')
        child.expect ('[#\$] ')
    elif i==3:
        print 'Login OK.'
        print 'Shell command prompt', child.after
    

    Actually, you're already using that functionality at the top.

    So you want to catch whether the file exists or not?...

    Try this...

            p.sendline('[ -f email_tidyup.sh ] && echo "File exists" || echo "File does not exists"')
            file_exists = {0: True, 1: False}[p.expect(('File Exists', 'File does not exists'))]
    

提交回复
热议问题