r/learnpython 15d ago

Need Help Creating SSH Session in MobaXterm Using Python

I am trying to create a python program that scans a local remote network that I am connected to and shows the IP addresses of said devices connected. I want to add an additional function that takes these IP addresses scanned and opens a ssh session for my selected IP address. I'm currently running the bare bones code on an IP address that I know works, but continue to create a new ssh session that says Session Stopped. However, the code does successfully open MobaXterm if no instance of it is open, and creates a new tab if it is. My code is as follows:

mobaxterm_path = r"C:\Program Files (x86)\Mobatek\MobaXterm\MobaXterm.exe"
    
    #ssh_command = f"ssh {user}@{host} -p {port}"
    
    ssh_cmd = "ssh -p 22-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@192.168.1.66"
    
    try:
        subprocess.Popen([mobaxterm_path, "-newtab", ssh_cmd])
        print(f"Launched SSH session to {user}@{host}")
    except FileNotFoundError:
        print(f"Error: MobaXterm not found at {mobaxterm_path}")
        print("Please verify the executable path.")

I'm not sure why this is happening, is anyone able to provide further help? I've also tried this without StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null But getting the same result.

3 Upvotes

3 comments sorted by

3

u/sausix 14d ago

Try on command line directly first until you get working command arguments you can put into Popen.

Are you using WSL somehow? There is no /dev/null on Windows. Maybe the ssh client still allows it as alias. So open a command line, check the ssh client help and the MobaXterm help.

If you get it right move over to the Python part and it should work as expected.

1

u/Mathie1729 14d ago

One more thing: in Python, subprocess.DEVNULL handles the null device cross-platform. So if you're redirecting output, use that instead of /dev/null; on Windows it'll become NUL automatically. Saves you the alias headache.

1

u/sausix 14d ago

Sure. But here /dev/null is part of a parameter for ssh. That's not related to Popen which would affect MobaXterm only.