Cisco – Ejecutar comando en router Cisco via ssh con c# y libreria Renci.SshNet
Posted in cisco, Networking, Scripting, Sistemas, Utilidades on Jun 21st, 2012
Nuevos Post: Cisco-Ejecutar comando en router Cisco via ssh con c# y libreria Renci.SshNet http://t.co/AOk3lyIk
@javcasta
Javier Castañón
Referencia:
Habilitar SSH en IOS o dispositivo Cisco
El código:
//hay que añadir la libreria al proyecto, en mi caso he usado SharpDeveloper
// Y declarar su uso
using Renci.SshNet;
//....
// Solo posteo el código interesante, el que establece la conexion ssh y ejecuta el comando
//....
int puerto;
if ((textBox4.Text == "") || (textBox4.Text == null)){
textBox4.Text = "22";
}
//convertimos puerto del textBox4 a int
puerto = Convert.ToInt32(textBox4.Text);
puerto = int.Parse(textBox4.Text);
//ssh.net
//SshClient mysshclient = new SshClient("host",puerto,"usuario","clave");
SshClient mysshclient = new SshClient(textBox5.Text,puerto,textBox2.Text,textBox3.Text);
richTextBox1.ResetText();
mysshclient.Connect();
try {
if ((textBox1.Text != null) || (textBox1.Text != "")) {
var salida = mysshclient.RunCommand(textBox1.Text);
if (salida != null) {
richTextBox1.Text = salida.Result;
}
}
} catch (Exception mye) {
mysshclient.Disconnect();
MessageBox.Show(mye.ToString(),"Ha habido un error: ");
throw;
}
mysshclient.Disconnect();
comprobamos las conexiones ssh establecidas en el router
router#show ssh
Y voila
Clika en el altavoz para oir el post




















