Verify a file exists over ssh

后端 未结 6 1958
耶瑟儿~
耶瑟儿~ 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:25

    I have worked the solution that will do me. The code is below:

    def VersionID(): 
    
        ssh_newkey = 'Are you sure you want to continue connecting' 
        # my ssh command line 
        p=pexpect.spawn('ssh service@10.10.0.0') 
    
        i=p.expect([ssh_newkey,'password:',pexpect.EOF]) 
        if i==0: 
            p.sendline('yes') 
            i=p.expect([ssh_newkey,'password:',pexpect.EOF]) 
        if i==1: 
            p.sendline("word") 
            i=p.expect('service@main-:') 
            p.sendline("cd /opt/ad/bin") 
            i=p.expect('service@main-:') 
            p.sendline('[ -f email_tidyup.sh ] && echo "File exists" || echo "File does not exists"') 
            i=p.expect('service@main-:') 
            assert True 
        elif i==2: 
            print "I either got key or connection timeout" 
            assert False 
    
    
            results = p.before # print out the result
            print results
            value = results.split('"')[8]
            split_value = value.split('\r\n')[1:-1]
            self.assertEquals(split_value, ['File exists'])
    

    This extracts the value from 'p' in a string format. I then split the string up to get the string 'File Exists' into a list and compare it to the response I am looking for. If the File does not exists the test will fail.

    Thanks for all the help.

提交回复
热议问题