Question Invoke-Plink (SSH compatible Cisco)
- fabien
- Auteur du sujet
- Hors Ligne
- Membre junior
-
Réduire
Plus d'informations
- Messages : 26
- Remerciements reçus 0
il y a 14 ans 1 mois #11164
par fabien
Invoke-Plink (SSH compatible Cisco) a été créé par fabien
Bonjour,
Script permettant de lancer une commande ou un fichier de commande via Plink (compatible Cisco). Pour les fichiers de commandes, comme la redirection de l'entrée standard n'a pas l'air d'exister sous PowerShell, je passe par un cmd.exe.
[code:1]
Function Invoke-Plink {
<#
.SYNOPSIS
Permet de lancer une commande ou un fichier de commande via Plink (compatible Cisco).
.EXAMPLE
Invoke-Plink -Hostname SwitchA -filePath ./ScriptSwitch.txt
Avec ./ScriptSwitch.txt contenant :
conf t
snmp community PowerTest
exit
exit
exit
.EXAMPLE
\"SwitchA\",\"SwitchB\",\"SwitchC\" | Invoke-Plink -Cmdline \"show snmp community\"
.DESCRIPTION
Permet de lancer une commande ou un fichier de commande via Plink (compatible Cisco).
#>
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true)]$Hostname,
[String]$filePath,
[String]$Cmdline,
[parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$Credential,
[String]$Domain = \"test.fr\",
[Switch]$log,
[Switch]$VerboseMode
)
begin {
$plinkpath = \"C:\Program Files\plink.exe\"
# Vérification de la présence de plink
if (-not (test-path $plinkpath)){
throw \"$plinkpath introuvable.\"
return $false
}
# Vérification des arguments
if ($filePath -ne \"\"«»){
try{
$cmdfile = (resolve-path $filePath -erroraction stop).path
$plinkargs = \"-batch < $cmdfile\"
}catch{
throw $error[0]
return $false
}
# Vérification du bon paramétrage
if ($Cmdline -ne \"\"«»){
throw \"Les arguments -filepath et -cmdline ne peuvent pas être utilisés simultanément.\"
return $false
}
}elseif($Cmdline -ne \"\"«»){
$plinkargs = \"`\"$Cmdline`\"\"
}else{
throw \"Arguments -filepath ou -cmdline obligatoire.\"
return $false
}
# Gestion du username
if ($Credential.username.startswith(\"\\"«»)){
$username = $Credential.username.split(\"\\"«»)[1]
}else{
$username = $Credential.username
}
# Récupération du mot de passe en clair (plink ne supporte pas l'objet [System.Security.SecureString])
$BSTR = [System.Runtime.InteropServices.marshal]::«»SecureStringToBSTR($Credential.password)
$password = [System.Runtime.InteropServices.marshal]::«»PtrToStringAuto($BSTR)
[System.Runtime.InteropServices.marshal]::ZeroFreeBSTR($BSTR)
}
process{
if(-not $Hostname.Contains(\".\"«»)){
$Hostname += \".$Domain\"
}
write-host -ForegroundColor \"yellow\" $Hostname
$plinkcmd = \"$plinkpath -ssh -l $username -pw $password $Hostname $plinkargs\"
Test-Connection $Hostname -Count 1 -ErrorAction SilentlyContinue | out-null
if ($?){
# Gestion des logs
if ($filePath -ne \"\" -or $log){
$logfile = (Get-Location).path + \"\\" + $Hostname + \".log\"
$plinkcmd += \" > $logfile\"
}
Write-Verbose $plinkcmd -verbose:$VerboseMode
# Lancement de la commande + attente
if ($filePath -ne \"\"«»){
$cmd_args = \"/c `\"$plinkcmd`\"\"
$PlinkCMD = [System.Diagnostics.Process]::«»Start(\"cmd.exe\",$cmd_args)
$PlinkCMD.WaitForExit()
}else{
Invoke-Expression -Command $plinkcmd
}
# Affichage ou log
if ($filePath -ne \"\" -and (-not $log)){
Get-Content $logfile
Remove-Item $logfile -Force
}
}else{
Write-Error \"Connexion impossible avec $Hostname.\"
}
}
end{
# Suppression du mot de passe en clair
remove-variable password
return $true
}
}
[/code:1]<br><br>Message édité par: Phebus, à: 16/03/12 11:08
Script permettant de lancer une commande ou un fichier de commande via Plink (compatible Cisco). Pour les fichiers de commandes, comme la redirection de l'entrée standard n'a pas l'air d'exister sous PowerShell, je passe par un cmd.exe.
[code:1]
Function Invoke-Plink {
<#
.SYNOPSIS
Permet de lancer une commande ou un fichier de commande via Plink (compatible Cisco).
.EXAMPLE
Invoke-Plink -Hostname SwitchA -filePath ./ScriptSwitch.txt
Avec ./ScriptSwitch.txt contenant :
conf t
snmp community PowerTest
exit
exit
exit
.EXAMPLE
\"SwitchA\",\"SwitchB\",\"SwitchC\" | Invoke-Plink -Cmdline \"show snmp community\"
.DESCRIPTION
Permet de lancer une commande ou un fichier de commande via Plink (compatible Cisco).
#>
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true)]$Hostname,
[String]$filePath,
[String]$Cmdline,
[parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$Credential,
[String]$Domain = \"test.fr\",
[Switch]$log,
[Switch]$VerboseMode
)
begin {
$plinkpath = \"C:\Program Files\plink.exe\"
# Vérification de la présence de plink
if (-not (test-path $plinkpath)){
throw \"$plinkpath introuvable.\"
return $false
}
# Vérification des arguments
if ($filePath -ne \"\"«»){
try{
$cmdfile = (resolve-path $filePath -erroraction stop).path
$plinkargs = \"-batch < $cmdfile\"
}catch{
throw $error[0]
return $false
}
# Vérification du bon paramétrage
if ($Cmdline -ne \"\"«»){
throw \"Les arguments -filepath et -cmdline ne peuvent pas être utilisés simultanément.\"
return $false
}
}elseif($Cmdline -ne \"\"«»){
$plinkargs = \"`\"$Cmdline`\"\"
}else{
throw \"Arguments -filepath ou -cmdline obligatoire.\"
return $false
}
# Gestion du username
if ($Credential.username.startswith(\"\\"«»)){
$username = $Credential.username.split(\"\\"«»)[1]
}else{
$username = $Credential.username
}
# Récupération du mot de passe en clair (plink ne supporte pas l'objet [System.Security.SecureString])
$BSTR = [System.Runtime.InteropServices.marshal]::«»SecureStringToBSTR($Credential.password)
$password = [System.Runtime.InteropServices.marshal]::«»PtrToStringAuto($BSTR)
[System.Runtime.InteropServices.marshal]::ZeroFreeBSTR($BSTR)
}
process{
if(-not $Hostname.Contains(\".\"«»)){
$Hostname += \".$Domain\"
}
write-host -ForegroundColor \"yellow\" $Hostname
$plinkcmd = \"$plinkpath -ssh -l $username -pw $password $Hostname $plinkargs\"
Test-Connection $Hostname -Count 1 -ErrorAction SilentlyContinue | out-null
if ($?){
# Gestion des logs
if ($filePath -ne \"\" -or $log){
$logfile = (Get-Location).path + \"\\" + $Hostname + \".log\"
$plinkcmd += \" > $logfile\"
}
Write-Verbose $plinkcmd -verbose:$VerboseMode
# Lancement de la commande + attente
if ($filePath -ne \"\"«»){
$cmd_args = \"/c `\"$plinkcmd`\"\"
$PlinkCMD = [System.Diagnostics.Process]::«»Start(\"cmd.exe\",$cmd_args)
$PlinkCMD.WaitForExit()
}else{
Invoke-Expression -Command $plinkcmd
}
# Affichage ou log
if ($filePath -ne \"\" -and (-not $log)){
Get-Content $logfile
Remove-Item $logfile -Force
}
}else{
Write-Error \"Connexion impossible avec $Hostname.\"
}
}
end{
# Suppression du mot de passe en clair
remove-variable password
return $true
}
}
[/code:1]<br><br>Message édité par: Phebus, à: 16/03/12 11:08
Connexion ou Créer un compte pour participer à la conversation.
Temps de génération de la page : 0.067 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Contributions à la communauté
- Invoke-Plink (SSH compatible Cisco)