I want to execute a powershell command on multiple windows machines.
At first the working command:
# ssh foo@windows_hostname powershell -command hostname
foo@windows_hostname's password:
windows_hostname
But as I need to execute the command in a loop I need to set the -n
option or redirect stdin to /dev/null
, but with this option I don't get any stdout back:
# ssh -n foo@windows_hostname powershell -command hostname
foo@windows_hostname's password:
#
The same problem is present if I use the cmd.exe:
# ssh foo@windows_hostname cmd.exe /c hostname
foo@windows_hostname's password:
#
But I don't have this problem with a linux machine and ssh only:
# ssh -n foo@linux_hostname hostname
foo@linux_hostname's password:
linux_hostname
I tried multiple variantes like -nT
, -nf
, -nt
, but the result stays blank.
This is the verbose output of ssh
when it works:
debug1: Next authentication method: password
foo@windows_hostname's password:
debug1: Authentication succeeded (password).
Authenticated to windows_hostname ([10.0.0.1]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = POSIX
debug1: Sending env LC_CTYPE = de_DE.UTF-8
debug1: Sending command: powershell -command hostname
windows_hostname
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 4304, received 2008 bytes, in 0.9 seconds
Bytes per second: sent 4565.1, received 2129.8
debug1: Exit status 0
And when it fails:
debug1: Next authentication method: password
foo@windows_hostname's password:
debug1: Authentication succeeded (password).
Authenticated to windows_hostname ([10.0.0.1]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = POSIX
debug1: Sending env LC_CTYPE = de_DE.UTF-8
debug1: Sending command: powershell -command hostname
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 4320, received 1976 bytes, in 0.1 seconds
Bytes per second: sent 37875.5, received 17324.5
debug1: Exit status 0
EDIT: Found something interesting. Although the verbose output says it sent the command, it did not:
# ssh foo@windows_hostname 'powershell -command date | Out-File C:\test.txt'
foo@windows_hostname's password:
plato:~ # ssh foo@windows_hostname 'powershell -command cat C:\test.txt'
foo@windows_hostname's password:
Samstag, 13. August 2022 16:15:53
plato:~ # ssh -n foo@windows_hostname 'powershell -command date | Out-File C:\test.txt'
foo@windows_hostname's password:
plato:~ # ssh foo@windows_hostname 'powershell -command cat C:\test.txt'
foo@windows_hostname's password:
Samstag, 13. August 2022 16:15:53