-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshinteract.py
More file actions
45 lines (39 loc) · 1.08 KB
/
sshinteract.py
File metadata and controls
45 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
'''
from Internet
'''
# -*- coding: utf-8 -*-
'''
usage
import sshinteract
host = '192.168.1.12'
user = 'root'
password = 'root'
shellcmd = sshinteract.sshconnect(user, host, password)
sshinteract.send_command(shellcmd, 'cat /etc/shadow | grep root')
'''
import pexpect
PROMPT = ['# ', '>>> ', '> ','\$ ']
def send_command(child, cmd):
child.sendline(cmd)
child.expect(PROMPT)
print child.before
def sshconnect(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,\
'[P|p]assword:'])
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