Friday, January 06, 2017

WIFI script for systemd init systems


I used the following Steps to create a startup script that will automatically connect my WIFI after a reboot. Not sure if there are more efficient way but this works for me so far. I normally do minimum install so there's no GUI running on my Linux machine. Tested it on Ubuntu 16.10 Yakkety.

Pre requisite
a. Disable network-manager service.
b. You need root access.



1. Create wifi WPA supplicant config appropriate for your Wifi Connection. The example was based on using WPA-Personal (Security Mode).

vi  /etc/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant

network={
        ssid="WIFI SSID (ex FreeWIFI)"
        psk="wifipassword"
        key_mgmt=WPA-PSK
        proto=RSN WPA
        pairwise=CCMP TKIP
        group=CCMP TKIP
}


2. Create the script that will contains the CLI command that connects the wifi

vi /*path*/wifi.sh
#!/bin/bash
/sbin/wpa_supplicant -Dnl80211 -iwlp2s0 -c/etc/wpa_supplicant.conf &

------
*Change the *path* to a folder of your choice (do the same on Step 3)
wlp2s0 - is the name of your wifi interface name. Run "ip addr sh" if you're not sure.


3. Create the systemd  init script file

vi /etc/systemd/system/wifi.service
[Unit]
Description=This will and connect the wifi @ startup

[Install]
WantedBy=multi-user.target

[Service]
Exec=/*path*/wifi.sh
Type=oneshot
RemainAfterExit=yes


4. ADD the systemd init script during Startup using systemctl:

systemctl daemon-reload
systemctl enable wifi.service
systemctl start wifi.service


5. Reboot your system to test if script works

reboot

==================================================

On second note just run nmtui if your network manager is working fine will save you the trouble of manually creating the wpa_supplicant file. The tutorial above works best if you're only connecting to 1 wifi connection and you don't expect the linux box or server to be more elsewhere.

Saturday, November 19, 2016

Task Automation without API through Headless Browser

I've been researching lately on headless browsing as I have this automation idea that can literally put me on early retirement. For those of you who are looking to automate some task but the Site you're relying on don't have any available API to work with then this might solve your problem. Special thanks to my colleague Huy who suggested the idea.


Undertanding Headless Browser:
https://en.wikipedia.org/wiki/Headless_browser

PhantomJS - This is a good headless browser
http://phantomjs.org/

CasperJS - You can create simple javascript code using this which makes interaction and navigation with your headless browser so much easier.
http://casperjs.org/

Sunday, April 13, 2014

Modify FSTAB on CentOS or RHEL 6 on Bootup

Ever encounter a problem when you can't boot due to corrupted or wrong FSTAB entry. Here's a simple fix

Problem:

Solution:

a. At  Grub press any key
b. Press "e" to edit

c. Under kernel parameter Append this command "init=/bin/bash"
d. Press Enter then "b"
e. This will take you directly to #.. Remount the / by typing  mount -o remount,rw /
f.  Edit fstab vi /etc/fstab
g. Save and Reset the system

Saturday, March 02, 2013

Annoying "perl: warning: Setting locale failed"

This post shows a simple way of removing this annoying warning when running perl in linux.


perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_CTYPE = "UTF-8",
LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

Solution:

on your home directory:


1. Edit your user bash_profile

vi .bash_profile

2. Insert the following:

export LANGUAGE=en_US.UTF-8

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

3. Rerun your Bash Profile

. ~/.bash_profile 


Hope this helps.

Sunday, July 24, 2011

FIND and REPLACE TEXT inside a file without using TEXT Editor


FIND and REPLACE TEXT inside a file without using TEXT Editor


You can perform a text replace within a file without using VI or other text editor available on your Unix or Linux box. Below command will help you perform that action. 


Using SED:
sed -i 's/FIND TEXT/REPLACE TEXT/g' FILENAME.TXT


Using PERL:
perl -p -i -e "s/FIND TEXT/REPLACE TEXT/g" FILENAME.TXT




Where:
FIND TEXT - Content you want to replace
REPLACE TEXT - New Value of the content
FILENAME.TXT - Filename

Thursday, July 07, 2011

FOLDING IN LINUX

Fold Long Line files to Finite Width output device 
files.

Ex: fold -b < long_line_file >folded_file

Now the folded_file will  contain output with newlines 
inserted after maximum bytes specified.  This will be 
useful when editing special files. You can do the reverse 
using "expand" command.

Monday, March 08, 2010

BAD and BETTER COMMANDS

BAD and BETTER COMMANDS

I cringe anytime I see someone code inefficiently.  Here are
three of the most common mistakes, followed by a better way to
do the same thing.

Bad:    cat somefile | grep something
Better: grep something somefile
Why:    You're running one program (grep) instead of two (cat
and grep).

Bad:    ps -ef | grep something | grep -v grep
Better: ps -ef | grep [s]omething
Why:    You’re running two commands (grep) instead of three (ps
and two greps).

Bad:    cat /dev/null > somefile
Better: > somefile
Why:    You're running a command (cat) with I/O redirection,
instead of just redirection.

Although the bad way will have the same result, the good way is
far faster.  This may seem trivial, but the benefits will really
show when dealing with large files or loops.