Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Putty/VBS Help  (Read 15572 times)

0 Members and 1 Guest are viewing this topic.

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Putty/VBS Help
« on: July 09, 2012, 03:02:46 AM »
I run a Team Fortress 2 server that uses Linux. To access it, I use Putty to run commands from my windows box, and Screen to actually run the server program. I'm trying to make a VBS file automatically run a specific command from inside the server program, and I'm having some weird errors. I'm using SendKeys and it's working for most of the script. It manages to write everything successfully, until it reaches Screen, where it only writes half of what I'm trying to run.

Quote
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """C:\Program Files (x86)\PuTTY\putty.exe"" -load tf2", 9
WScript.Sleep 3000
WshShell.SendKeys "root"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 2000
WshShell.SendKeys "SUPERSECRETPASSWORD"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys "screen -x"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 20000
WshShell.SendKeys "sm_re"
Wscript.Sleep 1000
WshShell.SendKeys "load"
Wscript.Sleep 1000
WshShell.SendKeys "admins"

WScript.Sleep 2000
WshShell.SendKeys "{ENTER}"
WScript.Sleep 2000
)>update.vbs
cscript //nologo update.vbs
I've tried changing the time it waits after it's opened the server software through screen and before it starts typing, but it's still only managing to write "oad"; sometimes it writes "sm_reoad". The whole command it's supposed to write is "sm_reloadadmins", but it's not getting anywhere close to that.

Edit: I've even tried to do each character individually, and it's not working.
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Putty/VBS Help
« Reply #1 on: July 09, 2012, 05:03:52 AM »
Try giving focus to the application before isssuing the first sendkeys operation:

Code: [Select]
WshShell.AppActivate("Window Title")
WScript.Sleep 1000

Change the "Window Title" to the actual window title you're sending the keys to.

Sendkeys needs to be performed like a well choreographed ballet. If the window you're sending keys to loses focus, all bets are off and you may end up sending keys to the wrong window. Try lowering the sleep times especially the one with a 20000 value.

Good luck.  8)
« Last Edit: July 09, 2012, 05:23:16 AM by Sidewinder »
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: Putty/VBS Help
« Reply #2 on: July 09, 2012, 12:48:26 PM »
I was experimenting with the 20 second wait, because I just assumed it needed more time to wait before it would accept input. I started with a 1s wait.
But Sidewinder, if the app is not active, how can it write anything at all?
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Putty/VBS Help
« Reply #3 on: July 09, 2012, 03:39:42 PM »
I can't duplicate the Putty window you're seeing (I only get PuTTY Configuration).

Some suggestions:

  • Replace the ENTER keys with TAB (except the last one). This will prevent dismissing the window prematurely.
  • Combine "sm_reloadadmins" into a single operation. Less instructions, less problems.
  • Lower the sleep times. I've always had better results with quick fire keystrokes and cursor movement. Too much can happen in 20 seconds.
  • Test the VBScript one control at a time and see how it progresses. For example, first test the script so "SUPERSECRETPASSWORD" gets entered correctly and the cursor moves to the next field. Add the next control to the VBS and test again.


Quote
if the app is not active, how can it write anything at all?

Sendkeys simply sends out keystrokes. Whether any application is there to catch them makes no difference.

Any chance you can use command line parameters instead of the GUI?

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: Putty/VBS Help
« Reply #4 on: July 09, 2012, 09:21:49 PM »
And here i thought you needed some windows re-glazed...
" Anyone who goes to a psychiatrist should have his head examined. "

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: Putty/VBS Help
« Reply #5 on: July 09, 2012, 09:33:16 PM »
I can't duplicate the Putty window you're seeing (I only get PuTTY Configuration).

Some suggestions:

  • Replace the ENTER keys with TAB (except the last one). This will prevent dismissing the window prematurely.
  • Combine "sm_reloadadmins" into a single operation. Less instructions, less problems.
  • Lower the sleep times. I've always had better results with quick fire keystrokes and cursor movement. Too much can happen in 20 seconds.
  • Test the VBScript one control at a time and see how it progresses. For example, first test the script so "SUPERSECRETPASSWORD" gets entered correctly and the cursor moves to the next field. Add the next control to the VBS and test again.


Sendkeys simply sends out keystrokes. Whether any application is there to catch them makes no difference.

Any chance you can use command line parameters instead of the GUI?

 8)
1. Can't replace enter keys with TAB or it won't execute the text. I tried doing it, and it typed out all the commands fine...into the user input field.
2. Done, now it sends "oadadmins".
3. See above.
4. The only part that experiences an issue is the final "sm_reloadadmins" part.

I'm not sure how I would use command line parameters for this, as once I run "screen -x" I'm no longer sending commands to putty, I'm sending them to the game server. I did use parameters for the username/password though.
I tried to do the AppActivate trick as well, and it didn't change anything from above.
Semi-functional code:
Code: [Select]
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """C:\Program Files (x86)\PuTTY\putty.exe"" root@IPADDRESS -pw SUPERSECRETPASSWORD", 9
WScript.Sleep 3000
WshShell.SendKeys "screen -x"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 1000
WshShell.AppActivate("[screen 0: srcds_run]")
WshShell.SendKeys "sm_reloadadmins"
WScript.Sleep 2000
WshShell.SendKeys "{ENTER}"
WScript.Sleep 2000

EDIT: Now it manages to write "reloadadmins"...
« Last Edit: July 09, 2012, 10:17:47 PM by Helpmeh »
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Putty/VBS Help
« Reply #6 on: July 10, 2012, 02:52:42 PM »
Quote
4. The only part that experiences an issue is the final "sm_reloadadmins" part.

Code: [Select]
WScript.Sleep 1000
WshShell.AppActivate("[screen 0: srcds_run]")
WshShell.SendKeys "sm_reloadadmins"

Try increasing the sleep parameter in this code block. Using sendkeys is more a balancing act than straight-up logic so you'll just have to play around with the numbers until it works. How busy your machine and the game server are also factor into how smoothly the final script will run.

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: Putty/VBS Help
« Reply #7 on: July 12, 2012, 12:09:58 AM »
I've tried modifying the sleep value before and after the appactivate, and the best I can get is _reloadadmins. I've even tried sending the SendKeys command more than once, even from different VBS files, and I still get the same problem.
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Re: Putty/VBS Help
« Reply #8 on: July 14, 2012, 01:10:14 AM »
Since VBS was being such a pain in the :-X and nothing I changed in the code had any effect, I moved on to AutoHotkey, which did the trick.
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

Rob Pomeroy



    Prodigy

  • Systems Architect
  • Thanked: 124
    • Me
  • Experience: Expert
  • OS: Other
Re: Putty/VBS Help
« Reply #9 on: July 19, 2012, 04:41:41 AM »
A bit late to suggest this, but a heads up anyway: Tera Term is a terminal application with a built-in scripting language.  It's idiosyncratic, but not difficult to learn.  The benefit is that it runs in much the same way as an "expect" script, so you don't really need to think about wait/recheck.  Well worth investigating: http://logmett.com/index.php?/products/teraterm.html
Only able to visit the forums sporadically, sorry.

Geek & Dummy - honest news, reviews and howtos