example_01.bmx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. SuperStrict
  2. Framework Net.libssh2
  3. Import BRL.StandardIO
  4. Const ipAddress:String = "127.0.0.1"
  5. Const username:String = "username"
  6. Const password:String = "password"
  7. ' Ultra basic "connect to port 22 on localhost"
  8. ' Your code is responsible for creating the socket establishing the connection
  9. Local socket:TSocket = TSocket.CreateTCP()
  10. Local res:Int
  11. ?bmxng
  12. res = socket.Connect(AddrInfo(ipAddress, 22)[0])
  13. ?Not bmxng
  14. res = socket.Connect(HostIp(ipAddress), 22)
  15. ?
  16. If Not res Then
  17. Print "Failed to connect"
  18. End
  19. End If
  20. ' Create a session instance and start it up
  21. ' This will trade welcome banners, exchange keys, and setup crypto, compression, and MAC layers
  22. Local session:TSSHSession = New TSSHSession
  23. If session.Startup(socket) Then
  24. Print "Failure establishing SSH session"
  25. End
  26. End If
  27. If session.UserAuthKeyboardInteractive(username, kbdCallback) Then
  28. Print "Authentication by keyboard-interactive failed!"
  29. Else
  30. Print "Authentication by keyboard-interactive succeeded."
  31. End If
  32. Local channel:TSSHChannel = session.OpenChannel()
  33. If Not channel Then
  34. Print "Unable to open a session"
  35. End
  36. End If
  37. ' Some environment variables may be set,
  38. ' It's up to the server which ones it'll allow though
  39. channel.SetEnv("FOO", "bar")
  40. ' Request a terminal with 'vanilla' terminal emulation
  41. ' See /etc/termcap for more options
  42. If channel.RequestPty("vanilla") Then
  43. Print "Failed requesting pty"
  44. End
  45. End If
  46. ' Open a SHELL on that pty
  47. If channel.Shell() Then
  48. Print "Unable to request shell on allocated pty"
  49. End
  50. End If
  51. '
  52. ' The following is by no means the correct way to interact with a shell..
  53. '
  54. Local buffer:Byte[2048]
  55. Local count:Int = channel.Read(buffer, 2048)
  56. While count > 0
  57. WriteStdout String.FromBytes(buffer, count)
  58. Local read:Int = socket._socket
  59. If select_( 1,Varptr read,0,Null,0,Null,1000 ) <= 0 Then
  60. Exit
  61. End If
  62. count = channel.Read(buffer, 2048)
  63. Wend
  64. ' get a directory listing...
  65. Local b:Byte Ptr = "ls~n".ToCString()
  66. count = channel.Write(b, 3)
  67. MemFree(b)
  68. ' ... read until output is finished.
  69. count = channel.Read(buffer, 2048)
  70. While count > 0
  71. WriteStdout String.FromBytes(buffer, count)
  72. Local read:Int = socket._socket
  73. If select_( 1,Varptr read,0,Null,0,Null,1000 ) <= 0 Then
  74. Exit
  75. End If
  76. count = channel.Read(buffer, 2048)
  77. Wend
  78. ' callback function to handle Keyboard Interactive Auth.
  79. Function kbdCallback(name:String, instruction:String, prompts:TSSHKeyboardPrompt[], responses:TSSHKeyboardResponse[])
  80. If prompts.length = 1 Then
  81. responses[0].SetText(password)
  82. End If
  83. End Function