Advertisement
iljimae

change bằng ssh with Bitvise

Aug 7th, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Runtime.InteropServices;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using ManagedWinapi.Windows;
  12.  
  13. namespace VinaCaptcha
  14. {
  15.     public static class BitviseHandle
  16.     {
  17.         #region Windows API - user32.dll configs
  18.         private const int WM_CLOSE = 16;
  19.         private const int BN_CLICKED = 245;
  20.         private const int WM_LBUTTONDOWN = 0x0201;
  21.         private const int WM_LBUTTONUP = 0x0202;
  22.  
  23.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  24.         public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
  25.         //Click - Worked Perfect
  26.         //SendMessage((int)hwnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  27.         //Thread.Sleep(100);
  28.         //SendMessage((int)hwnd, WM_LBUTTONUP, 0, IntPtr.Zero);
  29.         //---
  30.         //Close Window
  31.         //SendMessage((int)hwnd, WM_CLOSE ,0 , IntPtr.Zero);
  32.         #endregion
  33.  
  34.         private static Hashtable BitviseList = new Hashtable();
  35.         public static int TimeoutSeconds = 30;
  36.        
  37.         private static int PortIndex = 1079;
  38.         public static int GetPortAvailable()
  39.         {
  40.             PortIndex++;
  41.             if (PortIndex >= 1181) PortIndex = 1079;
  42.             Process BitviseApp = new Process();
  43.             BitviseList.Add(PortIndex, BitviseApp);
  44.             return PortIndex;
  45.         }
  46.  
  47.         public static bool Connect(string Host, string User, string Pass, int ForwardPort)
  48.         {
  49.             bool Connected = false;
  50.            
  51.             //Start Bitvise - Auto Login
  52.             ProcessStartInfo sinfo = new ProcessStartInfo();
  53.             sinfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient\\BvSsh.exe";
  54.             sinfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient";
  55.             sinfo.Arguments = "-profile=\"" + AppDomain.CurrentDomain.BaseDirectory + "BitviseSSHClient\\" + ForwardPort + ".bscp\" -host=" + Host + " -user=" + User + " -password=" + Pass + " -loginOnStartup -hide=trayIcon";
  56.             Process BitviseApp = Process.Start(sinfo);
  57.  
  58.             BitviseList[ForwardPort] = BitviseApp;
  59.            
  60.             Thread.Sleep(2000);
  61.  
  62.             //Bitvise Login Checking...
  63.             for (int i = 0; i < TimeoutSeconds; i++)
  64.             {
  65.                 //Detect Host Key Verification
  66.                 SystemWindow[] wins = SystemWindow.FilterToplevelWindows((SystemWindow w) => { return w.Title == "Host Key Verification"; });
  67.                 if (wins.Length > 0)
  68.                 {
  69.                     SystemWindow[] wins2 = wins[0].FilterDescendantWindows(false, (SystemWindow w) => { return w.Title == "&Accept for This Session"; }); //Accept and &Save
  70.                     if (wins2.Length > 0)
  71.                     {
  72.                         //Click 4 times to effected !
  73.                         SendMessage((int)wins2[0].HWnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  74.                         Thread.Sleep(10);
  75.                         SendMessage((int)wins2[0].HWnd, WM_LBUTTONUP, 0, IntPtr.Zero);
  76.  
  77.                         SendMessage((int)wins2[0].HWnd, WM_LBUTTONDOWN, 0, IntPtr.Zero);
  78.                         Thread.Sleep(10);
  79.                         SendMessage((int)wins2[0].HWnd, WM_LBUTTONUP, 0, IntPtr.Zero);
  80.                     }
  81.                 }
  82.  
  83.                 //Detect Connected
  84.                 SystemWindow[] wins3 = SystemWindow.FilterToplevelWindows((SystemWindow w) => { return w.Title == "Bitvise SSH Client - " + ForwardPort + ".bscp - " + Host + ":22"; });
  85.                 if (wins3.Length > 0)
  86.                 {
  87.                     Connected = true;
  88.                     break;
  89.                 }
  90.  
  91.                 Thread.Sleep(1000);
  92.             }
  93.  
  94.             if (Connected == false)
  95.             {
  96.                 try
  97.                 {
  98.                     BitviseApp.Kill();
  99.                     BitviseApp.Dispose();
  100.                 }
  101.                 catch { }
  102.             }
  103.  
  104.            
  105.             return Connected;
  106.         }
  107.  
  108.         public static void Disconnect(int ForwardPort)
  109.         {
  110.             if (BitviseList[ForwardPort] == null) return;
  111.  
  112.             try
  113.             {
  114.                 Process BitviseApp = BitviseList[ForwardPort] as Process;
  115.                 BitviseApp.Kill();
  116.                 BitviseApp.Dispose();
  117.             }
  118.             catch { }
  119.         }
  120.  
  121.         private static bool GetPort(string Host, int Port)
  122.         {
  123.             return true;
  124.  
  125.             //using (TcpClient tcpClient = new TcpClient())
  126.             //{
  127.             //    IAsyncResult result = tcpClient.BeginConnect(Host, Port, null, null);
  128.             //    WaitHandle timeoutHandler = result.AsyncWaitHandle;
  129.             //    try
  130.             //    {
  131.             //        if (!result.AsyncWaitHandle.WaitOne(200, false))
  132.             //        {
  133.             //            tcpClient.Close();
  134.             //            return false;
  135.             //        }
  136.  
  137.             //        tcpClient.EndConnect(result);
  138.             //    }
  139.             //    catch
  140.             //    {
  141.             //        return false;
  142.             //    }
  143.             //    finally
  144.             //    {
  145.             //        timeoutHandler.Close();
  146.             //    }
  147.             //    return true;
  148.             //}
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement