Need help for simle bash script - linux

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

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

Shell script not able to parse host list and host unknown

I have a script that I am trying to run. The purpose of this script is to run single or multiple commands on list of servers. When running it, the script is not able to parse the hosts file and gives an error:
ssh: Could not resolve host name nodename nor servname provided, or not known
while read host;
do
echo server: $host
sshpass -p 'password' ssh -o "StrictHostKeyChecking no" admin#$host 'command'
done < /path/to/hosts.txt
hosts.txt contains list of IP addresses of the hosts that need to connect in the following format:
server 1
server 2
server 3
I have tried putting the server IPs in "" and '' and ; and ,, but they all give the same error.
Can someone explain what I am doing wrong here?
Please check if $host variable is getting parsed properly. Also, in your hosts.txt, there are two entries in each row. What does it signify?
I have created a sample script to parse space-separated arguments
Input file: hosts.txt
Script output:
#!bin/sh
while IFS=' ', read hostname ipaddress;
do
echo server: $ipaddress
done < hosts.txt
#Cyrus and #Gordon Davisson thank you -n resolved the issue. such a simple fix i am surprised i didnt see that post here before despite of lot of searching. guess i wasnt wording it the right way. Thank all for the input.

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

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

How to create an effective WHOIS Script in Linux

Disclaimer: I'm a noob to linux and scripting, please don't heckle me.
GOAL: Feed my linux system a .txt file full of IP Addresses and Perform a WHOIS look on each IP and show me specific fields (grep) such as the Organization Field. Bonus points if someone can help me figure out how to use my API key to check my IP list against abuseipdb.com
I've created an ip.txt file with my IP Addresses and I've tried using the following syntax. I'm using Kali Linux to perform this, but it worked on my friend's Fedora system.
sudo for ip in $(cat ip.txt); do whois $ip | echo "$ip $(grep -e 'Organization' | grep -v 'Verizon')"; done > whois.txt
My output is: -bash: syntax error near unexpected token 'do'. Remove "do" from my command and then I receive -bash: syntax error near unexpected token 'done'
Remove the "done" and the pipe out and then it's just pissed..
Please help a linux n00b :)
You are attempting to invoke sudo on the command for ip in $(cat ip.txt), and then running the syntactically invalid commands do whois .... (Note that for ip in $(cat ip.txt) is also syntactically invalid, but sudo is never invoked so no shell ever tries to parse that string as a command). You need to narrow the scope of sudo. eg
while read ip; do sudo whois "$ip" | ... ; done > whois.txt < ip.txt

Adding new user to multiple unix servers using terminal

I am working within a company and require myself to be added onto different branch servers. The current way of doing this is:
sudo /usr/local/bin/sd-adduser test "Test User"
This needs to be done individually logging into each server manually - which is about 20 servers. I vaguely know of expect which allows you to do add a user to multiple servers? Could anyone point me in the right direction? Or provide me the script to do this.
Any help is appreciated.
Sounds like multi-ssh could help you or pssh or pdsh.
In the long run you probably want a central user management like LDAP.
Routine administration tasks such as this can be done using a script that reads a list of server names and runs a command. Something like this "each-host" script:
#!/bin/sh
for server in $(cat mylist)
do
ssh -t $server "$#"
done
where mylist is a file containing the list of servers.
Thus
each-host sudo /usr/local/bin/sd-adduser test "Test User"
would run the OP's command on each host. Once you get that working, you could tidy up a little, making it less verbose (not printing /etc/motd);
#!/bin/sh
for server in $(cat mylist)
do
echo "** $server"
ssh -q -t $server "$#"
done

Resources