Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
tautvydas

Batch script guides ?

Recommended Posts

Posted · Original PosterOP

for a while iv been looking at batch script guides and well it seems like they are rarely explaining anything.

i have some experience in c++ and whole pages dev. group PHP, JavaScript ,HTML stuff like that and some SQL . but no matter what i just cant seem to grasp batch scripting.

 

i need to write a short script that would extract specific elements from .txt file and would form another file with those values in commands.

i'm going to share my though process on how i want to approach this and if anyone has ideas on how to realize it, or has any videos or links that would explain how to do it that would be nice.

okay so lets say i have  an  "Network.txt." file witch has lots of lines of text, example :

192.168.0.101 , AA-BB-CC-11-22-33
192.168.0.102 , BB-AA-CA-1A-2B-3C
192.168.0.103 , 7C-D4-A2-F1-5B-AA
...

and with some sort of file reading command i would start to read data from text file and using some sort of cycle put them into arrays like I[x] for ip's C[x] for comma and M[x] for MAC adresses , assuming that command that reads file separates entries by spaces ( or somehow reading up until comma and then by end of the line , only seen this done in php but honestly i never tried it nor i remeber it how)
then when i have to put that output text somewhere i would jsut turn the cycle and just get the text formed like that

add action=drop chain=forward src-address= I[x] src-mac-address=! M[x]

so it would look something like this:

add action=drop chain=forward src-address=192.168.0.101 src-mac-address=!AA:BB-CC-11-22-33
add action=drop chain=forward src-address=192.168.0.102 src-mac-address=!BB-AA-CA-1A-2B-3C
add action=drop chain=forward src-address=192.168.0.103 src-mac-address=!7C-D4-A2-F1-5B-AA

 

main reason i'm looking into batch for this is that you should be able to run this script using .bat file without needing any compiler right ?

Link to post
Share on other sites

Batch is some half programming language that shouldn't exist. Consider Powershell or AutoIt.


PLEASE QUOTE ME IF YOU ARE REPLYING TO ME
LinusWare Dev | NotCPUCores Dev

Desktop Build: Ryzen 7 1800X @ 4.0GHz, AsRock Fatal1ty X370 Professional Gaming, 32GB Corsair DDR4 @ 3000MHz, RX480 8GB OC, Benq XL2730 1440p 144Hz FS

Retro Build: Intel Pentium III @ 500 MHz, Dell Optiplex G1 Full AT Tower, 768MB SDRAM @ 133MHz, Integrated Graphics, Generic 1024x768 60Hz Monitor


 

Link to post
Share on other sites
22 minutes ago, tautvydas said:

batch script

What OS?


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites
Posted · Original PosterOP
1 minute ago, rcmaehl said:

Batch is some half programming language that shouldn't exist. Consider Powershell or AutoIt.

arent powershell and batch are kinda the same thing ?

Link to post
Share on other sites
Posted · Original PosterOP
Just now, WereCatf said:

What OS?

windows

bash is for linux as far as i know, basicly anything works for me as long as it does not need extra compiler to be downloaded

Link to post
Share on other sites
sed 's/\([0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\) , /add action\=drop chain\=forward src-address\=\1 src-mac-address\=\!/' Network.txt

That should work for any valid IP/MAC pair, if you keep the same syntax.

Belt and braces would be to regex the MAC too, and do some input sanitation (filter out empty line comments etc)

Could just as easily be done in perl, tho that seems a bit overkill for simple search/replace regex.

 

Powershell does have some match capability, but it's criminally under documented.

Link to post
Share on other sites
IFS=$'\n'
for line in $(cat network.txt)
do
  ip=$(echo $line | cut -f1 -d' , ')
  mac=$(echo $line | cut -f2 -d' , ')
  echo add action=drop chain=forward src-address=$ip src-mac-address=!$mac
done

 

Link to post
Share on other sites
2 minutes ago, Meic said:

IFS=$'\n'
for line in $(cat network.txt)
do
  ip=$(echo $line | cut -f1 -d' , ')
  mac=$(echo $line | cut -f2 -d' , ')
  echo add action=drop chain=forward src-address=$ip src-mac-address=!$mac
done

 

That's bash


PLEASE QUOTE ME IF YOU ARE REPLYING TO ME
LinusWare Dev | NotCPUCores Dev

Desktop Build: Ryzen 7 1800X @ 4.0GHz, AsRock Fatal1ty X370 Professional Gaming, 32GB Corsair DDR4 @ 3000MHz, RX480 8GB OC, Benq XL2730 1440p 144Hz FS

Retro Build: Intel Pentium III @ 500 MHz, Dell Optiplex G1 Full AT Tower, 768MB SDRAM @ 133MHz, Integrated Graphics, Generic 1024x768 60Hz Monitor


 

Link to post
Share on other sites
Just now, Meic said:

IFS=$'\n'
for line in $(cat network.txt)
do
  ip=$(echo $line | cut -f1 -d' , ')
  mac=$(echo $line | cut -f2 -d' , ')
  echo add action=drop chain=forward src-address=$ip src-mac-address=!$mac
done

 

OP is asking for a batch file, meaning they have windows meaning they probably don't have bash :) 


Dutch Talk Thread

Unofficial LMG Social Media Accounts Website

Desktop Rig:

CPU: i7-4790K Cooler: Cooler Master Hyper 212 EVO Motherboard: MSI Z97 U3+ RAM: 4x4 GB DDR3 1600MHz GPU: MSI GTX 1070 Ti 8G Case: Corsair 230T Windowed Orange SSD: Crucial BX100 250 Gb PSU: Cooler Master G450M HDD: WD 1 TB Generic

Laptops: Macbook Air 2012 11" baseline (i5-3317U @ 1.7 Ghz) upgraded with a Samsung 850 evo 250 GB SSD

HP Zbook Studio G5 (i7-8750H, 16 GB DDR4 2666MHz, Samsung PM981 512 GB, Nvidia Quadro P1000)

Link to post
Share on other sites
3 minutes ago, Ralphred said:

sed 's/\([0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\) , /add action\=drop chain\=forward src-address\=\1 src-mac-address\=\!/' Network.txt

That should work for any valid IP/MAC pair, if you keep the same syntax.

Belt and braces would be to regex the MAC too, and do some input sanitation (filter out empty line comments etc)

Could just as easily be done in perl, tho that seems a bit overkill for simple search/replace regex.

 

Powershell does have some match capability, but it's criminally under documented.

sed doesn't exist in Windows


PLEASE QUOTE ME IF YOU ARE REPLYING TO ME
LinusWare Dev | NotCPUCores Dev

Desktop Build: Ryzen 7 1800X @ 4.0GHz, AsRock Fatal1ty X370 Professional Gaming, 32GB Corsair DDR4 @ 3000MHz, RX480 8GB OC, Benq XL2730 1440p 144Hz FS

Retro Build: Intel Pentium III @ 500 MHz, Dell Optiplex G1 Full AT Tower, 768MB SDRAM @ 133MHz, Integrated Graphics, Generic 1024x768 60Hz Monitor


 

Link to post
Share on other sites
Posted · Original PosterOP
3 minutes ago, Ralphred said:

sed 's/\([0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\.[0-2]\?[0-9]\?[0-9]\) , /add action\=drop chain\=forward src-address\=\1 src-mac-address\=\!/' Network.txt

That should work for any valid IP/MAC pair, if you keep the same syntax.

Belt and braces would be to regex the MAC too, and do some input sanitation (filter out empty line comments etc)

Could just as easily be done in perl, tho that seems a bit overkill for simple search/replace regex.

 

Powershell does have some match capability, but it's criminally under documented.

that honestly looks super weird, is it suppose to be like this in one line ? also running this script using powershell will work or not ?

Link to post
Share on other sites

Is there a specific reason that you can't use a programming language?

OS automation languages are not meant to process data, they are meant to conduct repetitious maintenance tasks for you.


"Ultimately, saying that you don’t care about privacy because you have nothing to hide is no different from saying you don’t care about freedom of speech because you have nothing to say." ~Verax

Link to post
Share on other sites
Posted · Original PosterOP
3 minutes ago, straight_stewie said:

Is there a specific reason that you can't use a programming language?

OS automation languages are not meant to process data, they are meant to conduct repetitious maintenance tasks for you.

well i suppse there is not, but i preffer if i didint had to download anything to my computer, and place where i would have to show the code i dont have the permission to install any  programs so powershell/batch is my goto for now (also class is for scripting specificly witch is why i'm upset i cant just use c++ since i know how to do that)

Link to post
Share on other sites

VBScript has been installed by default in every desktop release of Microsoft Windows since Windows 98 and will be much better for this than batch.

Set WSHFso = CreateObject("Scripting.FileSystemObject") 'Import File Handling

Set Output = WSHFso.CreateTextFile("C:\File Location\Output.txt", false, false) 'Define Output File

Set NetFile = WSHFso.OpenTextFile("C:\File Location\Network.txt", 1, False, 0) 'Define Input File
Do While NetFile.AtEndOfStream <> True 'Do Until End of File
	Line = NetFile.ReadLine 'Read Line
	LSplit = Split(Line, " , ", -1) 'Split using " , "
	ToWrite = "add action=drop chain=forward src-address=" & LSplit(0) & " src-mac-address=!" & LSplit(1) 'Combine Strings
	Output.WriteLine(ToWrite) 'Write Output
Loop
NetFile.Close 'Close Input File
Output.Close 'Close Output File


Tested working:

image.png.fe8068dc11a60a9752e78ec48b4ff711.png

Edited by rcmaehl
Remove unneeded shell import, add comments, close input file too.

PLEASE QUOTE ME IF YOU ARE REPLYING TO ME
LinusWare Dev | NotCPUCores Dev

Desktop Build: Ryzen 7 1800X @ 4.0GHz, AsRock Fatal1ty X370 Professional Gaming, 32GB Corsair DDR4 @ 3000MHz, RX480 8GB OC, Benq XL2730 1440p 144Hz FS

Retro Build: Intel Pentium III @ 500 MHz, Dell Optiplex G1 Full AT Tower, 768MB SDRAM @ 133MHz, Integrated Graphics, Generic 1024x768 60Hz Monitor


 

Link to post
Share on other sites
Posted · Original PosterOP
13 minutes ago, rcmaehl said:

VBScript has been installed by default in every desktop release of Microsoft Windows since Windows 98 and will be much better for this than batch.


Set WSHFso = CreateObject("Scripting.FileSystemObject") 'Import File Handling

Set Output = WSHFso.CreateTextFile("C:\File Location\Output.txt", false, false) 'Define Output File

Set NetFile = WSHFso.OpenTextFile("C:\File Location\Network.txt", 1, False, 0) 'Define Input File
Do While NetFile.AtEndOfStream <> True 'Do Until End of File
	Line = NetFile.ReadLine 'Read Line
	LSplit = Split(Line, " , ", -1) 'Split using " , "
	ToWrite = "add action=drop chain=forward src-address=" & LSplit(0) & " src-mac-address=!" & LSplit(1) 'Combine Strings
	Output.WriteLine(ToWrite) 'Write Output
Loop
NetFile.Close 'Close Input File
Output.Close 'Close Output File


Tested working:

image.png.fe8068dc11a60a9752e78ec48b4ff711.png

oh wow i remember watching vbs files getting destroyed by watching how malware works and i just assumed windows devs just do their own thing with their own language, but i never knew i could just open n++ and just do some little scripts with it without needing to install anything with visual basic , also thanks for comments it does work
i did found some cheatsheet so so say for powershell commands and how to use them so i will still try to do it with powershell (not too optimistic) but this helps a lot

Link to post
Share on other sites
1 hour ago, mikat said:

OP is asking for a batch file, meaning they have windows meaning they probably don't have bash :) 

Good point... Didn't read OP properly... D'oh

Link to post
Share on other sites
7 hours ago, tautvydas said:

for a while iv been looking at batch script guides and well it seems like they are rarely explaining anything.

i have some experience in c++ and whole pages dev. group PHP, JavaScript ,HTML stuff like that and some SQL . but no matter what i just cant seem to grasp batch scripting.

 

i need to write a short script that would extract specific elements from .txt file and would form another file with those values in commands.

i'm going to share my though process on how i want to approach this and if anyone has ideas on how to realize it, or has any videos or links that would explain how to do it that would be nice.

okay so lets say i have  an  "Network.txt." file witch has lots of lines of text, example :


192.168.0.101 , AA-BB-CC-11-22-33
192.168.0.102 , BB-AA-CA-1A-2B-3C
192.168.0.103 , 7C-D4-A2-F1-5B-AA
...

and with some sort of file reading command i would start to read data from text file and using some sort of cycle put them into arrays like I[x] for ip's C[x] for comma and M[x] for MAC adresses , assuming that command that reads file separates entries by spaces ( or somehow reading up until comma and then by end of the line , only seen this done in php but honestly i never tried it nor i remeber it how)
then when i have to put that output text somewhere i would jsut turn the cycle and just get the text formed like that


add action=drop chain=forward src-address= I[x] src-mac-address=! M[x]

so it would look something like this:

add action=drop chain=forward src-address=192.168.0.101 src-mac-address=!AA:BB-CC-11-22-33
add action=drop chain=forward src-address=192.168.0.102 src-mac-address=!BB-AA-CA-1A-2B-3C
add action=drop chain=forward src-address=192.168.0.103 src-mac-address=!7C-D4-A2-F1-5B-AA

 

main reason i'm looking into batch for this is that you should be able to run this script using .bat file without needing any compiler right ?

 

<?php
$filename = 'c:/document.txt';
$text = 'add action=drop chain=forward src-address=%%IP%% src-mac-address=!%%MAC%%';

$content = file_get_contents($filename);
// linux vs windows new lines
$content = str_replace("\x0D\x0A","\x0A",$content);
$lines = explode("\x0A",$content);

foreach ($lines as $line) {
	if ($line!='') {
    	$parts = explode(',',$line);
    	echo str_replace(array('%%IP%%','%%MAC%%'),array(trim($parts[0]),trim($parts[1])),$line)."\n";
    }
}

?>

 

optionally you can use getops() to read parameters from command line, like  php.exe script.php --filename="c:/document.txt"  and you use getopts('','filename::');  or something to that effect.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Buy VPN

×