Hi,
I've knocked the following code up to change the vlan on a switchport, is there a more efficient way of doing this rather than declaring a new var each time, also how do I catch the unhandled or should I say the un-expected?
I've knocked the following code up to change the vlan on a switchport, is there a more efficient way of doing this rather than declaring a new var each time, also how do I catch the unhandled or should I say the un-expected?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Renci.SshNet;
namespace SSHTest
{
class Program
{
static void Main(string[] args)
{
// Establish connection with the device
SshClient shell = new SshClient("192.168.0.254", "username", "password");
shell.Connect();
if (shell.IsConnected)
{
Console.WriteLine("Connected");
}
// Create Stream
var stream = shell.CreateShellStream("dumb", 80, 24, 800, 600, 1024);
var resp1 = stream.Expect(new Regex(@".+>$"), new TimeSpan(0, 0, 5));
//Console.WriteLine(resp1);
stream.WriteLine("enable");
var resp2 = stream.Expect(new Regex("Password:"), new TimeSpan(0, 0, 5));
//Console.WriteLine(resp2);
stream.WriteLine("enablepasswordhere");
var resp3 = stream.Expect(new Regex(@".+#$"), new TimeSpan(0, 0, 5));
//Console.WriteLine(resp3);
stream.WriteLine("conf t");
var resp4 = stream.Expect(new Regex(@"\(config\)#"), new TimeSpan(0, 0, 5));
//Console.WriteLine(resp4);
stream.WriteLine("interface FastEthernet0/2");
var resp5 = stream.Expect(new Regex(@"\(config-if\)"), new TimeSpan(0, 0, 5));
//Console.WriteLine(resp5);
stream.WriteLine("switchport access vlan 1");
var resp6 = stream.Expect(new Regex(@"\(config-if\)"), new TimeSpan(0, 0, 5));
//Console.WriteLine(resp6);
stream.WriteLine("end");
var resp7 = stream.Expect(new Regex(@".+#$"), new TimeSpan(0, 0, 5));
//Console.WriteLine(resp7);
stream.WriteLine("wr mem");
var resp8 = stream.Expect(new Regex(@"[OK]"), new TimeSpan(0, 0, 10)); // Extended time for switches with busy cpu
Console.WriteLine(resp8);
// Disconnect
shell.Disconnect();
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
}
}
Thanks in advance, and yes I know the code is crude, I'm new to c#.