writing a basic script that pings the current network [duplicate] - linux

This question already has answers here:
Send a ping to each IP on a subnet
(14 answers)
Closed 3 years ago.
I'm having trouble writing a simple script that pings its current network.
I want the output to look the same as a normal ping but I'm having trouble getting to that point
here's the original question from the assignment
-write a short script(using the ping command)to do a "ping" scan of your current network.
any help you can give is much appreciated
I originally tried printf but I wasn't able to even get a decent output so I gave up and started using echo
This is what I have right now
#!/bin/bash
for ping in $(ping -c 4 -v 192.168.1.10)
do echo $ping
done
my teacher said that I would run into two problems with the simple ping script and that I could find the answer in the ping man pages. however I can't seem to find anything to help.
I basically want the script to run as if I just ran a normal ping command and have it display output in the same way.

According to the question from your assignment, I would say what you need to do is invoking ping in all the IP's in your current network (192.168.1.0 in case it is a /24 mask).
What you are trying to do is a simple ping to 192.168.1.10.
If that is the case, why don't you just call ping command in your script?
#!/bin/bash
ping -c 4 -v 192.168.1.10
I think what your teacher really wants is you to find the way of making a loop over all the IP directions in your network.

#!/bin/bash
ping= ping -c 4 -v 192.168.1.10
print $ping
simply get the out put

Related

Ping results are not saved in a file [duplicate]

This question already has answers here:
Bash loop only read the last line
(6 answers)
Closed 18 days ago.
I am running a simple shell script which initiates the ping and save the result to a txt file.
ips=$(cat host.txt)
for ip in $ips
do
ping -c 2 $ip > pingtest.txt
done
However for some reason, there is no output in the text file,I am sure the ip is pinging(I have confirmed with TCP DUMP).
Can someone please help me?
hostfile output is:
10.0.0.10
10.0.0.11
172.28.209.43
172.16.84.131
Don't overwrite the file on every iteration. Do something like this.
ips=$(cat host.txt)
for ip in $ips
do
ping -c 2 $ip
done > pingtest.txt
Also as a suggestion, use consistent indentation and avoid word splitting when filename expansion a.k.a globbing is enabled. For example you can use a while read loop instead of a for.
while IFS="" read -r ip
do
ping -c 2 "$ip"
done <host.txt > pingtest.txt

How to send a character through telnet from Linux shell? [duplicate]

This question already has answers here:
Linux tool to send raw data to a TCP server
(3 answers)
Closed 2 years ago.
I manage a running program by sending a command-character through telnet. I usually do it step by step:
telnet localhost 12345
command-character
ctrl+]
quit
Is it possible that I send the command-character from bash directly, or if not, write a bash script to do that?
Do you need to use telnet? I would use the netcat (nc)
Anyway the related thread:
https://unix.stackexchange.com/a/160597
so for example:
echo 'c' | nc localhost 12345
for 'c' use any other character

Need help for simle bash script

Can someone help me with the syntax for a simple bash script that trying to write:
echo ping -c 1
echo nslookup
Basically I want to receive output of one line from the ping and the nslookup information for a domain that I'm checking. Unfortunately I'm unable to get this correctly.
P.s. this is basically the first thing that I'm trying to accomplish in bash.
Thank you in advance!
Thank you for the provided information on the matter. I felt little ashamed from the nature of my question so I spent little more time to read. The solution that I found is the following:
#!/bin/bash
for i in $*;do
ping -c 1 $i &
nslookup $i &
done
#
'
Once I added the scrit to the /bin folder I used the commands:
chmod +x "script name"
dos2unix ""scrit name"
so not I'm able to use it only by typing the name of the script.
hi after writing and saving script and giving permission to the script
just go the folder and
./
of else you just can use
sh
Hi i would be better if you elaborate your problem, if you want one line output for ping command filter using head
eg
ping www.google.com | head -n 1
or if you are thinking of sending one packet of data to the server
ping -n 1 www.google.com
nslookup www.google.com
and if your are writing Bash Script
#!/bin/bash
ping -n 1 www.google.com
nslookup www.google.com
save the file
give execute permission and run
Please let me know i answered your question

Detect IP-Address change on an interface

I would like to trigger a service when a change of an ip address on a specific interface occurs. Is there a target for this or some other method I am not aware of to achieve this using systemd on Linux (Kernel 3.19)?
The service would be used to send a SIGNAL to a defined process. The Linux is running on an embedded system.
Thanks!
Because you use Systemd you might already use systemd-networkd for managing your devices instead of relying on 3rd party code.
You could use the structured journal output to get the last 2 ADDRESS field of the current BOOD_ID.(sadly, there is no notification mechanism for address changes in systemd-networkd):
→ sudo journalctl -F ADDRESS -u systemd-networkd -n 2
192.168.178.29
So, if there is only one line output, there was no address change.
There is an solution in other question of StackOverflow. Just here:
Detecting a change of IP address in Linux
I like this code, it's easy, you onli need a cron job with frecuency as you need (I made a little change):
#!/bin/bash
OLD_IP=`cat ip.txt`
NEW_IP=`/sbin/ifconfig | awk -F "[: ]+'{ print $4}'`
if [ $NEW_IP != OLD_IP ]; then
YOU_COMMAND <commands>
echo $NEW_IP > ip.txt
fi
exit 0

Ping with a variable in linux script

I want that my script pings the ip-addresses
192.168.0.45
192.168.0.17
192.168.0.108
by doing this:
bash Script.sh 45 17 108
I want to give the last numbers with bash to ping this ip-addresses.
I don't know how I have to do this. Do I have to work with a 'case' in a do while or something else??
#!/bin/bash
for i in $*; do
ping 192.168.0.$i
done
I want to give the last numbers with bash to ping this ip-addresses.
I presume, you want to ping the addresses simultaneously. In that case you can do this:
Script.sh:
#!/bin/bash
ping 192.168.0.$1 & ping 192.168.0.$2 & ping 192.168.0.$3 &
This will send all of the three ping commands to background where they will be executed simultaneously and print continuous output on terminal.
You can do this with a for loop too:
#!/bin/bash
for i in $*;do
ping 192.168.0.$i &
done
The for loop method can take any number of arguments

Resources