Tôi đang cố gắng để chạy mã trong Tương tác với SSH Qua Pexpect và Brute Buộc Passwords SSH với Pxssh phần từ Chương 2 của Violent Python. Sử dụng cả hai child.expect()
và pxssh
Tôi nhận được lỗi EOF tương tự.EOF khi sử dụng Pexpect và pxssh
Chạy các lệnh từ Python console:
import pexpect
connStr = "ssh [email protected]"
child = pexpect.spawn(connStr)
ret = child.expect([pexpect.TIMEOUT, ssh_newkey, "[P|p]assword:"])
tôi nhận được kết quả này:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1316, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1330, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1401, in expect_loop
raise EOF (str(e) + '\n' + str(self))
EOF: End Of File (EOF) in read_nonblocking(). Empty string style platform.
<pexpect.spawn object at 0x10180c550>
version: 2.4 ($Revision: 516 $)
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '[email protected]']
searcher: searcher_re:
0: TIMEOUT
1: re.compile("Are you sure you want to continue connecting")
2: re.compile("[P|p]assword:")
buffer (last 100 chars):
before (last 100 chars):
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: 255
flag_eof: True
pid: 12122
child_fd: 4
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
Và chạy các lệnh này, sử dụng pxssh
:
import pxssh
s = pxssh.pxssh()
s.login("127.0.0.1", "root", "1234")
tôi nhận được kết quả này :
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pxssh.py", line
196, in login
i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:pas
sword)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT, "(?i)connectio
n closed by remote host"], timeout=login_timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1316, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1330, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li
ne 1401, in expect_loop
raise EOF (str(e) + '\n' + str(self))
EOF: End Of File (EOF) in read_nonblocking(). Empty string style platform.
<pxssh.pxssh object at 0x1016bff90>
version: 2.4 ($Revision: 516 $)
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '-q', '-l', 'root', '127.0.0.1']
searcher: searcher_re:
0: re.compile("(?i)are you sure you want to continue connecting")
1: re.compile("[#$]")
2: re.compile("(?i)(?:password)|(?:passphrase for key)")
3: re.compile("(?i)permission denied")
4: re.compile("(?i)terminal type")
5: TIMEOUT
6: re.compile("(?i)connection closed by remote host")
buffer (last 100 chars):
before (last 100 chars):
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: None
flag_eof: True
pid: 12136
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
Tôi nhận được kết quả tương tự khi tôi thay thế 127.0.0.1 bằng các máy chủ khác và thử các kết hợp tên người dùng/mật khẩu khác nhau.
pexpect documentation đề xuất sử dụng expect(pexpect.EOF)
để tránh tạo ngoại lệ EOF. Thật vậy, khi tôi làm như sau:
connStr = "ssh [email protected]"
child = pexpect.spawn(connStr)
print child.expect(pexpect.EOF)
Kết quả là 0
.
Nhưng các câu hỏi sau vẫn:
- Tôi đang bối rối bởi cú pháp của cuốn sách:
child.expect([pexpect.TIMEOUT, ssh_newkey, "[P|p]assword:"])
. Tại sao chúng ta chuyển danh sách sangexpect()
? Danh sách này có nghĩa vụ gì? - Làm cách nào để sử dụng
expect(pexpect.EOF)
, như tài liệu hướng dẫn, khi sử dụng pxssh? - Tại sao mã trong sách không hoạt động chính xác? Có một cái gì đó thay đổi trong thư viện pexpect kể từ khi xuất bản sách? Có phải vì tôi đang sử dụng OS X không?
Tôi có Python 2.7 và pexpect 2.4 đang chạy trên Mac OS X 10.8.4.
Bạn có thể đã thêm khóa công khai ssh của máy tính đang cố gắng kết nối với máy chủ. Bạn có chắc chắn khi bạn cố kết nối bạn nhận được lời nhắc mật khẩu không? – orezvani
Tôi cũng đang gặp phải vấn đề tương tự. Đó có phải là cách giải quyết ? – Nikole