Link to home
Start Free TrialLog in
Avatar of James Alston
James Alston

asked on

Using powershell automate logins to Cisco Switches over SSH with Plink

First I want to say that yes, I do know there are tools that already exist that can make this much easier. Unfortunately we don't have that option right now. It is coming in the future, but for now I am trying to get this to work

here is my script so far, pretty simple....

2..100 | % { plink "172.16.15.$_ "}

this is working good for me. This will hit switches in the range of 172.16.15.2 - 172.16.15.100. It allows me to login to our switches all in the PS window and rolls right to the next one in sequence when I type exit, instead of opening up a new putty session every time.

Is there a way I can add my username and password to this so that it automatically logs into the switch for me, versus having to type it in every time? The end goal is I want to add whatever config changes I want to make to the script and have powershell run through all of them and update the config.
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Probably if you merge what you have, with this answer:
https://www.experts-exchange.com/questions/29001099/Passing-Credentials-into-a-command-line.html

I recently created a PS script with UI for the credentials that will be asked once and used for several commands like the one you want.

function Do-job{
	#close main windows: 
	$global:Window.Close();
	#Get credentials
	Write-Host -ForegroundColor Magenta "Setting up Credential Object for $($tuser.Text)..."
	#$Global:Credentials = New-Object System.Management.Automation.PSCredential $tuser.Text,$secpasswd 
	#$global:ServiceCredentials =  New-Object System.Net.NetworkCredential($tuser.Text,$Tpwd.Password);

    try{
        2..100 | % { plink -ssh "172.16.15.$_" -l $($Tuser.Text) -pw $($pwd.Password) }
    }
    catch{
        Write-host -foreground "red" "$($_.Exception.Message)"
    }
}


[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms

[xml]$XAML=@'
<Window 
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
		xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
		xmlns:local="clr-namespace:WpfApp1"
		Title="Credentials XAML Form" Height="210" Width="361">
	<Grid Margin="1,1,-0.714,-1.429">
		<Label Content="User :" HorizontalAlignment="Left" Margin="45,54,0,0" VerticalAlignment="Top" FontSize="16" />
		<Label Content="Password :" HorizontalAlignment="Left" Margin="10,91,0,0" VerticalAlignment="Top" FontSize="16"/>
		<TextBox x:Name="TUser" HorizontalAlignment="Left" Height="25" Margin="99,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="236"  FontSize="14"/>
		<PasswordBox x:Name="Tpwd" HorizontalAlignment="Left" Height="25" Margin="99,97,0,0" VerticalAlignment="Top" Width="236"  FontSize="16" />
		<Label Content="Enter your credentials :" HorizontalAlignment="Left" Margin="62,15,0,0" VerticalAlignment="Top" FontSize="18" FontWeight="Bold"/>
		<Button x:Name="BOK" Content="OK" HorizontalAlignment="Left" Margin="36,135,0,0" VerticalAlignment="Top" Width="135"/>
		<Button x:Name="BExit" Content="Exit" HorizontalAlignment="Left" Margin="194,135,0,0" VerticalAlignment="Top" Width="135
				"/>
	</Grid>
</Window>

'@
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $XAML) 
$Global:Window=[Windows.Markup.XamlReader]::Load(  $reader ) 

#Connect to Controls 
#store Form objects
$XAML.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach {
		New-Variable  -Name $_.Name -Value $Global:Window.FindName($_.Name) -Force
	}

#add events to form objects
$BExit.Add_Click({$Global:Window.Close();Exit 0})
$BOK.Add_Click({Do-job;Exit 0})

$Window.ShowDialog() | Out-Null

 try{   
	if([string]::IsNullOrWhiteSpace($tuser.Text) -or [string]::IsNullOrWhiteSpace($Tpwd.Password) ){
		Write-Warning "Please fill up the username and Password for the script to continue"
		Write-Warning "Exiting... "
		exit 0;
	}
}
catch{
	$er =$_.Exception.Message
	Write-Error "there was an error. Message $($_.Exception.Message)"
	Write-Warning "Cleaning Up Sessions & Exiting, please run the Cmdlet again"
	exit -1  
}

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.