简体   繁体   中英

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. Can anyone suggest me any modules that will help me do this?

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?

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. Fabric is written on top of paramiko and has some API level abstraction that makes it easier to use than paramiko.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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