AutoHotkey Community

It is currently May 27th, 2012, 10:40 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: January 14th, 2008, 9:20 pm 
Offline

Joined: December 14th, 2004, 11:34 pm
Posts: 45
Location: Minneapolis, MN
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?

Code:
;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}



Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2008, 11:00 pm 
Offline

Joined: December 14th, 2004, 11:34 pm
Posts: 45
Location: Minneapolis, MN
Something like this doesn't appear to work either when running using the windows scheduler with no one logged in.

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2008, 8:00 pm 
Offline

Joined: June 27th, 2007, 9:07 pm
Posts: 101
Location: California
Coincidentally I was wanting to do a similar thing recently. This is the solution I ran across.

http://www.windowsnetworking.com/kbase/ ... gedon.html
http://support.microsoft.com/kb/q243486/

_________________
-m35


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2008, 4:09 pm 
Offline

Joined: September 10th, 2006, 1:41 pm
Posts: 157
I did a script for it just now.


Quote:
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 : http://www.autohotkey.net/~DJAnonimo/autotel10.zip

DJAnonimo


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2008, 10:55 pm 
Offline

Joined: December 14th, 2004, 11:34 pm
Posts: 45
Location: Minneapolis, MN
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.

DJAnonimo wrote:
I did a script for it just now.
DOWNLOAD LINK : http://www.autohotkey.net/~DJAnonimo/autotel10.zip
DJAnonimo

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2008, 11:14 pm 
Offline

Joined: September 10th, 2006, 1:41 pm
Posts: 157
Source is not available.

Here is new version
DOWNLOAD LINK : http://www.autohotkey.net/~DJAnonimo/autotel11.zip

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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2008, 11:39 pm 
Offline

Joined: December 14th, 2004, 11:34 pm
Posts: 45
Location: Minneapolis, MN
DJAnonimo wrote:
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2008, 2:28 am 
Offline

Joined: September 10th, 2006, 1:41 pm
Posts: 157
Yea, AHK.

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2008, 3:18 am 
Offline

Joined: December 14th, 2004, 11:34 pm
Posts: 45
Location: Minneapolis, MN
So there is no real 2-way communication? For example, wait for the command prompt and then run the command?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2008, 11:09 am 
Offline

Joined: September 10th, 2006, 1:41 pm
Posts: 157
AHK cant detect cmd prompt or putty text.
maybe if putty make some log you can use it for back information.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2008, 1:52 pm 
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 ...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2008, 10:22 pm 
Why don't you use plink.exe that comes with putty.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2008, 9:21 pm 
Offline

Joined: September 10th, 2006, 1:41 pm
Posts: 157
Maybe easier...

Anyway new version on:
http://www.autohotkey.com/forum/viewtopic.php?t=28024&highlight=autotelnet


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group