I am new to Linux, and I am having an issue of not being able to fully execute these lines of code. I have researched for hours and have also spent plenty of time manipulating these myself and still cannot create something that looks how it should be. Thank you all ahead of time.
How it is supposed to look:
$. network.sh
Today is: Day, Month, Day (numerical), year
The hostname for localhost.localdomain is: 192.168.xxx.xxx
Here is how it currently looks.
. network.sh
Today is: Sunday, October 11, 2015
The hostname for localhost.localdomain
is: [kris#localhost Desktop]$
is is on the next line. This completely disregards my last line of command and does not display it on the same line as "the hostname for localhost.localdomain".
Current file...
#!/bin/bash
# Script to print the current date
echo -n "Today is: "
d=$(date +%A," "%B" "%d," "%Y)
echo "$d"
# Show IP in color
echo -n "The hostname for " ; hostname ; echo -n "is:" ; echo -n ip addr list eth1 |grep "inet " |cut -d' ' -f6|cut -d/ -f1
Just interpolate the sub processes outputs:
echo "The hostname for $(hostname) is: $(ip addr list eth1 |grep "inet " |cut -d' ' -f6|cut -d/ -f1)"
The nested " are fine (the $(subshell) parsing takes precedence)
You can use:
echo "The hostname for $(hostname) is: $(ip addr list eth1 | grep "inet " | cut -d' ' -f6 | cut -d/ -f1)"
Or, I suggest:
ip=$(ip addr list eth1 | grep "inet " | cut -d' ' -f6 | cut -d/ -f1)
echo "The hostname for $(hostname) is: $ip"
Related
I need some Specs and Data from Number of Latops in a ini-file.
When I try to get the IP-Adress with ip addr list <interface> nothing happens in the script. Not even a Error.
I try to quote stuff in my code. Nothing change.
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(ip -o -4 addr list $w2i | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
I Think maybe it is because of the Varibales but this fails also (interface is set 'manually'):
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(ip -o -4 addr list wlp3s0 | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
When i run the script i get wlp3s0 and i must close the script with ctl-c. When i run ip -o -4 addr list wlp3s0 | awk '{print $4}' | cut -d/ -f1 it gives me my ip like i expected.
EDIT
I just need to wirte the full-path of the ip-command. Can somebody explain why i have to do this? And especially it is not necessary for cut or awk that also based in /usr/bin/?
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(/usr/bin/ip -o -4 addr list "$w2i" | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
}
So I've created this ping sweep in a bash terminal, but I want to make a neat looking script file for this:
for IPs in 192.168.0.{1..254}; do ping -c1 -W1 $IPs; done | grep -B1 "1 received" | grep "192.168.0" | cut -d " " -f2 > BashPingSweep.txt
I think I have the for loop correct, but I cant pipe the for loop into the other greps and cut then output. This is what I have now:
#!/bin/bash
for IPs in 192.168.0.{1..254}
do
ping -c1 -W1 $IPs
done
grep -B1 "1 received"
grep "192.168.0"
cut -d " " -f2
> BashPingSweep.txt
You could try this:
#!/bin/bash
for IP in 192.168.0.{1..254}
do
ping -c1 -W1 $IP
done |
grep -B1 "1 received" |
grep "192.168.0" |
cut -d " " -f2 \
> BashPingSweep.txt
It looks a bit awkward but it's a common way to format a lengthy pipe over multiple lines. You could also put it like this:
for IP in 192.168.0.{1..254}
do
ping -c1 -W1 $IP
done \
| grep -B1 "1 received" \
| grep "192.168.0" \
| cut -d " " -f2 \
> BashPingSweep.txt
which is what I prefer because it's easier to see where the pipe goes.
You need to pipe it inside the for loop
for IPs in 192.168.0.{1..254};
do
ping -c1 -W1 $IPs | grep -B1 "1 .* received" | grep "192.168.0" | cut -d " " -f2 > BashPingSweep.txt
done
I have a pattern like below in a file foo.txt
|eth0:9:8|eth1:7:0|
In a loop i have a variable with values eth0, eth1 and so on individually in Each iteration.
Based on the value of the variable, Say eth0 i need the values 9 and 8
In case of eth1 i need values 7 and 0
Something like this should work. The values you need are stored in var1 an var2 here.
TO_PARSE="|eth0:9:8|eth1:7:0|"
declare -a eths=(eth0 eth1)
for eth in "${eths[#]}"; do
parsed=$(echo "${TO_PARSE}" | \
awk 'BEGIN{FS=":"; RS="|"}{print $1 " " $2 " " $3}' | \
grep "${eth}")
var1=$(echo "${parsed}" | cut -f2 -d' ')
var2=$(echo "${parsed}" | cut -f3 -d' ')
echo "eth=${eth} var1=${var1} var2=${var2}"
done
I am making a script that will let you choose between which interface you want to use.
I need a way to get the interfaces and store each of them in a variable.
Here is my code, but it only gets the interfaces:
Interfaces=$(ifconfig | awk '{print $1}' | grep ':' | tr -d ':')
You need to only check the lines that contain the interface name, not the lines with details. In ifconfig, detail lines start with a space; in ip, interface lines start with a number.
In bash, you can use select to create a simple menu:
#! /bin/bash
select interface in $(ip link show | grep '^[0-9]' | cut -f2 -d:) ; do
if [[ $interface ]] ; then
echo You selected $interface
break
fi
done
or
select interface in $(ifconfig -a | grep -v '^ ' | cut -f1 -d' ') ; do
if [[ $interface ]] ; then
echo You selected $interface
break
fi
done
How do I create a script that will identify all live hosts (responding to ping) using bin/bash in linux? My thoughts are to first have a
fping -A "some URL" // to get the IP address
then to set that to a var. then run a
fping -g "var" // having the ip address inserted by using a var.
Is there an easier way? If so, what would that script look like?
Not sure what you want to do and why you use fping, but if you just need the IP for a host you should use somthing like this:
getent ahostsv4 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1
getent ahostsv6 www.google.de | grep STREAM | head -n 1 | cut -d ' ' -f 1
getent hosts google.de | head -n 1 | cut -d ' ' -f 1
All commands will resolve an IP address if host still exist. If host points to CNAME it will also get the IP in that case.
Lets say, you have a text file containing all your hosts to test:
like: test-hosts.txt
www.server1.dom
www.server2.dom
server3.dom
1.3.5.7
then the command line (contains bash-script) will do it:
cat test-hosts.txt | xargs -i\{\} bash -c 'HOST="{}"; IP=$(getent ahostsv4 "{}" | grep STREAM | head -n 1 | cut -d " " -f 1); ping -c 1 -w 5 "$IP" >/dev/null 2>&1; RESULT="$?"; echo -e "Host: $HOST ($IP) \c"; case "$RESULT" in 0) echo "is online";; 1) echo "not responding after 5 secs";; *) if [[ "$IP" == "" ]]; then echo "has no resolveable address"; else echo "not availabe due to error [$RESULT]"; fi;; esac;'
and provide outputs like:
is online
not responding
no resolveable address
not available due error [code]