SSH Script to Control Router
HI, I'm able to SSH into the router successfully using putty.
But it's not working when I'm trying to automate the script using a bat files, ps1 files. I've even tried paramiko and netmiko with no avail.
Here's my script for instance
# Variables
$router_ip = "192.168.0.1"
$username = "admin"
$password = "password_goes_here"
$plink_path = "C:\Program Files\PuTTY\plink.exe" # Adjust this path as needed
# Read commands from the file
$commands = Get-Content -Path "commands.txt" -Raw
# Construct the plink command
$plink_command = "&'$plink_path' -ssh $username@$router_ip -pw $password -batch -m commands.txt"
# Execute the plink command and capture output and error
Write-Output "Executing plink command..."
$output = Invoke-Expression $plink_command 2>&1 | Out-String
# Display the output
Write-Output "Output:"
Write-Output $output
# Check if the VLAN creation was successful
if ($output -like "*VLAN creation and configuration completed*") {
Write-Output "VLAN created successfully"
} else {
Write-Output "VLAN creation failed. Please check the router configuration."
}
And the commands.txt is as follows:
enable
configure
vlan 11
name created_using_automated_SSH
exit
exit
Any idea how to go about it?
Many thanks!