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

时间:2022-02-24 16:24:04

I need to write a script to ssh to remote server and run some commands there. Can anyone suggest me any modules that will help me do this?

我需要编写一个脚本ssh到远程服务器并在那里运行一些命令。任何人都可以建议我任何可以帮助我这样做的模块吗?

I tried to use pexpect. I was able to login to machine, provide password, but could not execute any unix commands. Is pexpect meant for this sort id usage?

我试着用pexpect。我能够登录到机器,提供密码,但无法执行任何unix命令。 pexpect是否意味着这种用法?

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.

直到最后一行的所有内容都可以,但是从那时起我的脚本就会退出。

1 个解决方案

#1


0  

If you want to do something relatively quick on a remote server fabric is probably the best thing to use for python. Fabric is written on top of paramiko and has some API level abstraction that makes it easier to use than paramiko.

如果你想在远程服务器上做一些相对快速的事情,那么对于python来说,这可能是最好的选择。 Fabric是在paramiko之上编写的,并且有一些API级别的抽象,比paramiko更容易使用。

#1


0  

If you want to do something relatively quick on a remote server fabric is probably the best thing to use for python. Fabric is written on top of paramiko and has some API level abstraction that makes it easier to use than paramiko.

如果你想在远程服务器上做一些相对快速的事情,那么对于python来说,这可能是最好的选择。 Fabric是在paramiko之上编写的,并且有一些API级别的抽象,比paramiko更容易使用。