Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

AutoPuTTY (scripted telnet) - need help


  • Please log in to reply
12 replies to this topic
3tones
  • Members
  • 45 posts
  • Last active: Jan 21 2008 10:38 PM
  • Joined: 14 Dec 2004
I know this is a hot topic on the board, and I have come up with a solution to log into a telnet server, and run some commands. (I didn't find any working solutions yet) The framework is there, you'll need to create the logic. Also note that you need to setup a putty profile and set it to log the session.

HOWEVER...

This works for me, but I need it to run as a service. I am using Send to send commands to the PuTTY window, which doesn't work when a user is not logged in. Does anyone have any suggestions on how to accomplish this?

;Tom Thomalla Jr.
;1/14/08
;AHK Ver 1.0.47.04
;PuTTY ver 0.58
;
;AutoPuTTY
;logs into a telnet session with putty, and runs some commands

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.

logfile = %A_ScriptDir%\putty.log
;NOTE: Must launch putty manually and setup this session name
session = test session

;change the session name to the way it is in the registry
stringreplace session2, session, %A_SPACE%, `%20 , 1

;delete the log file before putty is launched
filedelete %logfile%

;Change the location of the logfile if the script is moved
RegWrite, REG_SZ, HKEY_CURRENT_USER, Software\SimonTatham\PuTTY\Sessions\%session2%, LogFileName, %logfile%

;launch putty
run,C:\Program Files\PuTTY\putty.exe -load "%session%"

;look at the log file file for the login prompt every 100ms
loop {
  Loop, read, %logfile%
    last_line := A_LoopReadLine
  if last_line=login:
    break
  sleep 100
}

;Send the login name
Send login name{ENTER}

;look at the log file file for the password prompt every 100ms
loop {
  Loop, read, %logfile%
    last_line := A_LoopReadLine
  if last_line=password:
    break
  sleep 100
}

;Send the password
Send password{ENTER}

;wait for a prompt
loop {
  Loop, read, %logfile%
    last_line := A_LoopReadLine
  StringGetPos,pos,last_line,#
  if pos >=0
    break
  sleep 100
}

;do something
Send your command here{ENTER}

;wait for the prompt
loop {
  Loop, read, %logfile%
    last_line := A_LoopReadLine
  StringGetPos,pos,last_line,#
  if pos >=0
    break
  sleep 100
}

;logout
Send exit{ENTER}

;wait for login prompt
loop {
  Loop, read, %logfile%
    last_line := A_LoopReadLine
  if last_line=login:
    break
  sleep 100
}

;end session
Send ^C^]{ENTER}



3tones
  • Members
  • 45 posts
  • Last active: Jan 21 2008 10:38 PM
  • Joined: 14 Dec 2004
Something like this doesn't appear to work either when running using the windows scheduler with no one logged in.

run,C:\Program Files\PuTTY\putty.exe -load "%session%",,,ppid
SendMessage, 0x100, 0x41, 0,, ahk_pid %ppid%
;0x100 = WM_KEYDOWN
;0x41 = the 'a' key


erictheturtle
  • Members
  • 101 posts
  • Last active: Sep 04 2011 02:07 PM
  • Joined: 27 Jun 2007
Coincidentally I was wanting to do a similar thing recently. This is the solution I ran across.

<!-- m -->http://www.windowsne... ... gedon.html<!-- m -->
<!-- m -->http://support.micro...com/kb/q243486/<!-- m -->
-m35

DJAnonimo
  • Members
  • 171 posts
  • Last active: Apr 30 2013 01:20 AM
  • Joined: 10 Sep 2006
I did a script for it just now.


If you want to change the script or configurations
you can Delete config.ini and run AutoTelnet or simply edit the .ini file.
Script.txt is the file where you put the commands which you would like to execute.
I recommend to use Putty for telnet to avoid some chars (\, |, <, >, [, ]) problems which Microsoft Telnet does.

Limitation: you can't use { } brackets they simply produces spaces.



DOWNLOAD LINK : https://ahknet.autoh...o/autotel10.zip

DJAnonimo

3tones
  • Members
  • 45 posts
  • Last active: Jan 21 2008 10:38 PM
  • Joined: 14 Dec 2004
To get the whole works to run as a service, I ended up using a utility called "Telnet Scripting Tool" to do the work for me. I build an AHK script around that to add some other things I needed.

I did a script for it just now.
DOWNLOAD LINK : https://ahknet.autoh...o/autotel10.zip
DJAnonimo

DJAnonimo - I am interested in your script - Is the source of the script available?

DJAnonimo
  • Members
  • 171 posts
  • Last active: Apr 30 2013 01:20 AM
  • Joined: 10 Sep 2006
Source is not available.

Here is new version
DOWNLOAD LINK : <!-- m -->https://ahknet.autoh...o/autotel11.zip<!-- m -->

v1.1
1) Bugfix
2) Added Sleep in "Commands to send" window. (Wait X milliseconds between lines)
With negative value the script is a bit faster but it can cause skips.
3) Added "Sl33p, <ms>" command. (Wait X milliseconds for a single line)
4) Supports SSH with Putty (putty.exe -ssh IP)

3tones
  • Members
  • 45 posts
  • Last active: Jan 21 2008 10:38 PM
  • Joined: 14 Dec 2004

Source is not available.


Can you tell me then - was it programmed in AutoHotkey? If so, how did you make the 2-way connection with PuTTY?

DJAnonimo
  • Members
  • 171 posts
  • Last active: Apr 30 2013 01:20 AM
  • Joined: 10 Sep 2006
Yea, AHK.

Run, puttytel.exe, , Min UseErrorLevel, CMDPID
WinWait, ahk_pid %CMDPID%
ControlSend, , %username%{Enter}, ahk_pid %CMDPID%


3tones
  • Members
  • 45 posts
  • Last active: Jan 21 2008 10:38 PM
  • Joined: 14 Dec 2004
So there is no real 2-way communication? For example, wait for the command prompt and then run the command?

DJAnonimo
  • Members
  • 171 posts
  • Last active: Apr 30 2013 01:20 AM
  • Joined: 10 Sep 2006
AHK cant detect cmd prompt or putty text.
maybe if putty make some log you can use it for back information.

  • Guests
  • Last active:
  • Joined: --
Have a try with TeraTerm http://ttssh2.sourceforge.jp/
TeraTerm is the terminal emulator for Microsoft Windows, that supports serial port, telnet and SSH connections. Among many other features it also has built in Macro scripting language ... 8) ... allowing 2way communication ...

OleTrole
  • Guests
  • Last active:
  • Joined: --
Why don't you use plink.exe that comes with putty.

DJAnonimo
  • Members
  • 171 posts
  • Last active: Apr 30 2013 01:20 AM
  • Joined: 10 Sep 2006
Maybe easier...

Anyway new version on:
http://www.autohotke...ight=autotelnet