Windows – Need a Windows batch script that will telnet into a server and run a command

batch-filewindows

Looking for Windows batch file which will telnet to one IP address, automatically provide username & password which is specified in batch file, execute some series of commands and exit from telnet.

@echo off
SET username=abc
SET password=xyz@1234
SET servername=192.168.1.40

echo user %username%> telnetcmd.dat
echo %password%>> telnetcmd.dat
echo adbd^& >> telnetcmd.dat
echo exit>> telnetcmd.dat
telnet %servername% < telnetcmd.dat

del telnetcmd.dat

Here I am trying to do telnet to IP with username & password. After successful login it should execute adbc& (This will run necessary service in background) command and exit command. Once I complete this batch file and if I check adbc service running on server or not it is showing not running on server.

Output I am getting after completion of batch file.

Welcome to Microsoft Telnet Client

Escape Character is 'CTRL+]'


c:\Users\vshah\Desktop>

Expected Output:

root@pqrs:/ # ps | grep adbd
shell     31899 31828 1348   148   ffffffff 00013348 S adbd

Actual Output after executing batch file:

root@pqrs:/ # ps | grep adbd
1|root@pqrs:/ #

Kindly help me on this to achieve this scenario.

Thank your very much in advance.

Best Answer

It is not actually a batch, but rather a vbscript, and it uses sleep, so in high latency situations, it easily fail, but I have used the following script to test sending email via telnet

smtpadr = InputBox("SMTP server address", "","192.168.1.35" , 100, 200)
localname = InputBox("SMTP", "","mx01.domain.com" , 100, 200)
emailadr = InputBox("Email address", "","jonbdk@domain.com" , 100, 200)
if smtpadr & "" = "" OR localname & "" = "" OR emailadr & "" = "" then
    wscript.quit
end if

set Shell=CreateObject("WScript.Shell")

Shell.run "cmd /K"
wscript.sleep(1000)
sendkeys ("telnet "&smtpadr&" 25{ENTER}")
wscript.sleep(4000)
'sendkeys ("quit{ENTER}")
sendkeys ("HELO "&localname&"{ENTER}")
wscript.sleep(2000)
sendkeys ("MAIL FROM:<"&emailadr&">{ENTER}")
wscript.sleep(1000)
sendkeys ("RCPT TO:<"&emailadr&">{ENTER}")
wscript.sleep(1000)
sendkeys ("DATA{ENTER}")
wscript.sleep(1000)
sendkeys ("FROM:<"&emailadr&">{ENTER}")
wscript.sleep(100)
sendkeys ("TO:<"&emailadr&">{ENTER}")
wscript.sleep(100)
sendkeys ("Date:16-05-2010{ENTER}")
wscript.sleep(100)
sendkeys ("{ENTER}")
wscript.sleep(100)
sendkeys ("Hello World{ENTER}")
wscript.sleep(100)
sendkeys (".{ENTER}")


function sendkeys (strkeys)
on error resume next
' BACKSPACE {BACKSPACE}, {BS}, or {BKSP} 
' BREAK {BREAK} 
' CAPS LOCK {CAPSLOCK} 
' DEL or DELETE {DELETE} or {DEL} 
' DOWN ARROW {DOWN} 
' END {END} 
' ENTER {ENTER} or ~ 
' ESC {ESC} 
' HELP {HELP} 
' HOME {HOME} 
' INS or INSERT {INSERT} or {INS} 
' LEFT ARROW {LEFT} 
' NUM LOCK {NUMLOCK} 
' PAGE DOWN {PGDN} 
' PAGE UP {PGUP} 
' PRINT SCREEN {PRTSC} 
' RIGHT ARROW {RIGHT} 
' SCROLL LOCK {SCROLLLOCK} 
' TAB {TAB} 
' UP ARROW {UP} 
' F1 {F1} 
  shell.sendkeys(strKeys)
if err.number <> 0 then debug "Failed to sendkeys """ & strkeys &""""
on error goto 0
end function