相关疑难解决方法(0)

通过ssh验证文件是否存在

我试图使用pexpect测试SSH上是否存在文件.我有大部分代码工作,但我需要捕获值,所以我可以断言文件是否存在.我所做的代码如下:

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('File 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

VersionID()
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

python testing ssh bash pexpect

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

bash ×1

pexpect ×1

python ×1

ssh ×1

testing ×1