How Can I Batch SSH into Multiple Devices and Run a Command?

0
7
Asked By TechieNerd123 On

I'm looking for a simple PowerShell script that lets me SSH into a list of devices and execute a command automatically. The devices are all new and on a local network, and I don't care about the host key since it will change after I finish this initial task. I just want to accept the host key the first time, run a command, and then move on to the next device. I've got a CSV file with all the IPs listed. When I try to pipe 'y' to PLINK, it just hangs and does nothing. I need a straightforward solution to do this for 100 devices without manually connecting to each one. Is there an easy way to achieve this?

5 Answers

Answered By OldSchoolTechie On

Honestly, putty and plink are pretty dated. If you are set on scripting, why not use native SSH commands or tools instead? That could simplify your life a lot!

Answered By ScriptSorcerer On

You can try using the `-o StrictHostKeyChecking=accept-new` option with SSH to automatically accept host keys. Alternatively, using `-o StrictHostKeyChecking=no` will also skip the prompt entirely, which should help you get through your list without any hang-ups.

Answered By NinjaCoder88 On

Consider using threads or jobs for the connections if you're facing timeouts or slowdowns. Parallelizing your script can significantly speed up the process of handling multiple devices.

Answered By CmdLineJunkie On

I think you're overcomplicating it a bit. You don’t actually need to log in first to accept the key; just connect. Try this in your PowerShell script: `echo y | plink -ssh user@device "exit"`. That should get you where you need to go without the hang-ups you're experiencing.

Answered By PowerShellWizard On

Have you considered using Posh-SSH? It's a PowerShell module that simplifies SSH connections and could work better for your case. It allows you to handle the host keys more flexibly. Plus, instead of PLINK, it integrates more smoothly with PowerShell scripting.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.