(CORE) CORE (2021)

Linux shell parser (Renci.SshNet.SshClient, MySqlConnector.NET, RSA encription, Console password, XDocument/XPath, Tuple, Regex, Buffer.BlockCopy, Stream, Base64String, UTF8.GetBytes, ToString("X2"))

This is example of my typical Linux console application in VB. Of course this is only first step in this program, but all main framework is present. Full code you can see in Github https://github.com/Alex-1347/ParseServerResponse.



Program processing KVM config, more about KVM KVM Handbook.


As any other my Linux program this program working with MySQL, for example:


Program use XML parser. More detail see in page Топіки що стосуються специфічних технологій обробки JSON та XML., Parse HTML by HtmlAgilityPack (Xpath selector) and CsQuery (jQuery selector).


Program working with encoding and bytes, see more in page Робота з байтами у VB.NET - ChrW, BitConverter.ToInt32, Convert.ToInt32, Byte.Parse, ToString("X2") / AllowHexSpecifier, GetBytes/GetString, New Byte(N) {}, UInt32 = &H33.


Program use RSA-cryptography, please see more cryptographic example in my site:


What difference beetween this code and Encrypt sensitive data in DB by Rijndael symmetric algorithm.? "AES" and "Rijndael" use the same algorithm, but Rijndael supports block sizes of 128, 160, 192, 224, and 256 bits, however AES below use only 128 bit key.


   1:  Imports System.IO
   2:  Imports System.Security.Cryptography
   3:  Imports System.Text
   4:  Imports Microsoft.VisualBasic
   5:   
   6:  Partial Module Program
   7:   
   8:      Public Function EncryptString(ByVal Text As String, ByVal KeyString As String) As String
   9:          Dim Key As Byte() = Encoding.UTF8.GetBytes(KeyString)
  10:          ReDim Preserve Key(31)
  11:          Using AesAlg = Aes.Create()
  12:              Using Encryptor = AesAlg.CreateEncryptor(Key, AesAlg.IV)
  13:                  Using MsEncrypt = New MemoryStream()
  14:                      Using CsEncrypt = New CryptoStream(MsEncrypt, Encryptor, CryptoStreamMode.Write)
  15:                          Using SwEncrypt = New StreamWriter(CsEncrypt)
  16:                              SwEncrypt.Write(Text)
  17:                          End Using
  18:                      End Using
  19:                      Dim IV = AesAlg.IV
  20:                      Dim DecryptedContent = MsEncrypt.ToArray()
  21:                      Dim Result = New Byte(IV.Length + DecryptedContent.Length - 1) {}
  22:                      Buffer.BlockCopy(IV, 0, Result, 0, IV.Length)
  23:                      Buffer.BlockCopy(DecryptedContent, 0, Result, IV.Length, DecryptedContent.Length)
  24:                      Return Convert.ToBase64String(Result)
  25:                  End Using
  26:              End Using
  27:          End Using
  28:      End Function
  29:   
  30:      Public Function DecryptString(ByVal CipherText As String, ByVal KeyString As String) As String
  31:          Try
  32:              Dim FullCipher = Convert.FromBase64String(CipherText)
  33:              Dim IV = New Byte(15) {}
  34:              Dim Cipher = New Byte(15) {}
  35:              Buffer.BlockCopy(FullCipher, 0, IV, 0, IV.Length)
  36:              Buffer.BlockCopy(FullCipher, IV.Length, Cipher, 0, IV.Length)
  37:              Dim Key = Encoding.UTF8.GetBytes(KeyString)
  38:              ReDim Preserve Key(31)
  39:              Using AesAlg = Aes.Create()
  40:                  Using Decryptor = AesAlg.CreateDecryptor(Key, IV)
  41:                      Dim Result As String
  42:                      Using MsDecrypt = New MemoryStream(Cipher)
  43:                          Using CsDecrypt = New CryptoStream(MsDecrypt, Decryptor, CryptoStreamMode.Read)
  44:                              Using SrDecrypt = New StreamReader(CsDecrypt)
  45:                                  Result = SrDecrypt.ReadToEnd()
  46:                              End Using
  47:                          End Using
  48:                      End Using
  49:                      Return Result
  50:                  End Using
  51:              End Using
  52:          Catch ex As System.Security.Cryptography.CryptographicException
  53:              Return "Wrong password"
  54:          End Try
  55:   
  56:      End Function
  57:   
  58:   
  59:  End Module


This console program don't show sensitive data to console, another way (in desktop application you can see in this page Protect program by password..


   1:  Imports System
   2:  Imports System.Security
   3:  Imports Renci.SshNet.SshClient
   4:  Imports MySqlConnector
   5:  Imports System.Text.RegularExpressions
   6:  Imports System.Xml.XPath
   7:   
   8:  'https://github.com/sshnet/SSH.NET
   9:   
  10:  Partial Module Program
  11:      Sub Main(args As String())
  12:          Dim Str1 As String = ""
  13:          Dim Pass1 As String = ""
  14:          Dim CN As MySqlConnection
  15:          Dim SshClient As Renci.SshNet.SshClient
  16:  Start:
  17:          Console.WriteLine()
  18:          Console.WriteLine("Select function:")
  19:          Console.WriteLine("1. Aes Encrypt string")
  20:          Console.WriteLine("2. Aes Decrypt string")
  21:          Console.WriteLine("3. Setup MySQL connection")
  22:          Console.WriteLine("4. Remote server SSH shell")
  23:          Console.WriteLine("5. Processing special function to parse KVM Virtual Machine information to remote database")
  24:          Console.Write(">")
  25:          Dim Func1 = Console.ReadLine()
  26:          If Not IsNumeric(Func1) Then GoTo Start
  27:          Select Case Func1
  28:              Case 1
  29:                  Console.Write("Get string need to AES encript" & vbCrLf & ">")
  30:                  Str1 = Console.ReadLine()
  31:                  Console.Write("Get password string" & vbCrLf & ">")
  32:                  Pass1 = ReadPassword()
  33:                  Console.WriteLine(EncryptString(Str1, Pass1))
  34:              Case 2
  35:                  Console.Write("Get string need to AES decript" & vbCrLf & ">")
  36:                  Str1 = Console.ReadLine()
  37:                  Console.Write("Get password string" & vbCrLf & ">")
  38:                  Pass1 = ReadPassword()
  39:                  Console.WriteLine(DecryptString(Str1, Pass1))
  40:              Case 3
  41:                  Console.Write("Get password to encrypt MySQL connection" & vbCrLf & ">")
  42:                  Pass1 = ReadPassword()
  43:                  Dim EncryptedMySQLPass As String = ""
  44:                  Dim ConnectionStringPart As String() = My.Resources.ConnectionString.Split(";")
  45:                  For i As Integer = 0 To ConnectionStringPart.Length - 1
  46:                      If ConnectionStringPart(i).StartsWith("password") Then
  47:                          EncryptedMySQLPass = ConnectionStringPart(i).Replace("password=", "") 'password nned to last parm to avoid finishing char - ";"
  48:                          Dim RealPass As String = DecryptString(EncryptedMySQLPass, Pass1)
  49:                          ConnectionStringPart(i) = $"password={RealPass}"
  50:                      End If
  51:                  Next
  52:                  Dim DecryptMySQLConnectionString = String.Join(";", ConnectionStringPart)
  53:                  CN = New MySqlConnection(DecryptMySQLConnectionString)
  54:                  Try
  55:                      CN.Open()
  56:                  Catch ex As Exception
  57:                      Console.WriteLine(ex.Message & vbCrLf & DecryptMySQLConnectionString)
  58:                      GoTo Start
  59:                  End Try
  60:              Case 4
  61:                  Dim Res1 As Tuple(Of Renci.SshNet.SshClient, String)
  62:                  Try
  63:                      Res1 = SSHConnect()
  64:                      SshClient = Res1.Item1
  65:                      Pass1 = Res1.Item2
  66:                  Catch ex As Renci.SshNet.Common.SshAuthenticationException
  67:                      Console.WriteLine(ex.Message & vbCrLf & My.Resources.SSHURL & vbCrLf & My.Resources.SSHLogin & vbCrLf & Pass1)
  68:                      GoTo Start
  69:                  End Try
  70:                  If SshClient.IsConnected Then
  71:                      While True
  72:                          Console.Write("Get shell command" & vbCrLf & ">")
  73:                          Str1 = Console.ReadLine()
  74:                          Dim Out1 = SshClient.RunCommand(Str1)
  75:                          Console.WriteLine(Out1.Result)
  76:                      End While
  77:                  End If
  78:              Case 5
  79:                  Dim Res1 As Tuple(Of Renci.SshNet.SshClient, String)
  80:                  Try
  81:                      Res1 = SSHConnect()
  82:                      SshClient = Res1.Item1
  83:                      Pass1 = Res1.Item2
  84:                  Catch ex As Renci.SshNet.Common.SshAuthenticationException
  85:                      Console.WriteLine(ex.Message & vbCrLf & My.Resources.SSHURL & vbCrLf & My.Resources.SSHLogin & vbCrLf & Pass1)
  86:                      GoTo Start
  87:                  End Try
  88:                  Dim SshVirshList = SshClient.RunCommand("virsh list --all")
  89:                  Dim Out1 As List(Of String) = LineReader(SshVirshList.Result)
  90:                  Dim VmList As New List(Of Tuple(Of String, String, String))
  91:                  Out1.ForEach(Sub(x)
  92:                                   If Not String.IsNullOrWhiteSpace(x) Then
  93:                                       If Not x.StartsWith("---") Then
  94:                                           Dim Arr1 = ReplaceWhitespace(x).Trim.Split(" ")
  95:                                           If Arr1.Length = 3 Then
  96:                                               If Arr1(0) <> "Id" Then
  97:                                                   Dim OneVm = New Tuple(Of String, String, String)(Arr1(0).Trim, Arr1(1).Trim, Arr1(2).Trim)
  98:                                                   VmList.Add(OneVm)
  99:                                               End If
 100:                                           End If
 101:                                       End If
 102:                                   End If
 103:                               End Sub)
 104:                  Console.WriteLine(String.Join(vbCrLf, VmList))
 105:                  For Each One As Tuple(Of String, String, String) In VmList
 106:                      Try
 107:                          Dim XMLConfig = SshClient.RunCommand("virsh dumpxml " & One.Item2)
 108:                          Dim XML As XDocument = XDocument.Parse(XMLConfig.Result)
 109:                          Dim Uuid As String = XML.XPathSelectElement("domain/uuid").Value
 110:                          Dim Vcpu As String = XML.XPathSelectElement("domain/vcpu").Value
 111:                          Dim CpuSet As String
 112:                          Try
 113:                              CpuSet = XML.XPathSelectElement("domain/vcpu")?.Attribute("cpuset").Value
 114:                          Catch ex As Exception
 115:                              CpuSet = "NO"
 116:                          End Try
 117:                          Dim Memory As String = XML.XPathSelectElement("domain/memory").Value
 118:                          Dim SpicePort As String = XML.XPathSelectElement("domain/devices/graphics").Attribute("port").Value
 119:                          Dim MacAdr As String = XML.XPathSelectElement("domain/devices/interface/mac").Attribute("address").Value
 120:                          Dim BidgeTargetNetworkName As String = XML.XPathSelectElement("domain/devices/interface/target").Attribute("dev").Value
 121:                          Dim KvmVolumeName As String = XML.XPathSelectElement("domain/devices/disk/source").Attribute("dev").Value
 122:                          Dim CMD1 As MySqlCommand = New MySqlCommand($"SELECT * FROM `cryptochestmax`.`VM` where Id={One.Item1};", CN)
 123:                          Dim RDR1 As MySqlDataReader = CMD1.ExecuteReader
 124:                          Dim CurVmListIndex As Integer = 0
 125:                          If RDR1.Read Then
 126:                              CurVmListIndex = CInt(RDR1("i"))
 127:                          End If
 128:                          RDR1.Close()
 129:                          If CurVmListIndex = 0 Then
 130:                              Console.WriteLine($"VM {One} is absent in database VM table")
 131:                              Continue For
 132:                          Else
 133:                              Dim VMNetworkIndex As Integer = 0
 134:                              Dim CMD2 As MySqlCommand = New MySqlCommand($"SELECT * FROM cryptochestmax.VMNetwork where Name='{BidgeTargetNetworkName}';", CN)
 135:                              Dim RDR2 As MySqlDataReader = CMD2.ExecuteReader
 136:                              If RDR2.Read Then
 137:                                  VMNetworkIndex = CInt(RDR2("i"))
 138:                              End If
 139:                              RDR2.Close()
 140:                              If VMNetworkIndex = 0 Then
 141:                                  Console.WriteLine($"VmNetworkName {BidgeTargetNetworkName} is absent in database VMNetwork table")
 142:                              End If
 143:                              Dim KvmVolumeIndex As Integer = 0
 144:                              Dim CMD3 As MySqlCommand = New MySqlCommand($"SELECT * FROM cryptochestmax.KvmVolume where Path='{KvmVolumeName}';", CN)
 145:                              Dim RDR3 As MySqlDataReader = CMD3.ExecuteReader
 146:                              If RDR3.Read Then
 147:                                  KvmVolumeIndex = CInt(RDR3("i"))
 148:                              End If
 149:                              RDR3.Close()
 150:                              If KvmVolumeIndex = 0 Then
 151:                                  Console.WriteLine($"KvmVolumeName {KvmVolumeName} is absent in database KvmVolume table")
 152:                              End If
 153:                              Dim CMD4 As MySqlCommand = New MySqlCommand($"Update `cryptochestmax`.`VM` SET `ToKvmVolume` = '{KvmVolumeIndex}',`ToVmNetwork` = '{VMNetworkIndex}',`UUID` = '{Uuid}',`CpuSet` = '{CpuSet}',`Vcpu` = '{Vcpu}',`Memory` = '{Memory}',`SpicePort` = '{SpicePort}',`MacAdr` = '{MacAdr}',`LastUpdate` = Now() where i={CurVmListIndex};", CN)
 154:                              CMD4.ExecuteNonQuery()
 155:                              Console.WriteLine($"VM info {One} updated.")
 156:                          End If
 157:                      Catch ex As Exception
 158:                          Console.WriteLine(ex.Message)
 159:                          Continue For
 160:                      End Try
 161:                  Next
 162:          End Select
 163:          GoTo Start
 164:      End Sub
 165:   
 166:      Function ReplaceWhitespace(Str1 As String) As String
 167:          Dim WhiteSpaceReg As Regex = New Regex("\s+")
 168:          Return WhiteSpaceReg.Replace(Str1, " ")
 169:      End Function
 170:   
 171:      Function LineReader(Str1 As String) As List(Of String)
 172:          Dim Out1 As New System.IO.StringReader(Str1)
 173:          Dim Line As New List(Of String)
 174:          While Out1.Peek > -1
 175:              Line.Add(Out1.ReadLine)
 176:          End While
 177:          Return Line
 178:      End Function
 179:   
 180:      Function SSHConnect() As Tuple(Of Renci.SshNet.SshClient, String)
 181:          Console.Write("Get password to encrypt SSH connection" & vbCrLf & ">")
 182:          Dim Pass1 As String = ReadPassword()
 183:          Dim RealPass As String = DecryptString(My.Resources.SSHPassword, Pass1)
 184:          Dim SshClient As New Renci.SshNet.SshClient(My.Resources.SSHURL, My.Resources.SSHLogin, RealPass)
 185:          AddHandler SshClient.HostKeyReceived, AddressOf SshClient_HostKeyReceived
 186:          SshClient.Connect()
 187:          Return New Tuple(Of Renci.SshNet.SshClient, String)(SshClient, RealPass)
 188:      End Function
 189:   
 190:      Function ReadPassword() As String
 191:          Dim Pass1 As New Text.StringBuilder
 192:          While (True)
 193:              Dim OneKey As ConsoleKeyInfo = Console.ReadKey(True)
 194:              Select Case OneKey.Key
 195:                  Case = ConsoleKey.Enter
 196:                      Return Pass1.ToString
 197:                  Case ConsoleKey.Backspace
 198:                      Pass1.Remove(Pass1.Length - 1, 1)
 199:                      Console.Write(vbBack)
 200:                  Case Else
 201:                      If Not Char.IsControl(OneKey.KeyChar) Then
 202:                          Pass1.Append(OneKey.KeyChar)
 203:                          Console.Write("*")
 204:                      End If
 205:              End Select
 206:          End While
 207:      End Function
 208:   
 209:      Sub SshClient_HostKeyReceived(sender As Object, e As Renci.SshNet.Common.HostKeyEventArgs)
 210:          Dim Sb As Text.StringBuilder = New System.Text.StringBuilder()
 211:          For i As Integer = 0 To e.HostKey.Length - 1
 212:              Sb.Append(e.HostKey(i).ToString("X2"))
 213:          Next
 214:          Console.WriteLine(vbCrLf & "Hostkey: " & Sb.ToString())
 215:      End Sub
 216:   
 217:  End Module

Similar program Linux console app (.NET Core, EF DB first, CamelCase file and dir rename, calc MD5, RegExpression, change and check link).

Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: http://www.vb-net.com/LinuxShellParser/Index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>