• Support
  • Articles
  • Resources
  • Products

Macro to telnet to a remotehost and send cmds in loop

Moderator: boris

1 postPage 1 of 1

yogavukkum
Newbie

Posts:
3
Joined: Thu Jul 03, 2008 11:02 pm
by yogavukkum » Thu Sep 18, 2008 5:00 pm
Hi,

This macro connect to a remotehost and sends the commands one by one. Also, it logs the script execution start time and the end time. In the code, instead of reading and sending cmds in a loop, we can use sendfile function ( place all your cmd in a file ).

Code: Select all

  1. getpassword 'username.dat' 'myusername' username
  2. getpassword 'password.dat' 'mypassword' password
  3.  
  4. UsernamePrompt = 'ogin:'
  5. PasswordPrompt = 'assword:'
  6.  
  7. inputbox 'Directory path & name followed by \' 'Logging Directory'
  8. ; Default directory path for the log files C:\Logs
  9. ;loggerpath = 'C:\Logs\'
  10. loggerpath = inputstr
  11. strcompare loggerpath ''
  12.  
  13. if result = 0 then
  14.    loggerpath = 'C:\Logs\'
  15. endif
  16.  
  17. messagebox loggerpath 'logfile'
  18.  
  19. dirmaker = 'cmd /C md '
  20. strconcat dirmaker loggerpath
  21. exec dirmaker
  22.  
  23. pause 1
  24. ; Prompt for the loadfile name
  25. :loop
  26.  
  27. filenamebox 'LoadFileName'
  28. loadfilename = inputstr
  29.  
  30. strcompare loadfilename ''
  31.  
  32. if result = 0 then
  33.    Messagebox 'LoadFileName is empty' 'LoadFileName'
  34.    goto loop
  35. endif
  36.  
  37. connect '$connection$:5400 /nossh'
  38. ;messagebox result 'test'
  39. if result != 2 then
  40.      disconnect
  41. endif
  42.  
  43. wait UsernamePrompt
  44. sendln username
  45. wait PasswordPrompt
  46. sendln password
  47.  
  48. ;messagebox result 'test'
  49.  
  50. ; checking for the username/password.  if credentials are wrong, then delete the files.  
  51. ; the user will re-enter the username/password.  If username/password are correct, then
  52. ; user no need to enter his/her username/password for the second time
  53.  
  54. if result = 0 then
  55.      filedelete 'password.dat'
  56.      filedelete 'username.dat'
  57.      disconnect
  58. endif
  59.  
  60. ; log file name creation with date
  61. strconcat logfile loggerpath
  62. strconcat logfile '$parent$'
  63. strconcat logfile '_'
  64. strconcat logfile '$connection$'
  65. strconcat logfile '_'
  66. getdate datestr '%m%d%Y.txt'
  67. strconcat logfile datestr
  68. logopen logfile 0 0
  69. gettime timestr
  70. line='Script Execution Started At '
  71. strconcat line timestr
  72. logwrite line
  73. logwrite #13#10
  74.  
  75. ; Open a file.
  76. fileopen fhandle loadfilename 0
  77.  
  78. :loop1
  79. ; Read a line from the file.
  80. filereadln fhandle line
  81. if result goto fclose
  82. ; Display the line.
  83. sendln line
  84. wait '>'
  85. ; Repeat until the end of the file.
  86. goto loop1
  87.  
  88. :fclose
  89. ; Close the file.
  90. fileclose fhandle
  91.  
  92. pause 2
  93. sendln 'exit'
  94. wait '>'
  95. gettime timestr
  96. line='Script Execution Ended At '
  97. strconcat line timestr
  98. logwrite line
  99.  
  100. disconnect
  101. closett
  102. unlink
  103. end

1 postPage 1 of 1

Users browsing this forum: No registered users
cron