Using Batch file and VBS to change an IP - batch-file

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?

Related

Batch user input adding new line/haracter at the end of data in txt

I'm trying to write simple bat script, to take user input data and place it in couple of txt files (configuration files for python program)
But always on the end of placed data in txt, there is new line, like it makes "enter" on the end of placed data. I readed that it's beacause, when user input data in cmd window, he need press "Enter" to pass it, so the key press is also recorded and passed to txt file. I can be wrong with it.
I need to get ride of it in order to data be proper readed.
I didn't figured how to do it.
My code is ugly and mess, but working (except this problem)
I started making it 3 days ago
echo (1) Start
echo (2) Resume session
echo.
set /A "M=10*%ERRORLEVEL:~-1%"
CHOICE /C:12345 /M "Wybierz opcje:"
set /A "M+=%ERRORLEVEL:~-1%
:: Notatka - ERRORLEVELS decreasing
if %M%== 2 goto Resume
if %M%== 1 goto Start
:Start
set /p id= "channel ID:"
set /p sid= "Server ID:"
set /p text= "text:"
set /p time= "time:"
::ID of channel
echo
cd C:\Users\LoveRussia\Documents\DiscordBotClient\automatic-discord-message-main\spam skrypt\1\textfiles
echo %id% > channel.txt
::Link, made with server ID and channel ID
cd C:\Users\LoveRussia\Documents\DiscordBotClient\automatic-discord-message-main\spam skrypt\1\textfiles
echo https://discord.com/channels/%sid%/%id% > channellink.txt
::the message
echo %text%
cd C:\Users\LoveRussia\Documents\DiscordBotClient\automatic-discord-message-main\spam skrypt\1\textfiles
echo %text% > message.txt
::time between messages
echo %time%
cd C:\Users\LoveRussia\Documents\DiscordBotClient\automatic-discord-message-main\spam skrypt\1\textfiles
echo %time% > time.txt
echo Press to proceed
pause
:Resume
::Main process should start now
cd C:\Users\LoveRussia\Documents\DiscordBotClient\automatic-discord-message-main\spam skrypt\1
start py AutoMessage.py
timeout 1 > nul

Change the hostname using the batch script

Hi i wanted to change the value of hostname for new computer but things didn't work as i try to input the value and getting the value for the hostname. I try to restart the computer the result is still the same.. I am testing to see whether this script is capable of updating the hostname for pcs or computer name
thanks
here is my code
REM This script runs in MS-DOS Batch File Language
#echo off
set /p id= Enter ID or Hostname:
echo %id%
WMIC computersystem where caption='%ComputerName%' rename %id%
REM exit the applications
echo "Export completed successfully. Press any key to exit"
pause >nul
exit /B
Here's how I'd probably do it:
#Echo Off
Echo Your current name is %ComputerName%
:AskID
Set "ID="
Set /P "ID=Enter your new name: "
If Not Defined ID (Echo Can not be empty
GoTo AskID)
If /I "%ID%"=="%ComputerName%" Exit /B
If "%ID:~,1%"=="." (Echo Must not begin with a period
GoTo AskID)
Rem Put here some more checks for disallowed words or characters
WMIC ComputerSystem Where Name="%ComputerName%" Call Rename "%ID%"
Notes
This will need to be run 'As administrator'.
The change will not take effect until the next reboot.
It is important that you don't allow your end user, especially when running an administrative task to just enter anything at the prompt. Please consider following the Remarked line's advice.
I tested the below batch script to change the windows 10 hostname through batch script and it work perfectly fine try it out and reboot the system atlast !
set /p NEW_NAME="Please enter computer name: "
for /f %%i in ('hostname') do set OLD_NAME=%%i
echo %OLD_NAME%
echo %NEW_NAME%
WMIC computersystem where caption="%OLD_NAME%" rename "%NEW_NAME%"
pause

Batch scripting.. i want to run a command automatically based upon user input

writing a script so that when a certain value is returned it opens cmd. Then using a value that the user has added automatically run a command in cmd. not sure if this is even possible or not but any help would be appreciated.
Thanks
Edited - the code before
:OpekaiNetReset
echo "Target IP; host is DOWN!"
echo Enter your target IP
set/p TargetIP="Enter your target IP"
ping %TargetIP%
IF %TargetIP%==packets recieved +1 start cmd.exe
IF %TargetIp%==packets recieved 0 goto OpekaiNetReset
:OpekaiCMD
cls
echo "Target IP; host is UP!"
Just taking a wing at things, not being sure exactly what you want -- I commented out a few of your lines via :: and added replacements. The below should at least tell you if a host is up/down based on received packets from ping. Unsure what you meant by running cmd.exe on a certain count? The below runs command if the received packet count is 1.
:OpekaiNetReset
echo "Target IP; host is DOWN!"
::echo Enter your target IP
set/p TargetIP="Enter your target IP: "
::ping %TargetIP%
set received=0
for /f "tokens=2 delims=," %%p in ('ping %TargetIP%^|find /i "received"') do set /a %%p
echo packets received == %received%
::IF %TargetIP%==packets recieved +1 start cmd.exe
if %received%==1 start cmd.exe & exit /b
::IF %TargetIp%==packets recieved 0 goto OpekaiNetReset
if %received%==0 goto OpekaiNetReset
:OpekaiCMD
cls
echo "Target IP; host is UP!"

batch file log multiple telnet sessions

I have a batch file that logs into a device gathers inf and logs out. the data is recorded to a txt file. it then opens another telnet session gathers data and logs. my proble is i have two logs. any way to combine them into one?
#echo off
cls
:start
#ECHO OFF
:: Get Info
set /p input1="Enter First IP Address and press ENTER "
set /p input2="Enter Second IP Address and press ENTER "
#ECHO OFF
::Run script to Get Info
start telnet.exe -f C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input1%".txt "%input1%"
cscript /nologo 1.vbs
timeout /t 3
taskkill /im telnet.exe /f
timeout /t 2
#ECHO OFF
::Run script to Get Info
start telnet.exe -f C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input2%".txt "%input2%"
cscript /nologo 1.vbs
echo "DONE!"
pause
Just concatenate both logs in the end with type command
type C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input1%".txt C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input2%".txt > C:\Users\%username%\Desktop\TELNET\CHANGE_ME_TOTAL.txt
Before that, you'll need to wait until second telnet is finished and maybe kill it like you did with the first one, or second log file will be locked and unreadable.

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.

Resources