2010-03-03 8 views
2

Je souhaite exécuter le code suivant.Envoi de commandes à stdin et envoi de fin de transmission (Ctrl + D)

L'application que j'appelle attend des commandes comme User-Name = albert next line, une autre commande et ainsi de suite, jusqu'à ce que vous avez terminé.

Si vous deviez entrer ceci manuellement à partir de la ligne de commande, vous devez taper commande, appuyez sur entrée, tapez commande appuyez sur Entrée. Juste après la fin de la dernière commande, après avoir appuyé sur la touche Ctrl + D, appuyez sur Ctrl + D pour terminer le programme et revenir en indiquant ce qui s'est passé.

Si vous tapez une commande erronée, la demande dit « attendre = » ce qui signifie que vous avez envoyé une ligne qui n'est pas formaté droit ...

Alors ce que je fais ci-dessous simule cette frappe manuellement, une commande, une nouvelle ligne, une commande, une nouvelle ligne etc, jusqu'à la fin, alors je viens d'ajouter Ctrl + D après la dernière commande. Cela ne fonctionne pas et le programme dit "expectating ="

Des problèmes évidents?

Merci pour votre temps. Albert

 // CREATE DISCONNECT FILE 
     StringBuilder sb = new StringBuilder(); 
     sb.AppendLine("User-Name=" + this.userName); 
     sb.AppendLine("Acct-Session-Id=" + this.sessionID); 
     sb.AppendLine("NAS-IP-Address=" + Setting.Item["EDGE.NASIP"]); 
     sb.AppendLine("Framed-IP-Address=" + this.currentIP); 
     sb.Append("/x4"); 
     System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(); 
     procStartInfo.WorkingDirectory = radclientPath.Substring(0, radclientPath.LastIndexOf("\\")); 
     procStartInfo.FileName = radclientPath; 
     procStartInfo.Arguments = @"-r 1 -d ..\etc\raddb -x 192.168.1.240:3799 disconnect password"; 
     procStartInfo.RedirectStandardInput = true; 
     procStartInfo.RedirectStandardError = true; 
     procStartInfo.RedirectStandardOutput = true; 
     procStartInfo.UseShellExecute = false; 

     System.Diagnostics.Process proc = System.Diagnostics.Process.Start(procStartInfo); 

     proc.StandardInput.Write(sb.ToString()); 
     proc.WaitForExit(5000); 

     if (proc.HasExited) 
     { 
      System.IO.File.WriteAllText(@"D:\temp.txt", proc.StandardOutput.ReadToEnd()); 
      System.IO.File.WriteAllText(@"D:\temp2.txt", proc.StandardError.ReadToEnd()); 
      string message = proc.StandardOutput.ReadToEnd() + "\n---\nErrors:\n---\n" + proc.StandardError.ReadToEnd(); 
      return message; 
     } 
     System.IO.File.WriteAllText(@"D:\temp.txt", proc.StandardOutput.ReadToEnd()); 
     System.IO.File.WriteAllText(@"D:\temp2.txt", proc.StandardError.ReadToEnd()); 
     return ""; 
    } 

J'ai aussi essayé entre guillemets simples et avec un zéro conduisant le nombre (\ x04) et rien non plus, donc j'essayé d'écrire ce qui doit être envoyé au programme dans un fichier texte. J'ai ensuite exécuté le programme en utilisant les mêmes paramètres de ligne de commande que ci-dessous. Copié ce qui était dans le fichier texte entier et collé dans l'invite de commande, et cela fonctionne très bien. Je ne sais pas pourquoi il n'a pas été envoyé correctement au stdin ..

 StringBuilder sb = new StringBuilder(); 
     sb.AppendLine("User-Name=" + this.userName); 
     sb.Append('\x04'); 
     System.IO.File.WriteAllText(@"D:\input.txt", sb.ToString()); 
     System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(); 
     procStartInfo.WorkingDirectory = radclientPath.Substring(0, radclientPath.LastIndexOf("\\")); 
     procStartInfo.FileName = radclientPath; 
     procStartInfo.Arguments = @"-r 1 -d ..\etc\raddb -x 192.168.1.240:3799 disconnect password"; 
     procStartInfo.RedirectStandardInput = true; 
     procStartInfo.RedirectStandardError = true; 
     procStartInfo.RedirectStandardOutput = true; 
     procStartInfo.UseShellExecute = false; 
     System.Diagnostics.Process proc = System.Diagnostics.Process.Start(procStartInfo); 
     proc.StandardInput.Write(sb.ToString()); 
     proc.WaitForExit(5000); 

Répondre

0
sb.Append("/x4"); 

ce n'est pas un contrôle D

Peut-être que vous vouliez dire

sb.Append("\x04"); 
+0

J'avais \ x4 dans mon code original, désolé, c'était une faute de frappe que j'ai faite en créant le post. Il essaie d'ajouter le 0 avant le 4 et rapporte! merci, Albert – Albert

+0

hey tout, je viens de mettre à jour le ci-dessus, s'il vous plaît jeter un oeil ... – Albert

+0

se demandant si c'est un problème avec l'encodage quelque part ... – Albert

2

Vous devez écrire ajouter "\x04" , avec un backslah (\)

Pour plus d'informations, voir le documentation (section Séquences d'échappement de chaîne)