Advertisement
Guest User

SSH Changer

a guest
Dec 8th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace ChangerSSH
  14. {
  15. public partial class Form1 : Form
  16. {
  17.  
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23. private int porti = 1080;
  24. private int top = 100;
  25. Label[] l1 = new Label[50];
  26. int[,] x = new int[50, 4];
  27. int sshi = 1;
  28.  
  29. internal bool CheckSocksAlive(string host, int port, int TryTimes)
  30. {
  31. TryTimes++;
  32. TcpClient tcpClient = new TcpClient();
  33. try
  34. {
  35. tcpClient.Connect(host, port);
  36. return true;
  37. }
  38. catch (Exception)
  39. {
  40. if (TryTimes > 5) return tcpClient.Connected;
  41. return CheckSocksAlive(host, port, TryTimes);
  42. //if (tcpClient.Connected) return true;
  43. //else return false;
  44. }
  45. }
  46.  
  47. //List<Button> buttons = new List<Button>();
  48. private void NewBtn_Click(object sender, EventArgs e)
  49. {
  50. porti = Int32.Parse(StartPortTbx.Text);
  51. for (int i = 0; i < Int32.Parse(ThreadsTbx.Text); i++)
  52. {
  53. Button newButton = new Button();
  54. //buttons.Add(newButton);
  55. newButton.Name = porti.ToString();
  56. newButton.Height = 23;
  57. newButton.Width = 90;
  58. newButton.Left = 300;
  59. newButton.Top = top;
  60. newButton.FlatStyle = 0;
  61. newButton.Text = "Change_" + newButton.Name;
  62. this.Controls.Add(newButton);
  63. newButton.Click += new EventHandler(ButtonClickOneEvent);
  64. l1[i] = new Label();
  65. l1[i].Name = porti.ToString();
  66. l1[i].Width = 300;
  67. l1[i].Left = 10;
  68. l1[i].Top = top + 5;
  69. l1[i].Text = newButton.Name;
  70. this.Controls.Add(l1[i]);
  71. top = top + 30;
  72. porti++;
  73. x[i,0] = i;
  74. x[i,1] = porti;
  75. }
  76. }
  77.  
  78. private async void ButtonClickOneEvent(object sender, EventArgs e)
  79. {
  80. int iii;
  81.  
  82. Button button = sender as Button;
  83. Label label = sender as Label;
  84. //MessageBox.Show(button.Name);
  85. for(iii=0; iii < Int32.Parse(ThreadsTbx.Text); iii++)
  86. {
  87.  
  88. if (l1[iii].Name == button.Name)
  89. {
  90. Console.WriteLine("ProcID =" + x[iii, 3]);
  91. if(x[iii, 3] != 0)
  92. {
  93. string PlinkCommand4 = "Taskkill /F /PID " + x[iii, 3] + " /T";
  94. Process proc4 = new Process();
  95. proc4.StartInfo.FileName = "CMD.exe";
  96. proc4.StartInfo.Arguments = "/c " + PlinkCommand4;
  97. proc4.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  98. proc4.Start();
  99. }
  100. int port = Int32.Parse(button.Name);
  101. string[] SshInfo = SshTbx.Lines[sshi - 1].Split('|');
  102. l1[iii].Text = l1[iii].Name + ": Connecting to " + SshInfo[0];
  103. l1[iii].Refresh();
  104. bool conn = false;
  105. string PlinkCommand = "echo y | plink.exe -ssh -D " + port + " " + SshInfo[1] + "@" + SshInfo[0] + " -N -pw " + SshInfo[2];
  106. Process proc = new Process();
  107. proc.StartInfo.FileName = "CMD.exe";
  108. proc.StartInfo.Arguments = "/c " + PlinkCommand;
  109. proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  110. proc.Start();
  111. x[iii, 3] = proc.Id;
  112. await Task.Delay(200);
  113. conn = CheckSocksAlive("127.0.0.1", port, 2);
  114. if (conn)
  115. {
  116. l1[iii].Text = l1[iii].Name + ": Connected " + SshInfo[0];
  117. l1[iii].Refresh();
  118. }
  119. else
  120. {
  121. l1[iii].Text = l1[iii].Name + ": Connect Failed. Click ================>>>>>> ";
  122. l1[iii].Refresh();
  123. }
  124. break;
  125.  
  126. };
  127. }
  128. sshi++;
  129.  
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement