shell script to ips from hosts file using hostname - linux

I have a list of hostnames and i would like to get the ips from the hosts file. However, when I run the script I wrote I only get the ip of the final hostname.
Can anybody spot the obvious error I cannot? Thanks.
!/bin/ksh
for hostname in `sort /home/steveo/out_stream.txt`
do
#extract hostnames from hosts file
ip=`grep -i $hostname /etc/hosts | awk '{print $1}'`
echo ${hostname} ${ip} >>ip_hostname_list.txt
done
echo "press RETURN \c";read dummy;clear
$ more ip_hostname_list.txt
arq852mux1_8k
arq852mux2_8k
arq854mux1_8k
arq854mux2_8k
arq898mux1_8k
arq898mux2_8k
arq902mux1_8k
arq902mux2_8k 172.27.228.70

Related

check the dns record for a hostname with nslookup

I have a host list hostfile that i'm doing name lookup just to know if the host has records in DNS or not with the help of Linux native tool nslookup.
Below is what i'm doing and its working, however there is a situation when nslookup don't find anything thus the result is nothing in that case i want to fill "no dns record"
What i'm trying:
$ for i in `cat hostfile`;
> do
> echo $i $result
> result=$(nslookup $i | awk 'FNR==6{print $2}')
> done
OR
for i in `cat hostfile`;
do
printf "$i: %s\\n" $(nslookup $i | awk 'FNR==6{print $2}');
done
Its not giving correct output if there is no nslookup found.
Desired:
myhosts01 192.168.1.1
myhosts02 192.168.1.2
myhosts03 192.168.1.3
myhosts04 192.168.1.4
myhosts03 no dns record
myhosts04 no dns record
You can either use a full if statement to check whether the result is blank (-z for zero-length):
if [[ -z "$result" ]]; then
result="no dns record"
fi
echo "$i $result"
Or use a default value when expanding the variable:
echo "$i ${result:-no dns record}"
In either case, I'd recommend using a while read loop to read the file instead of for (assuming there's one hostname per line), using dig +short instead trying to parse nslookup, doing the lookup before printing the result, and double-quoting all variable references (and probably using more descriptive variable names). Something like this:
while read -r hostname; do
hostIP=$(dig +short "$hostname")
echo "$hostname ${hostIP:-no dns record}"
done <hostfile

Execute a remote script on a local server in Linux using ssh

I have a script called "test" on a remote server's home folder that contains the following lines:
#!/bin/bash
tengbe=`ifconfig | grep -B1 192.168 | awk 'NR==1 { print $1 }' | sed 's/://g'`
Basically, it just stores the name of the interface of the local server into a variable once the script is called. When I ssh into the remote server to call the script, I get an error:
ssh remoteserver-IP './test'
./test: line 3: ifconfig: command not found
What might be wrong with my script? I have seen various answers that does not offer a solution to my issue.
Try:
$ ssh remotehost ifconfig
bash: ifconfig: command not found
ifconfig isn't in your PATH on the remote host. You can prove it with:
$ which ifconfig
/sbin/ifconfig
$ ssh remotehost 'echo $PATH'
(returns lots of dirs, none of which is /sbin)
To get around this either specify the full path to ifconfig:
$ ssh remotehost /sbin/ifconfig
... or configure $PATH before calling it:
$ ssh remotehost 'PATH=$PATH:/sbin ifconfig'
... or edit $HOME/.bashrc (or alternatives -- read about your shell's initialisation process) to add /sbin to $PATH all the time.
For security, in a script it's usually better to specify absolute paths, maybe via variables. So in your script:
#!/bin/bash
IFCONFIG=/sbin/ifconfig
tengbe=$(${IFCONFIG} | grep -B1 192.168 | awk 'NR==1 { print $1 }' | sed 's/://g')
Note, I've replaced your backticks with $() - this isn't required, but it's a good habit to adopt - What is the benefit of using $() instead of backticks in shell scripts?

Shell script to find the IP address of an Virtual machine created by using KVM/virsh command

I am looking for the shell script to find the IP address of a virtual machine created by using KVM/VIRSH.
I used the following steps to get it so, but couldn´t able to find it.
Ping the IP addresses in range
2.Use Virsh list command to list all the active VM
3.use Virsh dumpxml domainname and project the xml of vm
use grep command and fetch the Hardware address of vm
5.Display the hardware address of each vm
Now I would like to add one more step like fetching the IP address for that particular Hardware address using ¨arp -ne¨
I couldn´t able to figure out how to add the part.
could any one help me on this.
for i in {1..150}
do
ping -c 1 -n -q -r -t 1 -s 1 -W 1 192.168.1.$i > /dev/null &
done
for name in `virsh list | grep running | awk '{ print $2 }'`
do
# printf "\n$name\n "
arp -e | grep "`virsh dumpxml $name | grep "mac address"|sed "s/.*'\(.*\)'.*/\1/g"`" |
awk '{ printf $1 ; printf " " ; printf $3 "\n" }'
done
current output:
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41
vm3 52:54:00:0e:4c:42
The output I am expecting is
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41 192.*.*.*
vm3 52:54:00:0e:4c:42 192.*.*.*
Use nmap to do network discovery instead of ping. It can do what ping does but also much more, plus it runs way faster and takes care of the network-scope scanning that you're doing in your for loop.
$ nmap -T5 -n -PE 192.168.4.0/24 > /dev/null
$ ip neigh show | grep 192.168.4 | grep -v FAILED
192.168.4.92 dev eth0 lladdr 54:52:00:90:90:92 REACHABLE
192.168.4.11 dev eth0 lladdr fa:16:3e:fa:ac:07 REACHABLE
192.168.4.91 dev eth0 lladdr 54:52:00:90:90:91 REACHABLE
192.168.4.90 dev eth0 lladdr 54:52:00:90:90:90 REACHABLE

How to find a list of ip addresses in another file

I was given the task to see if we are advertising a list of ip addresses(3000). Not a good idea to do it manually, so I copied all the ip addresses that we are advertising in a file. Now I just have to create a bash script and feed the list of ip address into the script so it can find the ip addresses in the file with the list of ip addresses being advertised. If found save it in one file if not in a different file. This is what I have so far. The problem with this script is that I have to type in manually every ip address. HOW CAN I FEED THE FILE WITH THE LIST OF IP ADDRESSES TO BE SEARCHED IN THE FILE WITH THE LIST OF IP ADDRESSES WE ARE ADVERTISING. Thank you very much in advanced.
#!/bin/bash
while true; do
echo -e "IP address: \c"
read ip
if grep --color "$ip" "ips"; then
echo $ip "was found"
echo $ip >> found
else
echo $ip "was NOT found"
echo $ip >> notFound
fi
done
If you sort the two files, you can use the comm command:
sort all_ip_addresses > all_ip_addresses_sorted
sort adverted_ip_address > advertised_ip_address_unsorted
comm -23 all_ip_addresses_sorted advertised_ip_addresses_sorted
will show the IP addresses that are not advertised, and:
comm -12 all_ip_addresses_sorted advertised_ip_addresses_sorted
will show the advertised IP addresses.
You can also avoid creating the separate sorted files by using process substitution:
comm -23 <(sort all_ip_addresses) <(sort advertised_ip_addresses)
A better script :
while read ip
do
grep "$ip" "$ips" > /dev/null 2>&1 && echo "$ip" >> ip.found || echo "$ip" >> ip.notfound
done
Name the script "searchip.sh"
Assume your input file is "iplist" ,set up variable and call like this:
ips=ips
cat iplist | sh searchip.sh
or
sh searchip.sh < iplist
Then you get two files , one is ip found, other one is ip not found.
What you need is shell I/O redirection.
$ script < list_of_ip_addresses
That's all you need.

Linux bash script that pings multiple IP addresses from a file

I have a file containing multiple hosts and IPs in the format above:
alpha, 192.168.1.1
beta, 192.168.1.2
gamma, 192.168.1.3
I am trying to create a script that says something like:
"Pinging hostname alpha"
ping 192.168.1.1
and jump to the next ip in the list.
I don't want the entire script, just some suggestions.
Thanks,
Alex
If you add a comma to the input field separator, it'll help parse the lines:
IFS=$IFS,
while read name ip; do
echo -n "Pinging hostname $name..."
ping -c2 "$ip" &>/dev/null && echo success || echo fail
done < /tmp/hosts
I'd read in the lines with read. You'll probably also want to give ping an option telling it how many times to ping. The default on most Linux systems for example is to ping forever, which doesn't seem like it would work well in your situation.
You could use AWK:
$ awk '{print "Pinging hostname "$1; system("ping -c 3 "$2) }' ips
Pinging hostname alpha,
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
You can also remove that comma if is it important to you:
$ awk '{sub(/,/,"");print "Pinging hostname "$1; system("ping -c 3 "$2) }' ips
Pinging hostname alpha
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
I might be a bit late to the party, but how about fping? Use -f to read from a file (requires sudo), or pipe the file with < (as suggested on the man page). It won't tell you "pinging alpha", but it will quickly tell you whether or not you can get in touch with the hosts.
Script for hosting 100+ hosts in same scheme like 192.168.xx.xxx
#!/bin/bash
for i in `seq ${2} ${3}`
do
ping -c 1 ${1}.${i} > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "${1}.${i} responded."
else
echo "${1}.${i} did not respond."
fi
done
command to ping the host
bash test.sh 192.168.1 0 100
Try this
#!/bin/bash
IPLIST="path_to_the_Ip_list_file"
for ip in $(cat $IPLIST)
do
ping $ip -c 1 -t 1 &> /dev/null
if [ $? -ne 0 ]; then
echo $ip ping faild;
else
echo $ip ping passed;
fi
done

Resources