assign command output to variable - linux

I'm cloning lots of virtual machines, and I need to edit /etc/hostname and replace old hostname with a new one.
then I would have to run
hostname new-server-name
and log out of the ssd session and login again.
I'm trying to make my job easier by doing this
echo "new-server-name" > /etc/hostname
if there is a way to assign the value to /etc/hostname to host command? Something like
cat /etc/hostname | hostname
but it doesn't work.

You probably want this:
hostname $(cat /etc/hostname)
And don't forget to edit /etc/hosts to reflect the correct host name as well.

Related

Can I resolve a MX record using hosts file?

To test mail server I need an MX record in DNS server, it always with a delay because DNS cache, I need to make it faster. Is there a way make an MX record locally like A record by etc/hosts file?
Thanks, this works.
echo "10.10.10.1 mail.example.com" | sudo tee --append /etc/hosts > /dev/null
echo "disable_dns_lookups = yes" | sudo tee --append /etc/postfix/main.cf > /dev/null
systemctl restart postfix
i'm managing to use postfix on a proxmox server to delivery the mail to a local mail server on a vm , with a different ip address from the dns mx record, i've found very useful this article,
http://www.readonlymaio.org/rom/2018/01/16/force-postfix-to-search-mx-records-in-etc-hosts-file/
he leds me to disable the dns lookup in postfix to force it to use the hosts A record !!
it works
in the hosts
X.X.X.X example.com
Edit the /etc/postfix/main.cf file and add this line:
disable_dns_lookups = yes
after, restart postfix

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

some commands not wroking on remote servers through ssh (shell)

I have to run some userdefined commands on remote servers. So I do the following. It works for many commands such as crontab -l, ls, date +%s, etc... However, it doesn't work for ip addr
When I actually ssh INSIDE those server ip addr works fine. But when I execute it using ssh it doesnt.
This is how I execute it.
$ sshpass -p myPassword ssh -q root#127.0.0.1 'ip addr' > $PWD/tmp
$ cat $PWD/tmp
Again, this works for any commands I've tried so far except ip addr.
For ip addr it gives the following output
bash: ip: command not found
So i was wondering why and if there's anything wrong I'm doing....
Also, please don't sugges to use rsync or any other nondefault linux command since the environment I work in does not have them nor do I have the permission to install.
Thank you in advance
This is caused because a non-interactive ssh session does not source your login profile, which amongst other things is setting your PATH variable.
The default path does not contain /sbin, which is the usual location of the ip command.

Check if a Samba share exists in local network

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"

Linux - change the hostname in the CLI

I don't know how to search for this and that is why I asked for it (all my searches did not reveal any relevant information).
I have a Fedora 18 server that looks like this:
[root#dhcp-192-168-5-100 ~]#
I want to change that to:
[root#server1 ~]#
Currently, this machine is set to get IP through DHCP, but that is not the IP address of the machine and that is why I need to change the CLI to something more relevant.
Thank you
you can type "hostname HOSTNAME" where HOSTNAME is the new name you want. The next time you log in / connect via ssh, that's what you'll see.
Edit /etc/sysconfig/network and change/add HOSTNAME variable like so HOSTNAME=server1.domain.com After restart it should have server1.
Edit /etc/hosts and add server1 and server1.domain.com to 127.0.0.1 line, so it will look like: 127.0.0.1 localhost.localdomain localhost server1 server1.domain.com
More about network file you can read here:
https://docs.fedoraproject.org/en-US/Fedora/15/html/Deployment_Guide/ch-The_sysconfig_Directory.html#s2-sysconfig-network
Just found this and this is described pretty good.
[root#dhcp-192-168-5-100 ~]# hostname MYHOSTNAME
Change hostname in Feodora
Add "yournewhostname" into /etc/sysconfig/network in the HOSTNAME
field..
Add "yournewhostname" into /etc/hosts for 127.0.0.1
echo "yournewhostname" > /proc/sys/kernel/hostname or service hostname
IMPORTANT: logout and log back in.

Resources