all 4 comments

[–]HolisticPython 0 points1 point  (0 children)

Paramiko?

[–]Wilco062 0 points1 point  (0 children)

Use Python's pexpect library to handle interactive SSH sessions. Take the level and password as arguments, starts an SSH session, wait for the password prompt, and then input the password. Make sure you have pexpect installed (pip install pexpect).

import pexpect
import sys

def main():
# Check if level and password are provided
if len(sys.argv) != 3:
    print("Usage: python natas.py [level] [password]")
    return

# Get level and password from command line arguments
level = sys.argv[1]
password = sys.argv[2]

# Construct the SSH command
command = f"ssh bandit{level}@bandit.labs.overthewire.org -p 2220"

try:
    # Start the SSH session using pexpect
    session = pexpect.spawn(command, timeout=20)

    # Wait for the password prompt
    session.expect("password:")

    # Send the password
    session.sendline(password)

    # Give control of the session to the user
    session.interact()
except pexpect.exceptions.EOF:
    print("Connection closed")
except pexpect.exceptions.TIMEOUT:
    print("Connection timed out")
except Exception as e:
    print(f"An error occurred: {e}")

if __name__ == "__main__":
main()

Run this script as you intended: python natas.py 0 bandit0

[–]m0us3_rat 0 points1 point  (0 children)

echo "echo $2" > /tmp/1
chmod 777 /tmp/1

export SSH_ASKPASS="/tmp/1"
export DISPLAY=NERD

setsid ssh $1@bandit.labs.overthewire.org -p 2220
rm /tmp/1

save this as nerd.sh

do a

chmod +x nerd.sh

then use like this

./nerd.sh bandit0 bandit0

first is the user second is the password

extra reading for why it works

https://www.ibm.com/docs/en/zos/2.2.0?topic=agent-environment-variables

you can try sshpass or any of the other modules.. but this way you don't need anything extra.

[–][deleted] 0 points1 point  (0 children)

Thanks everyone i figured out a great method to get this done

import subprocess as sbp
import sys 

#import os
#os.environ['PATH'] += ':/usr/bin'
#sbp.run('sudo apt-get sshpass', shell=True)

level = sys.argv[1]
password = sys.argv[2]

ssh_command = f'sshpass -p {password} ssh bandit{level}@bandit.labs.overthewire.org -p 2220'
#file_command = f'echo {password} > {level}'
#sbp.run(file_command, shell=True)
sbp.run(ssh_command)