Ok, I am reading some stuffs about brute forcing ssh server. So here is a python module that deals with possible interactive prompts.
The target server is a cisco router, I have put in all possible expected prompts.Such as:
1. if there is a timeout?
2. if there is an unknown ssh key prompting you to accept or not?
3. put in a password when the password prompt appears.
This code sample can be modified to read in dictionary of passwords, supposed the username is “admin” or “cisco”, in this example the code is against the username “cisco”
import pexpect prompt = ">" def send_command(session,cmd): session.sendline(cmd) session.expect(prompt) print(session.before) def connect(username,password,host): ssh_unknown_key = "Are you sure you want to continue connecting" conn_params = "ssh " + username + "@" + host session = pexpect.spawn(conn_params) response = session.expect([pexpect.TIMEOUT, ssh_unknown_key, '[P|p]assword:']) if response == 0: print("Error connecting!") return if response == 1: session.sendline('yes') response = session.expect([pexpect.TIMEOUT, '[P|p]assword:']) if response == 0: print("Error connecting!") return session.sendline(password) session.expect(prompt) return session def main(): username = 'cisco' password = 'cisco' host = '192.168.1.150' cmd = 'sh version | in Cisco IOS' session = connect(username,password,host) send_command(session,cmd) if __name__ == '__main__': main()
The result looks like this:
'sh version | in Cisco IOS\r\nCisco IOS Software, Linux Software (I86BI_LINUX-ADVENTERPRISEK9-M), Version 15.5(2)T, DEVELOPMENT TEST SOFTWARE\r\nrouter'