본문 바로가기

컴퓨터/Python

Pexpect로 SSH 연결하기

pexpect.sourceforge.net 에서 모듈을 다운받아 설치한다. 

(프로그램 작동, 프로그램에서 기대하고 있는 결과값 보기, 그리고 기대하고 있는 결과값으 바탕으로 응답하기 기능이 있음)

tar xvzf pexpect-2.3.tar.gz
cd pexpect-2.3
python setup.py install


import pexpect
PROMPT = ['# ','>>> ','> ','\$ ']

def send_command(child, cmd):
        child.sendline(cmd)
        child.expect(PROMPT)
        print child.before
def connect(user, host, password):
        ssh_newkey = 'Are you sure you want to continue connecting'
        connStr = 'ssh '+user+'@'+host
        child = pexpect.spawn(connStr)
        ret = child.expect([pexpect.TIMEOUT, ssh_newkey])
        if ret == 0:
                print '[-] Error Connecting'
                return
        if ret == 1:
                child.sendline('yes')
                ret = child.expect([pexpect.TIMEOUT, \
                '[P|p]assword:'])
                if ret == 0:
                        print '[-] Error Connecting'
                        return
                child.sendline(password)
                child.expect(PROMPT)
                return child
def main():
        host = '127.0.0.1'
        user = 'root'
        password = 'root'
        child = connect(user, host, password)
        send_command(child, 'cat /etc/shadow | grep root')
if __name__ == '__main__':
        main()

'컴퓨터 > Python' 카테고리의 다른 글

Pxssh로 SSH 패스워드 공격  (0) 2013.08.20
nmap 포트 스캐너 통합하기  (0) 2013.08.13
포트 스캐너  (0) 2013.08.12
압축 파일의 패스워드 찾기  (0) 2013.08.10
압축파일에 잘못된 패스워드를 입력  (0) 2013.08.09