4
$\begingroup$

I want to run Windows PowerShell commands via Mathematica. Specifically, I need to access a ssh server which is password-protected. When I do this using the PowerShell, I get the following prompt

enter image description here

where I can then type my password. However, while using RunProcess in Mathematica, I do not get any password requests and the evaluation does not end. Any way around this?

Here is my code, which works for any other PowerShell commands, inspired by this post,

shell[s_] := RunProcess[{"powershell", s}, "StandardOutput"]
shell["ssh [user]@[domain]"]

for which the second evaluation does not end. How can I overcome this, somehow introducing the password, and access the server? I am also aware of this question, but it does not really help me. Any ideas?

Update: The server has now imposed a Multi-Factor Authentication (MFA) process for which I need to introduce an OTP (following the password), adding to the problem. However, I think a solution to the first issue might potentially also fix this. Any ideas?

$\endgroup$
6
  • 6
    $\begingroup$ What do you actually want to achieve? The title asks for "running PowerShell", but in the body of the question you ask about how to log in with ssh, but without saying what you want to do once you've logged in. Presumably, you want to run a command. The "right way" to do that is to (1) set up public key authentication (2) run ssh user@domain command directly, and do not use PowerShell. Recent versions of Mathematica even have built-in tools to make this much simpler, see e.g. RemoteRunProcess[]. $\endgroup$
    – Szabolcs
    Commented Nov 8, 2022 at 12:58
  • 6
    $\begingroup$ In summary, this reads like an XY problem. Please update the question and explain what you actually want to achieve. $\endgroup$
    – Szabolcs
    Commented Nov 8, 2022 at 12:58
  • $\begingroup$ @Szabolcs I have edited the question. Hopefully now it is more clear. I am not familiar with setting up public key authentication. I have not attempted any solution. $\endgroup$
    – sam wolfe
    Commented Nov 9, 2022 at 11:57
  • $\begingroup$ I run commands remotely from Windows with ssh very frequently using RemoteRunProcess. It works great. You have to make sure to add your private key to the ~/.ssh/authorized_keys file in the server. I can expand but this is not the question as it doesn't use PowerShell. $\endgroup$ Commented Nov 9, 2022 at 13:20
  • 1
    $\begingroup$ I am not sure if removing the need of entering the ssh password will also skips the MFA, but you can try. For this copy the public (not private, my mistake) key from FileNameJoin[{$UserBaseDirectory, "ApplicationData", "SecureShellLink", "Keys"}] to a file named authorized_keys in the .ssh directory in your home directory in the server. It is just one line so you can just copy and paste it or do echo "the key" >> ~/.ssh/authorized_keys $\endgroup$ Commented Nov 14, 2022 at 14:49

1 Answer 1

2
+150
$\begingroup$

WindowsPowerShell

powershell = FileNameJoin[
    {
        Environment["SystemRoot"]
        , "system32"
        , "WindowsPowerShell"
        , "v1.0"
        , "powershell.exe"
    }
]

session = StartExternalSession[{"Shell", "Target"-> powershell}];

Now you have a session you can run anything you do in a shell

ExternalEvaluate[session, "ping google.com"]
Pinging google.com [142.250.178.14] with 32 bytes of data:
Reply from 142.250.178.14: bytes=32 time=3ms TTL=57
Reply from 142.250.178.14: bytes=32 time=4ms TTL=57
Reply from 142.250.178.14: bytes=32 time=6ms TTL=57
Reply from 142.250.178.14: bytes=32 time=6ms TTL=57

Ping statistics for 142.250.178.14:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 3ms, Maximum = 6ms, Average = 4ms

Including SSH sessions, like with plink (part of putty) which does allow using the password as a command-line parameter.

ExternalEvaluate[session, "plink [email protected] -pw password"]

Other ways to avoid typing a password, like using Public cryptographic keys, are well documented, and explaining in detail not within the scope of Mathematica StackExchange.

RemoteRunProcess

You can avoid going through a "Shell" by using RemoteRunProcess or access remote files using RemoteFile

RemoteRunProcess[
    "test.rebex.net"
    ,"ls -la readme.txt"
    , Authentication -> <|
        "Username" -> "demo",
        "Password"->"password"
    |>
]
<|
    "ExitCode" -> 127
    , "StandardOutput" -> "-rw------- 1 demo users        405 Dec 17  2021 readme.txt"
    , "StandardError" -> "ls: error in `ls': Path contains invalid characters"
|>

Streams with StartProcess

In principle one could also do

proc = StartProcess[{powershell,"-NonInteractive"}];
WriteLine[proc, "ssh [email protected]"];

and read the outputs with

ReadString[ProcessConnection[proc, "StandardError"],EndOfBuffer] 
ReadString[proc, EndOfBuffer]

But I didn't manage to craft an example because of the error

Pseudo-terminal will not be allocated because stdin is not a terminal.

I trust the other two solutions should be enough.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.