Skip to content

Instantly share code, notes, and snippets.

@rkrishnasanka
Last active May 31, 2022 07:18
Show Gist options
  • Save rkrishnasanka/5944263 to your computer and use it in GitHub Desktop.
Save rkrishnasanka/5944263 to your computer and use it in GitHub Desktop.
uses pexpect to automate ssh login
import pxssh
def send_command(s, cmd):
s.sendline(cmd)
s.prompt()
print s.before
def connect(user, host, password):
try:
s = pxssh.pxssh()
s.login(host, user, password)
return s
except:
print '[-] Error Connecting'
exit(0)
def main():
host = 'localhost'
user = 'root'
password = 'toor'
s = connect(user, host , password)
send_command(s, 'cat /etc/shadow | grep root')
if __name__ == '__main__':
main()
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, '[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
def main():
host = 'localhost'
user = 'root'
password = 'toor'
child = connect(user, host , password)
send_command(child, 'cat /etc/shadow | grep root')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment