OSCP
  • All About OSCP
  • OSCP- One Page Repository
  • About the Author
  • Basic Linux & Windows Commands
    • Linux Commands
    • Windows
      • cmd
      • Powershell
      • Basics of windows
    • Linux / WindowsMain commands
    • Bash Scripting
  • Recon (Scanning & Enumeration)
    • Active Info Gathering
      • My Network Recon Checklist
      • My Web Recon Checklist
      • Network Enumeration
      • Port Scanning
    • Common Ports and Services
      • Other Services Enumeration
    • DNS Zone Transfer Attack
    • SNMP Enumeration
    • SMB Enumeration
    • Web Application Directory bruteforcing / fingerprinting
    • Port & Services Scanning
  • Web Application
    • My checklist
      • LFI
      • RFI
      • SQLI
    • File Upload bypass
    • Enumeration and Exploitation
    • No-Sql Injection
    • SQL Injection
    • Hidden Files and directories
    • RFI
    • LFI
  • Brute Force
    • Reuse the hash
    • Password Crack
  • Shells
    • Linux Reverse Shell [One liner]
    • Reverse Shell to fully interactive
    • Reverse Shell Cheat Sheet
    • WebShell
  • Transferring files
    • My Checklist
    • Transfer files on linux
  • Priv Escalation
    • Linux Priv Escalation
      • g0tmi1k linux privilege escalation
      • Privilege Escalation - Linux
      • Checklist - Linux Privilege Escalation
    • Windows Priv Escalation
      • Fuzzysecurity window priv escalation
      • Privilege Escalation - Windows
      • Checklist - Local Windows Privilege Escalation
  • Post Exploitation
    • Cover your tracks
    • Persistence
    • Loot Linux
    • Loot Windows
    • Escaping Restricted Shell
    • Meterpreter shell for post-exploitation
    • Spawn Shell
  • Pivoting
    • My Checklist for Pivoting
    • Tunneling and Port Forwarding
    • Pivotind understanding
  • Buffer Overflow
    • Buffer overflow
    • Buffer overflow Step by Step
      • Study about buffer overflow
      • Brainpan
      • VulnServer
      • Minishare
  • Main Tools
  • MISC
    • Exploit Compiling
  • CheatSheet (Short)
  • OSCP/ Vulnhub Practice learning
    • Machines Practice
    • My Practice on HTB Windows boxes
    • My Practice on Vulnhub boxes
    • Over the Wire (Natas)
    • Over The wire (Bandit)
Powered by GitBook
On this page
  • Iterate over a file
  • For-loops
  • If statement
  • If/Else
  • Command line arguments
  • Daemonize an execution
  • Use the output of command

Was this helpful?

  1. Basic Linux & Windows Commands

Bash Scripting

https://sushant747.gitbooks.io/total-oscp-guide/basics_of_linux.html

Iterate over a file

This script will iterate over a file and echo out every single line:

#!/bin/bash

for line in $(cat file.txt);do
    echo $line
done

Another way of writing is this:

#!/bin/bash

while read p; do
    echo  $p
done <file.txt

For-loops

#!/bin/bash

for ((i = 0; i < 10; i++)); do
    echo $i
done

Another way to write this is by using the program seq. Seq is pretty much like range() in python. So it can be used like this:

#!/bin/bash

for x in `seq 1 100`; do
    echo $x
done

If statement

$1 here represent the first argument.


if [ "$1" == "" ]; then
    echo "This happens"
fi

If/Else

#!/bin/bash

if [ "$1" == "" ]; then
    echo "This happens"
else
    echo "Something else happens"
fi

Command line arguments

Command line arguments are represented like this

#!/bin/bash

$1

This is the first command line argument.

Daemonize an execution

If you do a ping-sweep with host the command will take about a second to complete. And if you run that against 255 hosts I will take a long time to complete. To avoid this we can just deamonize every execution to make it faster. We use the & to daemonize it.

#!/bin/bash

for ip in $(cat ips.txt); do
    ping -c 1 $ip &
done

Use the output of command

It has happened to me several times that I want to input the output of a command into a new command, for example:

I search for a file, find three, and take the last line, which is a path. Now I want to cat that path:

#!/bin/bash

locate 646.c | tail -n 1

This can be done like this:

#!/bin/bash

cat $(locate 646.c | tail -n 1)
PreviousLinux / WindowsMain commandsNextRecon (Scanning & Enumeration)

Last updated 5 years ago

Was this helpful?