public static JsonCommand ResetExpectSession(int timeout, ExpectType expect_type = ExpectType.P2P) { var jc = new JsonCommand(CommandType.ResetExpectSession, "", timeout); jc.ExpectType = expect_type; return(jc); }
public static JsonCommand ClearExpectBuffer(int timeout, ExpectType expect_type = ExpectType.P2P) { var jc = new JsonCommand(CommandType.ClearExpectBuffer, "", timeout); jc.ExpectType = expect_type; return(jc); }
public static JsonCommand ConnectRemouteAS(String asIP, Int32 asPort, String cmd, Int32 timeout) { String result = String.Empty; byte[] bytes = new byte[4096]; // Connect to a remote device IPAddress ipAddress = null; if (!IPAddress.TryParse(asIP, out ipAddress)) {//Dns.GetHostEntry may return wrong IP address if some threads have queried a while ago foreach (var addr in Dns.GetHostEntry(asIP).AddressList) { Logging.logMessage(String.Format("{0} addr list:[{2}]", asIP, addr.ToString())); } foreach (var addr in Dns.GetHostEntry(asIP).AddressList) { if (addr.AddressFamily == AddressFamily.InterNetwork) { ipAddress = addr; Logging.logMessage(String.Format("{0} resolve IPv4={2}", asIP, ipAddress.ToString())); break; } } } if (ipAddress == null) { Logging.logMessage("Cannot resolve ip:", asIP); return(null); } IPEndPoint remoteEP = new IPEndPoint(ipAddress, asPort); // Create a TCP/IP socket. Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect the socket to the remote endpoint. Catch any errors. sender.Connect(remoteEP); Logging.logMessage(String.Format("{0} Socket connected to {1}", asIP, sender.RemoteEndPoint.ToString())); // Encode the data string into a byte array. var jc = JsonCommand.RunProgram(cmd, timeout); // Send the data through the socket. var jc_string = JSON.Stringify(jc); byte[] msg = Encoding.ASCII.GetBytes(jc_string); Logging.logMessage(String.Format("Send as command:[{0}]", jc_string)); int bytesSent = sender.Send(msg); sender.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout + 5000); // Receive the response from the remote device. int bytesRec = sender.Receive(bytes); result = Encoding.UTF8.GetString(bytes, 0, bytesRec); var json_result = JSON.Parse <JsonCommand>(result); Logging.logMessage(String.Format("{0} Run Remote command {1} response:{2}", asIP, cmd, result)); // Release the socket. sender.Shutdown(SocketShutdown.Both); sender.Close(); return(json_result); }
public static JsonCommand ExpectOutput(string command, string regex_string, int timeout, ExpectType expect_type = ExpectType.P2P) { var jc = new JsonCommand(CommandType.ExpectOutput, command, timeout); jc.RegexString = regex_string; jc.ExpectType = expect_type; return(jc); }
public static JsonCommand RunProgram(string command, int timeout) { var jc = new JsonCommand(CommandType.RunProgram, command, timeout); return(jc); }