| View previous topic :: View next topic |
| Author |
Message |
3tones
Joined: 14 Dec 2004 Posts: 45 Location: Minneapolis, MN
|
Posted: Mon Jan 14, 2008 8:20 pm Post subject: AutoPuTTY (scripted telnet) - need help |
|
|
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 |
|
 |
3tones
Joined: 14 Dec 2004 Posts: 45 Location: Minneapolis, MN
|
Posted: Mon Jan 14, 2008 10:00 pm Post subject: |
|
|
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 |
|
 |
erictheturtle
Joined: 27 Jun 2007 Posts: 67 Location: California
|
|
| Back to top |
|
 |
DJAnonimo
Joined: 10 Sep 2006 Posts: 148
|
Posted: Sun Jan 20, 2008 3:09 pm Post subject: |
|
|
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 |
|
 |
3tones
Joined: 14 Dec 2004 Posts: 45 Location: Minneapolis, MN
|
Posted: Sun Jan 20, 2008 9:55 pm Post subject: |
|
|
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 - I am interested in your script - Is the source of the script available? |
|
| Back to top |
|
 |
DJAnonimo
Joined: 10 Sep 2006 Posts: 148
|
Posted: Mon Jan 21, 2008 10:14 pm Post subject: |
|
|
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 |
|
 |
3tones
Joined: 14 Dec 2004 Posts: 45 Location: Minneapolis, MN
|
Posted: Mon Jan 21, 2008 10:39 pm Post subject: |
|
|
| 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 |
|
 |
DJAnonimo
Joined: 10 Sep 2006 Posts: 148
|
Posted: Tue Jan 22, 2008 1:28 am Post subject: |
|
|
Yea, AHK.
| Code: | Run, puttytel.exe, , Min UseErrorLevel, CMDPID
WinWait, ahk_pid %CMDPID%
ControlSend, , %username%{Enter}, ahk_pid %CMDPID% |
|
|
| Back to top |
|
 |
3tones
Joined: 14 Dec 2004 Posts: 45 Location: Minneapolis, MN
|
Posted: Tue Jan 22, 2008 2:18 am Post subject: |
|
|
| So there is no real 2-way communication? For example, wait for the command prompt and then run the command? |
|
| Back to top |
|
 |
DJAnonimo
Joined: 10 Sep 2006 Posts: 148
|
Posted: Tue Jan 22, 2008 10:09 am Post subject: |
|
|
AHK cant detect cmd prompt or putty text.
maybe if putty make some log you can use it for back information. |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Jan 22, 2008 12:52 pm Post subject: |
|
|
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 ... ... allowing 2way communication ... |
|
| Back to top |
|
 |
OleTrole Guest
|
Posted: Tue Jan 22, 2008 9:22 pm Post subject: |
|
|
| Why don't you use plink.exe that comes with putty. |
|
| Back to top |
|
 |
DJAnonimo
Joined: 10 Sep 2006 Posts: 148
|
|
| Back to top |
|
 |
|