Telnet router via batch, get info and export to csv file - batch-file

I set up a small 3 router network via GNS3. I can connect on it via my pc (telnet) and execute simple cisco commands like show int brief.
Now I found a batch file here that I modified a little to login all 3 routers and execute the show ip int brief command. I have all the info on my screen, but I would like to have it in a .csv file.
I changed the router IP's
R1: 1.1.1.1, R2: 2.2.2.2, R3: 3.3.3.3
This is the batch-file
:: Open a Telnet window
start telnet.exe 1.1.1.1
:: Run the script
cscript showipintbrief.vbs
This is the vbs file (showipintbrief.vbs):
set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 50
OBJECT.SendKeys "loginR1password{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "enable{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "enableR1password{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "show ip int brief{ENTER}"
WScript.sleep 50
Echo Interface, IP-Address > showipintbrief.csv
OBJECT.SendKeys "telnet 2.2.2.2{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "loginR2password{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "enable{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "enableR2password{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "show ip int brief{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "telnet 3.3.3.3{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "loginR3password{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "enable{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "enableR3password{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "show ip int brief{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "exit{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "exit{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "exit{ENTER}"
For instance Interface, Ip-address, status and protocol should be sent to a .csv file
Anyone can help wiht this please?
Thanks a lot

Try this to log the data and execute it from the cmd prompt, where batchfile.bat is your batch file name.
batchfile.bat >file.log

Related

VBScript - Bring Microsoft Edge window to foreground

My script launches a MS Edge window and then tries to log into Netflix with my credentials. The problem is, at times, the browser window comes up behind the CMD console and the script doesn't work.
How do I launch the browser in the foreground or bring it to the foreground after it's launched?
<!-- :
rem #Echo Off
Set "usr_name=#####"
Set "usr_pass=#####"
%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf" "%browser%" "%usr_name%" "%usr_pass%"
Exit /B
-->
<Job>
<Script Language="VBScript">
Dim ObjShell, MyChrome
Set ObjShell = WScript.CreateObject("WScript.Shell")
MyChrome = Chr(34) & WScript.Arguments(0) & Chr(34)
ObjShell.Run MyChrome & "--app=""https://www.netflix.com/login""", 1
WScript.Sleep 5000
ObjShell.SendKeys "{TAB}"
ObjShell.SendKeys "{TAB}"
ObjShell.SendKeys(WScript.Arguments(1))
ObjShell.SendKeys "{TAB}"
ObjShell.SendKeys(WScript.Arguments(2))
ObjShell.SendKeys "{ENTER}"
WScript.Sleep 6000
ObjShell.SendKeys "{ESCAPE}"
ObjShell.SendKeys "{TAB}"
WScript.Sleep 1000
ObjShell.SendKeys "{TAB}"
WScript.Sleep 1000
ObjShell.SendKeys "{ENTER}"
WScript.Sleep 6000
ObjShell.SendKeys "%{F4}"
</Script>
</Job>
Scripts that depend on SendKeys are notoriously unreliable. A more reliable approach is to control the browser with Seleniumbasic and Chrome driver or use WebView2 in a C# program. Barring that, one option for adding window control functionality to your existing script is to use the command line tool Cmdow. For example:
ObjShell.Run "cmdow ""Netflix*"" /MAX",1,False
Update:
You can also try the built-in AppActivate method:
ObjShell.AppActivate "Netflix"
The solution to to use a powershell cmdlet to minimize all windows before running the script that launches the browser.
'''powershell -command "(new-object -com shell.application).minimizeall()'''

Calling two scripts from batch file run asynchronously

I have batch file which gets user input, tranfers the input to a vb script.
set /p "whichPort= Which port do you want to reset:"
start /b "" cscript.exe //NoLogo loginInXyplex.vbs /portname:"%whichPort%"
Then a telnet session is exceuted from within the batch file, followed by the exection of the same script file in order to issue commands in the telnet session.
telnet.exe 192.120.187.35 2000
REM run the script
cscript loginInXyplex.vbs
When in the telnet session it appears the two scripts are running at the same time as the conmmands are out of synch so cannot log on properly
This is my first vb script so I could be missing somethong obivious, so i have included all source code :
Batch File:
#echo off
cls
echo.
echo Welcome to Xyplex Server Port Reset
echo.
pause
echo.
set /p whichXplex= Which Xyplex server is your device connected too: 1 or 2 ?
echo.
set /p "whichPort= Which port do you want to reset:"
echo Sending port "%whichPort%" to script file
echo.
pause
rem send using strat /b so we open script file on same command line window
start /b "" cscript.exe //NoLogo loginInXyplex.vbs /portname:"%whichPort%"
echo.
echo Conencting to Xyplex "%whichXplex%"
pause
if "%whichXyplex%" == "1" (
REM COnnecting to xyplex one IP:Socket
echo.
echo Connecting to Xpyplex %whichXyplex%...
telnet.exe 192.120.187.35 2000
REM run the script
cscript loginInXyplex.vbs
) ELSE (
rem ^ missing spaces in )ELSE(
echo Conencting to xyplex %whichXyplex%....
telnet.exe 193.120.187.245 2000
REM run the script in same widow wuth start /b
cscript loginInXyplex.vbs
)
Script:
set OBJECT=WScript.CreateObject("WScript.Shell")
OBJECT.SendKeys"{ENTER}"
OBJECT.SendKeys "access{ENTER}"
WScript.sleep 50
OBJECT.SendKeys "access{ENTER}"
OBJECT.SendKeys "SET PRIV{ENTER}"
OBJECT.SendKeys "system{ENTER}"
OBJECT.SendKeys "sh po all{ENTER}"
OBJECT.SendKeys "{ENTER}"
' source heer: http://stackoverflow.com/questions/21013428/pass-variable-from-batch-to-vbs
port = WScript.Arguments.Named.Item("portname")
OBJECT.SendKeys port
OBJECT.SendKeys "Port number selected is: " & port
OBJECT.SendKeys "{ENTER}"
OBJECT.SendKeys "Port reset....logging off...{ENTER}"
OBJECT.SendKeys "SET NOPRIV {ENTER}"
Wscript.Sleep 1000
OBJECT.SendKeys "Quit"
Any help appreciated.

How to automatically export contacts from Thunderbird

Is there some way to export contacts from Thunderbird in CSV silently by using script, so we won't have to do it manually for every user?
My intention is to move all gathered addresses (stored in history.mab) from Thunderbird to Outlook 2013 on multiple stations.
After some research, there is probably no straight way how to do it.
I have ended up with this simple macro, which start Thunderbird and via adress book export selected contact list into CSV - far from ideal, but it works in most cases. (only works as intended with constant amount of lists)
SET oShell = CreateObject( "WScript.Shell" )
oShell.Run """C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"""
WScript.Sleep 10000 ' wait a bit for opening Thunderbird
oShell.SendKeys "^+b" 'open address book
WScript.Sleep 2000
oShell.SendKeys "{TAB}{TAB}"
oShell.SendKeys "{DOWN}"
oShell.SendKeys "%n"
oShell.SendKeys "x"
WScript.Sleep 2000
oShell.SendKeys "thunderbird_export_" & username & "" 'name your CSV
WScript.Sleep 1000
oShell.SendKeys "{TAB}"
oShell.SendKeys "{DOWN}{DOWN}"
oShell.SendKeys "{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "^l"
WScript.Sleep 1000
oShell.SendKeys "u:{ENTER}" 'set destination folder
WScript.Sleep 1000
oShell.SendKeys "%u"
WScript.Sleep 1000
oShell.SendKeys "{ENTER}"
WScript.Sleep 1000
' close both adress book and Thunderbird
oShell.Run("taskkill /im thunderbird.exe")
WScript.Sleep 1000
oShell.Run("taskkill /im thunderbird.exe")
WScript.Quit

Using Batch file and VBS to change an IP

I have equipment that comes with a default ip address that I need to change via telnet.
The new ip will always be the same except for the last two octets.
I currently access the equipment with a batch file that call on a .vbs
#ECHO OFF
:: Get Hub IP
set /p input1="Enter Hub and press ENTER "
set /p input2="Enter Shelf and press ENTER "
#ECHO OFF
::Run script to Set IP Address
start telnet.exe 10.230.%input1%.%input2%
cscript SetIPAddress.vbs .
echo "DONE!"
pause
HERE IS THE VBS
set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 1000
OBJECT.SendKeys "admin{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "admin{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "configure{ENTER}"
WScript.sleep 2000
This works fine
What I need is to add something like this
#ECHO OFF
SET /p paramA=Hub:
SET /p paramB=Shelf:
ECHO set interfaces eth0 10.230.%paramA%.254 mask 255.255.255.0 gateway 10.230.%paramA%.254
PAUSE
This also works fine but I’m not sure how to get it to send to the command prompt.
Can it be added to the .vbs or can the elements of the .vbs be added to the batch file?

telnet connection to multiple hosts and ports

i need an example script or bat file where by i want to do the following :>
test the telnet connection and ports to 3 different machines and ports at the same time.
the machine is very locked down, so unable to install putty or any other 3rd party apps or have web access.
I just need the script to telnet to the 1st box and then move to the next.
i have tried the script below but it just stops on the 1st box
<job>
<script language="VBScript">
Option Explicit
On Error Resume Next
Dim WshShell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
WScript.Sleep 1000
WshShell.SendKeys "telnet x.x.x.x 7005"
WshShell.SendKeys ("{Enter}")
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WshShell.SendKeys "telnet x.x.x.x 8600"
WshShell.SendKeys ("{Enter}")
WshShell.SendKeys "exit"
WshShell.SendKeys ("{Enter}")
WScript.Quit
</script>
</job>
hope someone can shed some light on this.
Many thanks
Marzanur
There are several issues with this script:
You need to seperatate the
Telnet *.*.*.* 7005
Commands with
wscript.sleep 1000
So that the computer has time to connect. Secondly. Telnet opens in a unique command line interface and "exit" is not an accepted by the telnet interface. However, "quit" is used.
So to replace:
Wshshell.sendkeys "exit"
With
Wshshell.sendkeys "quit"
'Wait for close
Wscript.sleep 500
Telnet commands -> http://technet.microsoft.com/en-us/library/c.aspx
WshShell.SendKeys "telnet x.x.x.x 7005"
WshShell.SendKeys "{Enter}"
WshShell.SendKeys "^]"
WshShell.SendKeys "quit{Enter}"
CTRL + ] jumps out of your telnet session and to the terminal console where you can type quit.

Resources