Created
November 14, 2012 01:59
-
-
Save lite/4069782 to your computer and use it in GitHub Desktop.
openqq login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding=utf-8 | |
import sys | |
import pexpect | |
class OpenQQShell: | |
def __init__(self, app_user, jumper_ip, jumper_port, token, passwd, cvm_ip): | |
self.p = self.get_shell(app_user, jumper_ip, jumper_port, token, passwd, cvm_ip) | |
def get_shell(self, app_user, jumper_ip, jumper_port, token, passwd, cvm_ip): | |
need_pass = '(?i)password: ' | |
known_hosts='(?i)are you sure you want to continue connecting' | |
p = pexpect.spawn ('ssh %s@%s -p %s -i ./shell/openssh-key' % (app_user, jumper_ip, jumper_port )) | |
#p.logfile = sys.stdout | |
i=p.expect([known_hosts, need_pass, pexpect.EOF, pexpect.TIMEOUT]) | |
if i==0: | |
p.sendline('yes') | |
i=p.expect(need_pass) | |
if i==1: | |
p.sendline(str(token) + passwd) | |
p.expect(':~>') | |
p.sendline('ssh ' + cvm_ip) | |
i=p.expect([known_hosts, need_pass, pexpect.EOF, pexpect.TIMEOUT]) | |
if i==0: | |
p.sendline('yes') | |
i=p.expect(need_pass) | |
if i==1: | |
p.sendline(passwd) | |
p.expect('\w+@.+:~>') | |
return p | |
def run_cmd(self, cmd): | |
self.p.sendline(cmd) | |
self.p.expect('\w+@.+:~>') | |
return self.p.before | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment