1

Hi I don't know much about windows scripting but I have this case where I need to connect to my email server with different protocols for testing and I need to telnet those constantly which is very useless thing to do over and over again so I thought I should write a windows script so I can automate having to type the same thing over and over again.

This is what I have so far:

echo off
title Login to Mail using IMAP,POP3 and SMTP
echo Please enter the protocol required (POP3=1,IMAP=2,SMTP=3)&  
set /P id= Enter Value :
echo You have selected option :%id%
IF %id%==1(
   telnet <ip address> 110
   user user@example.com
   pass 123
)
IF %id%==2(
   telnet <ip address> 143
   a login user@example.com 123
)
IF %id%==3(
   telnet <ip address> 25
   helo r
   auth login
   <base64encodedusername>
   <base64encodedpassword>
)

The commands itself works as expected when I type them on cmd but it wouldn't work in the batch file. I'm assuming my syntax for conditional statements is the culprit but I am not sure. Can anyone help?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Pissu Pusa
  • 1,218
  • 3
  • 16
  • 26

1 Answers1

2

There is a problem with your conditional syntax. It has to be: IF %id%==1 ( and so on with a space in front of the starting parenthesis. On ss64 is a nice guide about condionally performing commands.

But also after the correction it will not work as the telnet command is not scribtable in Windows batch. That means after the first telnet command your batch script will stuck in that and no other command will be executed. Look at this SO question about using telnet in batch. There are several answers which show ways how to accomplish your task.

This answer is working for several people according to the comments. It's using start telnet.exe <IP> and a Visual Basic Script (.vbs) to simulate key strokes. Quotation of that anwer:

Batch File (named Script.bat ):

:: Open a Telnet window
start telnet.exe 192.168.1.1
:: Run the script 
cscript SendKeys.vbs 

Command File (named SendKeys.vbs ):

set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 50 
OBJECT.SendKeys "mylogin{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys "mypassword{ENTER}"
WScript.sleep 50 
OBJECT.SendKeys " cd /var/tmp{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys " rm log_web_activity{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys " ln -s /dev/null log_web_activity{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys "exit{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys " "
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47