Check if a Samba share exists in local network - linux

i'm trying to figure out how to make a script for check if a dir exist in the network and than backup a directory in the network. Unfortunatly the IP of this machine (Windows 7 with samba) is not static and everytime is changing.
at the moment what I do for backup my notebook is:
in W7 notebook, launch cmq and with ipconfig check the ip of the machine
in linux, go in /etc/hosts file and change the IP set for the notebook
launch rsnapshot manually or wait for the crontab to do it
now what I want to do is a script that does:
to check which IP is available in a range of IPs
check which one has a specific dir available
if both tests are ok, I change the hosts name with the IP and I lanunch the rsnapshot.
maybe a smartest way could be to check if the MAC Address is available in the network and which IP is associated to it and launch the rssnapshot after have changed the hosts or the rsnapshots.conf file
till now what I did is:
for ip in 192.168.1.{128..142}; do # for loop and the {} operator
ping -c 1 -t 1 $ip > /dev/null 2> /dev/null # ping and discard output
if [ $? -eq 0 ]; then # check the exit code
if [check if dir exists for ip $ip] #not sure how do this check
/usr/bin/rsnapshot daily
else
echo "${ip} is down"
fi
done

Use smbclient to list (-L) the shares ex. :
smbclient -L SERVERNAME | grep -i "YOURSHARENAME"

Related

Transfer variables in ssh an get answer back

I want to write script in bash to connect to server, ping on it another server, get ip from ping command, and send that info back to pc who ran the script, and at the end connect to server who is pinged before.
My script:
#!/bin/bash
echo "Script to connect to server"
#ip_p used to connect to first server, on that server i want to use ping command'
ip_p=XYz.XYZ.XYZ.XYZ
user='username to the servers'
#ip_d - in that variable i want to save ip of the pinged server
ssh -t $user#$ip_p ip_d="ip_d=ping -c1 $1.domain | sed -nE 's/^PING[^(]+\(([^)]+)\).*/\1/p' && exit "
echo "start"
echo $ip_d
echo "stop"
ssh -t $user#$ip_d
How i wish to work:
domain i want to check test.nyslay.pl
connect to server which ip, and username was defined in script
ping server(part ".nyslay.pl", is always the same, but "test" i want to read from first argument of script run
get ip of domain from previous point
transfer ip from point: 2 to local machine, on which script is run
connect to the server which ip we get from point: 2
Use command substitution:
ip_d=$(ssh -t $user#$ip_p "ping -c1 $1.domain | sed -nE 's/^PING[^(]+\(([^)]+)\).*/\1/p'")
The output of ping (through sed) comes to the local machine via ssh, whose output is captured in the local variable ip_d.

Wifi disconnected before init.d script is run

I've set up a simple init.d script "S3logrotate" to run on shutdown. The "S3logrotate" script works fine when run manually from command line but the script does not function correctly on shut down.
The script uploads logs from my PC to an Amazon S3 bucket and requires wifi to run correctly.
Debugging proved that the script is actually run but the upload process fails.
I found that the problem seems to be that the script seems to run after wifi is terminated.
These are the blocks I used to test my internet connection in the script.
if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
echo "IPv4 is up" >> *x.txt*
else
echo "IPv4 is down" >> *x.txt*
fi
if ping -q -c 1 -W 1 google.com >/dev/null; then
echo "The network is up" >> *x.txt*
else
echo "The network is down" >> *x.txt*
fi
The output for this block is:
IPv4 is down
The network is down
Is there any way to set the priority of an init.d script? As in, can I make my script run before the network connection is terminated? If not, is there any alternative to init.d?
I use Ubuntu 16.04 and have dual booted with Windows 10 if that's significant.
Thanks,
sganesan7
You should place you scrip in:
/etc/NetworkManager/dispatcher.d/pre-down.d
change group and owner to root
chown root:root S3logrotate
and it should work. If you need to do this for separate interface place script in
create a script inside
/etc/NetworkManager/dispatcher.d/
and name it (for example):
wlan0-down
and should work too.

Using SSH to grep keywords from multiple servers

I am completely new to scripting and am having some trouble piecing this together from some other online resources.
What I want to do is run a bash script that will grep for a keyword domain in the /etc/hosts file on multiple servers. In the output file, I am looking for a list of the servers that contain this keyword but am not looking to make any changes. Simply looking for which machines have this value. Since there are a bunch of machines in question, listing the servers I am looking to search for won't work, but the machine I am doing this from does have SSH keys for all of the ones in question.
I have a listing of the servers I want to query in three files on the machine (one for each environment) I am going to run this script from.
Linux.prod.dat
Linux.qa.dat
Linux.dev.dat
Each file is simply a list of server names in the environment. For example..
server1
server2
server3
etc...
I am totally lost here and would appreciate any help.
Here is an example:
KEYWORD=foo
SERVERLIST=Linux.prod.dat
OUTPUTLIST=output.dat
for host in $(cat ${SERVERLIST}); do
if [[ -n "$(ssh ${host} grep '${KEYWORD}' /etc/hosts && echo Y)" ]]; then
echo ${host} >> ${OUTPUTLIST}
fi
done
Try GNU parallel
parallel --tag ssh {} grep -l "KEYWORD" /etc/hosts :::: Linux.prod.dat
parallel run command multiple times substituting{}with lines from Linux.prod.dat file.
--tag switch adds value from the Linux.prod.dat on the beginning of the file. So, the output of the command will look like:
server1 /etc/hosts
server5 /etc/hosts
server7 /etc/hosts
Where server1, server5, etc. will be names of the servers where /etc/hosts contains KEYWORD

LDAP - SSH script across multiple VM's

So I'm ssh'ing into a router that has several VM's. It is setup using LDAP so that each VM has the same files, settings, etc. However they have different cores allocated, different libraries and packages installed. Instead of logging into each VM individually and running the command, I want to automate it by putting the script in .bashrc.
So what I have so far:
export LD_LIBRARY_PATH=/lhome/username
# .so files are in ~/ to avoid permission denied problems
output=$(cat /proc/cpuinfo | grep "^cpu cores" | uniq | tail -c 2)
current=server_name
if [[ `hostname-s` != $current ]]; then
ssh $current
fi
/path/to/program --hostname $(echo $(hostname -s)) --threads $((output*2))
Each VM, upon logging in, will execute this script, so I have to check if the current VM has the hostname to avoid an SSH loop. The idea is to run the program, then exit back out to the origin to resume the script. The problem is of course that the process will die upon logging out.
It's been suggested to me to use TMUX on an array of the hostnames, but I would have no idea on how to approach this.
You could install clusterSSH, set up a list of hostnames, and execute things from the terminal windows opened. You may use screen/tmux/nohup to allow processes started to keep running, even after logout.
Yet, if you still want to play around with scripting, you may install tmux, and use:
while read host; do
scp "script_to_run_remotely" ${host}:~/
ssh ${host} tmux new-session -d '~/script_to_run_remotely'\; detach
done < hostlist
Note: hostlist should be a list of hostnames, one per line.

Changing network ip in a bash script started from a NFS-mounted folder

I wrote a simple Bash script to change the network address of a Linux Host:
#!/bin/sh
REMOTE_HOST=192.168.2.127 # Default Host address
NEW_IP=192.168.30.33 # New IP I want to set
NEW_GW=192.168.30.1 # New Gateway I want to set
sudo ifconfig eth0 192.168.2.1 # Moving to the right network...
#ping $REMOTE_HOST -c 3 # I can correctly ping the host here...
ssh-copy-id root#${REMOTE_HOST} > /dev/null # ...for my comfort...
# Setting the network with new values for the IP addr and the GW...
COMMAND="sed -i 's#address *\\([0-9.]\\+\\)#address ${NEW_IP}#' /etc/network/interfaces\
&& sed -i 's#gateway *\\([0-9.]\\+\\)#gateway ${NEW_GW}#' /etc/network/interfaces"
ssh root#${REMOTE_HOST} $COMMAND
# done!
# Now restart the network services:
ssh root#${REMOTE_HOST} "/etc/init.d/networking restart &" & # (Note the 2nd '&' !!!)
# Come back to my old IP
sudo ifconfig eth0 192.168.30.10
sudo route add default gw 192.168.30.1
This script works almost perfectly but:
1) If I run it from my home folder, no problems; if I run it from a NFS shared folder the script hangs for a minute or two before to end correctly
2) If I omit the second '&' when restarting the network on the host the command never returns...
The questions are:
1) What causes the long wait (NFS, different IP address, different gateway)? And is it possible to workaround it?
2) Why it happens? How could I avoid it?
Thanks for any kind of help and sorry for my bad English!
You're restarting networking services, which drops all active connections.
Bash reads the file you're running line by line. Since NFS is a Network File System, this will terminate the connection to the file. So the system waits (can't actually) with executing the lines after networking restart until the connection is re-established.
Instead, you should first make a local copy of the entire script and then run it locally.
You could also code a script for that ;-)

Resources