AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AutoPuTTY (scripted telnet) - need help

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
3tones



Joined: 14 Dec 2004
Posts: 45
Location: Minneapolis, MN

PostPosted: Mon Jan 14, 2008 8:20 pm    Post subject: AutoPuTTY (scripted telnet) - need help Reply with quote

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}

Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
3tones



Joined: 14 Dec 2004
Posts: 45
Location: Minneapolis, MN

PostPosted: Mon Jan 14, 2008 10:00 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
erictheturtle



Joined: 27 Jun 2007
Posts: 67
Location: California

PostPosted: Tue Jan 15, 2008 7:00 pm    Post subject: Reply with quote

Coincidentally I was wanting to do a similar thing recently. This is the solution I ran across.

http://www.windowsnetworking.com/kbase/WindowsTips/WindowsNT/AdminTips/Miscellaneous/RunbatchfileatWindowsNTstartupwithnooneloggedon.html
http://support.microsoft.com/kb/q243486/
_________________
-m35
Back to top
View user's profile Send private message
DJAnonimo



Joined: 10 Sep 2006
Posts: 148

PostPosted: Sun Jan 20, 2008 3:09 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message MSN Messenger
3tones



Joined: 14 Dec 2004
Posts: 45
Location: Minneapolis, MN

PostPosted: Sun Jan 20, 2008 9:55 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
DJAnonimo



Joined: 10 Sep 2006
Posts: 148

PostPosted: Mon Jan 21, 2008 10:14 pm    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message MSN Messenger
3tones



Joined: 14 Dec 2004
Posts: 45
Location: Minneapolis, MN

PostPosted: Mon Jan 21, 2008 10:39 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
DJAnonimo



Joined: 10 Sep 2006
Posts: 148

PostPosted: Tue Jan 22, 2008 1:28 am    Post subject: Reply with quote

Yea, AHK.

Code:
Run, puttytel.exe, , Min UseErrorLevel, CMDPID
WinWait, ahk_pid %CMDPID%
ControlSend, , %username%{Enter}, ahk_pid %CMDPID%
Back to top
View user's profile Send private message MSN Messenger
3tones



Joined: 14 Dec 2004
Posts: 45
Location: Minneapolis, MN

PostPosted: Tue Jan 22, 2008 2:18 am    Post subject: Reply with quote

So there is no real 2-way communication? For example, wait for the command prompt and then run the command?
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
DJAnonimo



Joined: 10 Sep 2006
Posts: 148

PostPosted: Tue Jan 22, 2008 10:09 am    Post subject: Reply with quote

AHK cant detect cmd prompt or putty text.
maybe if putty make some log you can use it for back information.
Back to top
View user's profile Send private message MSN Messenger
Guest






PostPosted: Tue Jan 22, 2008 12:52 pm    Post subject: Reply with quote

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 ... Cool ... allowing 2way communication ...
Back to top
OleTrole
Guest





PostPosted: Tue Jan 22, 2008 9:22 pm    Post subject: Reply with quote

Why don't you use plink.exe that comes with putty.
Back to top
DJAnonimo



Joined: 10 Sep 2006
Posts: 148

PostPosted: Sun Jan 27, 2008 8:21 pm    Post subject: Reply with quote

Maybe easier...

Anyway new version on:
http://www.autohotkey.com/forum/viewtopic.php?t=28024&highlight=autotelnet
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group