简体   繁体   English

交互式Python脚本可在远程服务器上运行命令

[英]Interactive Python Script to run commands on remote server

I need to write a script to ssh to remote server and run some commands there. 我需要编写脚本以ssh到远程服务器并在其中运行一些命令。 Can anyone suggest me any modules that will help me do this? 谁能建议我任何可以帮助我做到这一点的模块?

I tried to use pexpect. 我尝试使用pexpect。 I was able to login to machine, provide password, but could not execute any unix commands. 我能够登录到计算机,提供密码,但是无法执行任何Unix命令。 Is pexpect meant for this sort id usage? pexpect是否适合这种id用途?

command = 'ssh '+'uname@'+ vm

    ssh_newkey = 'Are you sure you want to continue connecting'
    # my ssh command line
    p=pexpect.spawn(command)

    i=p.expect([ssh_newkey,'password:',pexpect.EOF])
    if i==0:
        print "Say yes to prompt"
        p.sendline('yes')
        i=p.expect([ssh_newkey,'password:',pexpect.EOF])
    if i==1:
        print "I give password\n",
        p.sendline("abcd")
        p.sendline("pwd")
        p.expect('xyza')
        #p.sendline("//some unix command")

Everything until the last line is ok, but there onwards my script just exits. 直到最后一行都可以,但是从那以后我的脚本就退出了。

If you want to do something relatively quick on a remote server fabric is probably the best thing to use for python. 如果要在远程服务器结构上相对较快地执行某项操作,可能是对python最好的选择。 Fabric is written on top of paramiko and has some API level abstraction that makes it easier to use than paramiko. Fabric是在paramiko之上编写的,并具有一些API级别的抽象,这使它比paramiko更加易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM