r/PowerShell • u/dnuohxof-2 • Mar 24 '26
Batch SSH in to new devices, issue command, exit. How hard can this be!?!?
All I want is a simple script to use PuTTY or something to SSH in to a list of devices, Auto accept the host key, issue a command, and exit.
I do not give one single F**K about security with the host key.
I have 100 devices. Brand new. Local network. I need to add them to an orchestrator that will update SSH keys and stuff. Host key won't even matter because once my task is done, the key changes anyway.
What I need is a first login to accept the host key, issue a command and move on to the next. The hosts are listed in a csv.
No, piping Y to PLINK does not work, it just hangs. Example:
foreach ($d in $devices) {
$ip = $d.IP
Write-Host "Running on $ip ..." -ForegroundColor Cyan
echo y | plink $ip -P $Port -l $Username -pw $Password -no-antispoof
}Does absolutely nothing.
No I am not going to manually connect to 100 devices just to accept the stupid host key
Yes, I am aware the folks at PuTTY are stubborn idiots who think they know better than everyone else.
All I want is a powershell script to reference a list of hosts in a CSV, accept the host key, issue a single command, and exit. Rinse, repeat 100 times. Is this possible!? Copilot, Google and ChatGPT fail hard and keep sending me in circles
12
u/vermyx Mar 24 '26
Apparently security is being a stubborn idiot...
But to answer your question, you realize plink has a -batch parameter right?
3
u/dpwcnd Mar 24 '26
you dont have to log in to get the key, just connect
this was in one of my powershells
echo y | & plink -ssh $ip
or
echo y | plink -ssh user@device "exit";
dont remember which one i used to do something similar
24
u/PantsOnHeadCrazy Mar 24 '26
ssh -o StrictHostKeyChecking=accept-new
Or ssh -o StrictHostKeyChecking=no
2
u/BlackV Mar 24 '26
not sure that is saying anything about them more than it is about you
but if you don't like the 3rd party product, use the native options or a different 3rd party tool
1
u/mats_o42 Mar 24 '26
Why not do it native in powershell?
Invoke-command -Hostname uses ssh and you can send options to ssh with the -options flag
3
u/Adeel_ Mar 24 '26
Use Posh-SSH
1
u/AlfaNovember Mar 24 '26
If you don’t mind paying a few bucks for good software, Vandyke SecureCRT and related tools are amenable to scripting and automation.
1
u/titlrequired Mar 24 '26
Try using Posh-SSH?
0
u/enforce1 Mar 24 '26
Use threads or jobs or parallel