Posts

Shell script parse xml in linux

Image
How to use shell script parse xml file . In this tutorial, I will use grep command, awk command and sed command Reading xml file extracting value in linux . For example xml person file as below <person>   <sex>Male</sex>   < firstname >Huu</ firstname >   <lastname>Phan</lastname>   <sex>female</sex>   < firstname >Miranda</ firstname >   <lastname>Kerr</lastname> </person> How to extracting value Huu and Miranda  of < firstname > < /firstname > Use grep command [huupv@huupv huuphan.com]$ grep -oP '(?<=<firstname>).*(?=</firstname)' person.xml Use awk command [huupv@huupv huuphan.com]$ awk -F "[><]" '/firstname/{print $3}' person.xml Use sed command [huupv@huupv huuphan.com]$ sed -n '/firstname/{s/.*<firstname>//;s/<\/firstname.*//;p;}' person.xml  The result, Shell script parse xml in linux

rename command in linux

Image
rename command similar with mv command. This command is slightly more advanced than mv  command. How to rename multiple files in linux. The syntax rename command rename [options] expression replacement-file For example, basic use rename command [huupv@huupv huuphan.com]$  rename file huu file1.txt The same output How to rename all file extension .txt to .atxt file [huupv@huupv huuphan.com]$ rename .txt .atxt *.txt The same output Use option --verbose in rename command [huupv@huupv huuphan.com]$ rename -v foo huu foo?.atxt The same ouput `foo2.atxt' -> `huu2.atxt' `foo3.atxt' -> `huu3.atxt' Details more information rename command man rename

How to setup OpenVPN Server on Centos 7

In this tutorial, I will setup OpenVPN Server on Centos 7.   Step 1: Prepare install OpenVPN server sudo yum update -y sudo yum install epel-release -y sudo yum update -y sudo yum install -y openvpn easy-rsa Configure Ip forwarding for OpenVPN Server vim /etc/sysctl.conf The content sysctl.conf file as below: Packet forwarding net.ipv4.ip_forward = 1   Step 2: Configure OpenVPN Server Open server.conf file vim /etc/openvpn/server.conf The content configure as below: #Secure OpenVPN Server Config #Basic Connection Config dev tun proto udp port 1194 keepalive 10 120 max-clients 4 #Certs ca ca.crt cert server.crt key server.key dh dh.pem tls-auth ta.key 0 #Ciphers and Hardening reneg-sec 0 remote-cert-tls client crl-verify crl.pem tls-version-min 1.2 cipher AES-256-CBC auth SHA512 tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256 #D

Failed to create the VirtualBox COM object solve problem

Image
Someday, your Virtualbox not start and the display error "Failed to create the VirtualBox COM object". it got somewhat corrupted. The error of Virtualbox "Failed to create the VirtualBox COM object" as below: Location: '/home/huupv/.config/VirtualBox/VirtualBox.xml' , line 1 (0), column 1. /home/vbox/vbox-5.2.14/src/VBox/Main/src-server/VirtualBoxImpl.cpp[554] (nsresult VirtualBox::init()). How to solve problem! Step 1: make sure process VBoxSVC not running Step 2: make a copy file VirtualBox.xml then delete original file. Step 3: Rename VirtualBox.xml-prev file to VirtualBox.xml file. Step 4: try again start VirtualBox. To check not running process VBoxSVC not running use ps command as below: $ ps -ef | grep VBoxSVC huupv 10122 2421 0 22:19 pts/1 00:00:00 grep --color=auto VBoxSVC To check VirtualBox.xml file and Delete VirtualBox.xml file $ ll /home/huupv/.config/VirtualBox/ | grep VirtualBox.xml -rw-----

How to connect openvpn server from a linux computer

Image
Connect access server (VPS) from a linux computer. In this tutorial, I will access server VPS installed OpenVPN server from a linux computer. Ref to How to setup OpenVPN Server on Centos 7 Install openvpn client on linux Run Openvpn client with .ovpn file for example: client.ovpn Install OpenVPN client on linux  Fedora/CentOS/RedHat client sudo yum install openvpn Ubuntu/Debian client sudo apt-get install openvpn Checking Openvpn client version [huupv@huupv ~]$ openvpn --version The output as below: OpenVPN 2.4.6 x86_64-redhat-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Apr 26 2018 library versions: OpenSSL 1.1.0h-fips  27 Mar 2018, LZO 2.08 Running the OpenVPN client linux computer with the downloaded client config ovpn file: [huupv@huupv vpn-client-01-config]$ sudo openvpn --config client.ovpn The output OpenVPN client linux connect to server OpenVPN server as below: Sun Jul  1 21:43:35 2018 TUN/TAP device tun0 open

Split a string in shell and get the last field

Image
How to split a string in shell and get the last field. in this tutorial, i written a small program use bash script split string /www/huu/phan/com/xyz to new path /www/huu/phan/com and last field of new path /www/huu/phan/com is com . Introduction Input: one_path is /www/huu/phan/com/xyz Output: new_path is  /www/huu/phan/com last_field of new_path is com My bash shell as below: #!/bin/bash ############### # # Author: HuuPV # My blog: www.huuphan.com # ############### # How to split a string in shell and get the last field one_path="/www/huu/phan/com/xyz" last_slash=-1 for (( i=0; i < ${#one_path}; i++)); do         if [ ${one_path:$i:1} == '/' ]         then                 last_slash=$i;         fi done new_path=${one_path:0:$last_slash} last_field=$(basename $new_path) echo "Old path: $one_path" echo "New path: $new_path" echo "Last field of $new_path:  $last_field" The display as picture below:

Lock and Unlock zimbra account from command line

Image
How to Lock and Unlock zimbra account from command line. some account sending email spam, you are administrator to need lock account from command line. To login your Mailbox system. To perform some command below to lock and unlock zimbra account. For example, I will lock and unlock account [email protected] . The command line as below running su - zimbra . Now, let's start! Lock and Unlock zimbra account Step 1: Checking status account [email protected] as below: $ zmprov ga huupv02mail.huuphan.com | grep zimbraAccountStatus The display as below: Another command to check status account with zmaccts as below: Step 2: To Lock zimbra account [email protected] $ zmprov ma [email protected] zimbraAccountStatus locked The display as picture below: To check account status after locked Step 3: To Unlock zimbra account [email protected] $ zmprov ma [email protected] zimbraAccountStatus active The display as picture belo

Linux diff command

Image
In this tutorial, I use diff command output on screen. How to compare different line by line in  files. How to compare side by side, color , output do not output common lines with diff command. What diff command work? compare files line by line For example my topic, I compare line by line in 2 file: file1.txt and file2.txt The content file1.txt file [huupv@huupv huuphan.com]$ cat file1.txt <####################################> author: huupv My Blog: www.huuphan.com Hello everbody! <####################################> The content file2.txt file [huupv@huupv huuphan.com]$ cat file2.txt <####################################> author: huupv My Blog: www.devopsrole.com Hello everbody! <####################################> The default compare line by line with diff comand [huupv@huupv huuphan.com]$ diff file1.txt file2.txt The output as below: 3c3 < My Blog: www.huuphan.com --- > My Blog: www.devopsrole.com The output number

Postfix only allow whitelisted Recipient Domain

Image
In this tutorial, How to Configure " Postfix only allow whitelisted Recipient Domain ". We test environment with user data. To minimize the risk of sending to unwanted email recipients. Step 1: Add line into main.cf file as below smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recipient_domains, reject Note: Warring /etc/postfix/recipient_domains, reject , Allow receive email in recipient_domain, and REJECT All domain not in whitelisted domain. Step 2: To create recipient domain is the whitelist file mycompany.com OK mail.huuphan.com OK Note: only two domain mycompany.com and mail.huuphan.com receive mail Step 3: To generate hash file: $ sudo postmap /etc/postfix/recipient_domains Step 4: To restart postfix service $ sudo /etc/init.d/postfix restart Now to try sending an email to a another domain not in whitelist. You will find error something like that as below: NOQUEUE: reject: RCPT from …: 554 5.7.1 <[email protected]&g

Bash script arguments from a file content

Image
In this tutorial, how to change string content of file as arguments. I written  Bash script arguments from a file content as below: Input file How to change NAME and DOMAIN in input file with any arguments in content as below. My name: NAME My Blog: https:// DOMAIN .com Output file with arguments default value My name: Huu My Blog: https://huuphan.com Output file with arguments another vaule  My name: Devops My Blog: https://devopsroles.com My bash script  $ cat arguments_file.sh The content bash script #!/bin/bash argument1=Huu argument2=huuphan INPUT=/home/huupv/scripts/input.txt OUTPUT=/home/huupv/scripts/output.txt RED='\033[0;31m' NC='\033[0m' # No Color if [ $# -eq 2 ];then     argument1=$1     argument2=$2 fi sed -f - "$INPUT" > "$OUTPUT"<<EOF     s/NAME/$argument1/g     s/DOMAIN/$argument2/g EOF Running bash script with default value $ ./arguments_file.sh The output as picture below: To run