Question Utilisation de Telnet/ping in PowerShell

Plus d'informations
il y a 13 ans 8 mois #7485 par Arthur

**Ce n'est pas un code qui provient de moi mais je ne me souviens plus de qui il vient.


halr9000.com/article/397 :P

Ce script est en effet incomplet il manque pas mal de choses mais il propose de bonnes bases.

il ne ferme pas le socket à la fin. (Pas bien )

Sinon il y a plusieurs pistes :

- avec plink.exe je pense qu'il est possible de faire du telnet, si putty peut faire du telnet plink doit le faire aussi non? :dry:

- Avec une librairie C# il y a pas mal de libraire C# pour faire du Telnet sur le web.C'est un peut plus difficile mais c'est plus fun. :lol:

-Il y a surement un moyen d'adapter ce script :
www.leeholmes.com/blog/ScriptingNetworkT...onsInPowerShell.aspx

pour qu'il fasse du telnet.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 8 mois #7511 par Alan Pinard
Bonjour à tous,

Voici ce que j'ai pu dénicher sur le net grâce à Bilbao.

[code:1]
# ==============================================================================================
#
# Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 4.1
#
# NAME:
#
# AUTHOR:
# DATE : 3/25/2009
#
# COMMENT: Code originally found at www.leeholmes.com/blog/ScriptingNetworkT...onsInPowerShell.aspx
#
# ==============================================================================================

## Connect-Computer.ps1
## Interact with a service on a remote TCP port
param(
[String] $remoteHost = \"localhost\",
[Int] $port = 23,
[String] $inputObject,
[Int] $commandDelay = 100,
[String]$Username = \"<USERNAME>\",
[String]$Password = \"<PASSWORD>\",
[String]$Cmd = \"<COMMAND>\"
)

[string] $output = \"\"

##$errorActionPreference = \"SilentlyContinue\"

$currentInput = $inputObject

if(-not $currentInput)
{
$SCRIPT:currentInput = @($input)
}
##I want it to always run batched, not wait for input, so I commented out the check and forced $scriptedMode
##$scriptedMode = [bool] $currentInput
$scriptedMode = 1

##Here we have to replace username, password, and command with what we want to happen once the telnet session is opened.
##$currentInput = (\"username\",\"password\",\"command\"«»)
$currentInput = ($Username,$Password,$Cmd)

function Main
{
## Open the socket, and connect to the computer on the specified port
## if(-not $scriptedMode)
## {
write-host \"Connecting to $remoteHost on port $port\"
## }

trap
{
Write-Host \"Could not connect to remote computer: $_\"
continue
}
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)

## if(-not $scriptedMode)
## {
## write-host \"Connected. Press ^D followed by [ENTER] to exit.`n\"
## }

$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)

## Create a buffer to receive the response
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding

$sentCommands = 0
while(-not $sentCommands)
{
## Receive the output that has buffered so far
$SCRIPT:output += GetOutput

## If we're in scripted mode, send the commands,
## receive the output, and exit.
if($scriptedMode)
{
foreach($line in $currentInput)
{
$writer.WriteLine($line)
$writer.Flush()
Start-Sleep -m $commandDelay
}

$SCRIPT:output += GetOutput
$sentCommands = 1
break
}
## If we're in interactive mode, write the buffered
## output, and respond to input.
else
{
if($output)
{
foreach($line in $output.Split(\"`n\"«»))
{
write-host $line
}
$SCRIPT:output = \"\"
}

## Read the user's command, quitting if they hit ^D
$command = read-host
if($command -eq ([char] 4)) { break; }

## Otherwise, Write their command to the remote host
$writer.WriteLine($command)
$writer.Flush()
}
}

## Close the streams
$writer.Close()
$stream.Close()

## If we're in scripted mode, return the output
if($scriptedMode)
{
$output
[string] $output = \"\"
}
}

## Read output from a remote host
function GetOutput
{
$outputBuffer = \"\"
$foundMore = $false

## Read all the data available from the stream, writing it to the
## output buffer when done.
do
{
## Allow data to buffer for a bit
start-sleep -m 500

## Read what data is available
$foundmore = $false
while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
$foundmore = $true
}
} while($foundmore)

$outputBuffer
}

. Main
[/code:1]

De mon côté, il fonctionne. J'ai testé avec un commutateur CISCO 2960G avec nom d'usager et mot de passe.

Vous devez modifier les informations de paramètres afin d'y mettre les votre.

Vous pouvez rediriger le résultat dans un fichier texte avec le redirecteur \&quot;&gt;\&quot;.<br><br>Message édité par: Versiona, à: 2/08/10 15:20

Alan Pinard
Version A

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 12 ans 7 mois #10274 par arnold
bonjour,

Il m'est impossible de comuniquer avec telnet.
J'ai utiliser \&quot;192.168.1.1:23\&quot;

il y a une possibilte de voir dans le router de
l'existence du telnet serv?

merci pour votre aide

arnold

============================

?????? ?? login: root

Password: ****

You are not allowed to log in in the next 240 seconds
term len 0

You are not allowed to log in in the next 239 seconds
en

You are not allowed to log in in the next 238 seconds
enablepassword

You are not allowed to log in in the next 237 seconds
show run

You are not allowed to log in in the next 236 seconds

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.073 secondes
Propulsé par Kunena