2016年5月25日 星期三

Linux Bash

  1. shell 的 PID:『 echo $$ 』
  2. 指令回傳值:『 echo $? 』
  3. 連續指令的種類:『 ; 』、『 && 』、『 || 』
  1. shell 的 PID:『 echo $$ 』
  2. [root@rhel7 ~]# echo $$
    6601
    [root@rhel7 ~]# ps aux |grep 6601
    root 6601 0.0 0.4 115484 2124 pts/0 Ss 13:03 0:00 -bash
    root 7450 0.0 0.1 112640 980 pts/0 S+ 13:32 0:00 grep --color=auto 6601
    view raw 2016052610.sh hosted with ❤ by GitHub
  3. 指令回傳值:『 echo $? 』
  4. [root@rhel7 ~]# true
    [root@rhel7 ~]# echo $?
    0
    view raw 2016052620.sh hosted with ❤ by GitHub
    [root@rhel7 ~]# false
    [root@rhel7 ~]# echo $?
    1
    view raw 2016052621.sh hosted with ❤ by GitHub
  5. 連續指令的種類:『 ; 』、『 && 』、『 || 』
  6. [root@rhel7 ~]# id || pwd
    uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    view raw 2016052620.sh hosted with ❤ by GitHub
    [root@rhel7 ~]# id && pwd
    uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    /root
    view raw 2016052621.sh hosted with ❤ by GitHub
    [root@rhel7 ~]# id ; pwd
    uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    /root
    view raw 2016052622.sh hosted with ❤ by GitHub
    [root@rhel7 ~]# false || echo "Oops, fail"
    Oops, fail
    [root@rhel7 ~]# true || echo "Will not be printed"
    [root@rhel7 ~]# true && echo "Things went well"
    Things went well
    [root@rhel7 ~]# false ; echo "This will always run"
    This will always run
    view raw 2016052630.sh hosted with ❤ by GitHub
    1. 連續指令『 cmd1 』失敗的結果:
    [root@rhel7 ~]# foo || id
    -bash: foo: command not found
    uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    [root@rhel7 ~]# foo && id
    -bash: foo: command not found
    [root@rhel7 ~]# foo ; id
    -bash: foo: command not found
    uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    view raw 2016052640.sh hosted with ❤ by GitHub

2016年5月23日 星期一

Apache Configuration Sections

連結位置:http://httpd.apache.org/docs/current/sections.html

PHP HTML 轉換為 PHP

<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
view raw 2016052403.html hosted with ❤ by GitHub
<?php
echo "<html>\n";
echo "<head>\n";
echo "<title>Page Title</title>\n";
echo "</head>\n";
echo "<body>\n";
echo "\n";
echo "<h1>This is a Heading</h1>\n";
echo "<p>This is a paragraph.</p>\n";
echo "\n";
echo "</body>\n";
echo "</html>\n";
?>
view raw 2016052404.php hosted with ❤ by GitHub
http://www.yellowpipe.com/yis/tools/HTML_converter/
view raw 2016052405.lnk hosted with ❤ by GitHub

PHP 顯示範圍內任意數值

<?php
echo rand(5200,6000);
?>
view raw 2016052402.php hosted with ❤ by GitHub

PHP HTML 內加入 PHP

<html>
<head>
<title>Information</title>
</head>
<body>
<?php
/*
multiline
*/
// Single line comments
# Another comment
echo "<p>"Data processed</>";
date_default_timezone_set('UTC')
echo date('h:i:s:u a, l F jS Y e');
?>
</body>
</html>
view raw 2016052401.php hosted with ❤ by GitHub
資料來源:https://www.youtube.com/watch?v=7TF00hJI78Y

2016年5月7日 星期六

Linux 使用者管理

[root@localhost /]# useradd user1 ;echo passwd |passwd user1 --stdin
更改使用者 user1 的密碼。
passwd:所有驗證 token 都已成功更新。
#新增使用者與建立密碼。
view raw 2016050802.sh hosted with ❤ by GitHub
[root@localhost /]# useradd user2 ;echo passwd |passwd user2 --stdin;id user2
Changing password for user user2.
passwd: all authentication tokens updated successfully.
uid=504(user2) gid=504(user2) groups=504(user2)
#查詢[uid]
view raw 2016050803.sh hosted with ❤ by GitHub
[root@localhost /]# useradd -u 1234 user3 ;echo passwd |passwd user3 --stdin;id user3
Changing password for user user3.
passwd: all authentication tokens updated successfully.
uid=1234(user3) gid=1234(user3) groups=1234(user3)
#建立帳戶,指定[uid]。
view raw 2016050804.sh hosted with ❤ by GitHub
[root@localhost /]# useradd -g Manager user5
[root@localhost /]# id user5
uid=1236(user5) gid=1236(Manager) groups=1236(Manager)
#建立帳戶時,指定群組。
view raw 2016050805.sh hosted with ❤ by GitHub
[root@localhost ~]# useradd u1
[root@localhost ~]# groupadd admins
#建立帳戶與群組。
[root@localhost ~]# grep u1 /etc/passwd
u1:x:507:509::/home/u1:/bin/bash
#查詢帳號資訊
[root@localhost ~]# usermod -s /sbin/nologin u1
[root@localhost ~]# grep u1 /etc/passwd
u1:x:507:509::/home/u1:/sbin/nologin
#將可用[bash]修改為[nologin]。
[root@localhost ~]# usermod -g admins u1
[root@localhost ~]# id u1
uid=507(u1) gid=505(admins) groups=505(admins)
#將[u1]加入[admins]群組。
view raw 2016050806.sh hosted with ❤ by GitHub

Linux 搜尋工具 find

[root@localhost ~]# find [路徑] -user [帳號] -exec [指令] {} [目的位置] \;
#後續需要進行動作需要搭配 -exec、{}
# {}為find找尋出來的項目
#結尾必須加上 \;
view raw 2016050801.sh hosted with ❤ by GitHub
[root@localhost ~]# find /home -user s1 |sort
/home/s1
/home/s1/.bash_history
/home/s1/.bash_logout
/home/s1/.bash_profile
/home/s1/.bashrc
#搜尋 /home 底下 owner 為 s1 的資料
#並且搜尋出來的資料,需要按照檔名進行排列
view raw 2016050802.sh hosted with ❤ by GitHub
[root@rhel7 ~]# find /TEMP/ -name "*.txt"
/TEMP/123.txt
/TEMP/456.txt
#找所有 txt 檔案
view raw 2016050803.sh hosted with ❤ by GitHub
[root@rhel7 ~]# find /TEMP/ -name 123 -type d
/TEMP/123
#找 123 目錄
view raw 2016050804.sh hosted with ❤ by GitHub
[root@rhel7 ~]# find /TEMP/ -type d
/TEMP/
/TEMP/123
/TEMP/234
/TEMP/345
#找所有目錄
view raw 2016050805.sh hosted with ❤ by GitHub
[root@localhost ~]# find /home -user andy -exec ls -al {} \;
total 20
drwx------. 2 andy andy 4096 Jul 17 03:45 .
drwxr-xr-x. 7 root root 4096 Jul 17 03:45 ..
-rw-r--r--. 1 andy andy 18 Jul 18 2013 .bash_logout
-rw-r--r--. 1 andy andy 176 Jul 18 2013 .bash_profile
-rw-r--r--. 1 andy andy 124 Jul 18 2013 .bashrc
-rw-r--r--. 1 andy andy 124 Jul 18 2013 /home/andy/.bashrc
-rw-r--r--. 1 andy andy 18 Jul 18 2013 /home/andy/.bash_logout
-rw-r--r--. 1 andy andy 176 Jul 18 2013 /home/andy/.bash_profile
#查詢目錄內Owner為[andy]的資料
view raw 2016050810.sh hosted with ❤ by GitHub

2016年5月5日 星期四

Juniper 查看歷史指令

root> show cli history
14:01:17 -- configure
14:24:22 -- show version
14:27:40 -- show configuration |set
14:27:52 -- configure
14:41:26 -- show system alarms
14:41:37 -- .showchassishardware
14:41:51 -- show chassis hardware
14:41:59 -- show chassis fpc pic-status
14:42:07 -- show ppp interface pp0 extensive
14:42:37 -- show log messages
14:42:42 -- show log messages |no-more
14:43:16 -- show configuration |display set |no-more
14:55:44 -- configure
14:57:57 -- show cli history
root>
view raw 2016050615.sh hosted with ❤ by GitHub

Juniper 顯示設定

root# run show configuration |display set
set version 12.1R1.9
set system syslog user * any emergency
set system syslog file messages any notice
set system syslog file messages authorization info
set system syslog file interactive-commands interactive-commands any
[edit]
view raw 2016050614.sh hosted with ❤ by GitHub

Juniper 網路卡設定

[edit]
root# set interfaces em0 unit 0 family inet address 192.168.1.1/24
view raw 2016050615.sh hosted with ❤ by GitHub

Juniper 設定主機名稱

[edit]
root@R1# set system host-name R1
[edit]
root@R1# set system domain-name icto.corp
[edit]
root@R1# set system name-server 8.8.8.8
view raw 2016050614.sh hosted with ❤ by GitHub

Juniper 設定靜態路由

[edit]
root@R1# set routing-options static route 10.50.0.0/24 next-hop 192.168.1.5
[edit]
root@R1# commit
commit complete
[edit]
root@R1# run show route
inet.0: 4 destinations, 4 routes (4 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
10.10.0.1/32 *[Local/0] 01:59:43
Reject
10.50.0.0/24 *[Static/5] 00:00:03
> to 192.168.1.5 via em0.0
192.168.1.0/24 *[Direct/0] 01:59:43
> via em0.0
192.168.1.1/32 *[Local/0] 01:59:43
Local via em0.0
[edit]
view raw 2016050612.sh hosted with ❤ by GitHub

Juniper 顯示路由資訊

[edit]
root@R1# run show route
inet.0: 3 destinations, 3 routes (3 active, 0 holddown, 0 hidden)
+ = Active Route, - = Last Active, * = Both
10.10.0.1/32 *[Local/0] 01:59:02
Reject
192.168.1.0/24 *[Direct/0] 01:59:02
> via em0.0
192.168.1.1/32 *[Local/0] 01:59:02
Local via em0.0
[edit]
view raw 2016050611.sh hosted with ❤ by GitHub
root@R1# run show route table inet.0
#顯示單撥路由
root@R1# run show route table inet.1
#顯示多撥路由
view raw 2016050613.sh hosted with ❤ by GitHub

Linux 硬碟分割

  1. 新的磁碟管理工具:gdisk
  2. 查看磁區分割狀態:lsblk
  3. 核心的磁區分割狀態:/proc/partitions
  4. 查看磁碟ID:blkid
  5. 傳統的磁碟分割工具:fdisk
  6. 範例:
    1. gfisk:新增一個磁碟分割區

  • 新的磁碟管理工具:gdisk
    [root@rhel7 ~]# yum install gdisk -y
    view raw 2016050606.sh hosted with ❤ by GitHub
    [root@rhel7 ~]# gdisk -l /dev/sda
    GPT fdisk (gdisk) version 0.8.6
    Partition table scan:
    MBR: MBR only
    BSD: not present
    APM: not present
    GPT: not present
    ***************************************************************
    Found invalid GPT and valid MBR; converting MBR to GPT format.
    ***************************************************************
    Disk /dev/sda: 266338304 sectors, 127.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 75131CAA-CA89-4CB7-865F-FBC4C4228B56
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 266338270
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 202440637 sectors (96.5 GiB)
    Number Start (sector) End (sector) Size Code Name
    1 2048 411647 200.0 MiB 8300 Linux filesystem
    2 411648 41371647 19.5 GiB 8300 Linux filesystem
    3 41371648 61851647 9.8 GiB 8300 Linux filesystem
    5 61853696 63901695 1000.0 MiB 8200 Linux swap
    view raw 2016050607.sh hosted with ❤ by GitHub
    [root@rhel7 ~]# gdisk /dev/sdb
    GPT fdisk (gdisk) version 0.8.6
    Partition table scan:
    MBR: not present
    BSD: not present
    APM: not present
    GPT: not present
    Creating new GPT entries.
    Command (? for help): m
    b back up GPT data to a file
    c change a partition's name
    d delete a partition
    i show detailed information on a partition
    l list known partition types
    n add a new partition
    o create a new empty GUID partition table (GPT)
    p print the partition table
    q quit without saving changes
    r recovery and transformation options (experts only)
    s sort partitions
    t change a partition's type code
    v verify disk
    w write table to disk and exit
    x extra functionality (experts only)
    ? print this menu
    Command (? for help):
    view raw 2016050609.sh hosted with ❤ by GitHub
  • lsblk
    [root@rhel7 ~]# lsblk
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sda 8:0 0 127G 0 disk
    ├─sda1 8:1 0 200M 0 part /boot
    ├─sda2 8:2 0 19.5G 0 part /
    ├─sda3 8:3 0 9.8G 0 part /usr
    ├─sda4 8:4 0 1K 0 part
    └─sda5 8:5 0 1000M 0 part [SWAP]
    sdb 8:16 0 127G 0 disk
    sr0 11:0 1 1024M 0 rom
    view raw 2016050605.sh hosted with ❤ by GitHub
  • 核心的磁區分割狀態:/proc/partitions
    [root@rhel7 ~]# cat /proc/partitions
    major minor #blocks name
    11 0 1048575 sr0
    8 16 133169152 sdb
    8 0 133169152 sda
    8 1 204800 sda1
    8 2 20480000 sda2
    8 3 10240000 sda3
    8 4 1 sda4
    8 5 1024000 sda5
    view raw 2016050604.sh hosted with ❤ by GitHub
  • 查看磁碟ID:blkid
    [root@rhel7 ~]# blkid
    /dev/sda1: UUID="059b93ee-695e-4929-8453-82e922e2bada" TYPE="xfs"
    /dev/sda2: UUID="58eb227d-08b5-47b8-8a6b-909f3bb3c8be" TYPE="xfs"
    /dev/sda3: UUID="881ba74d-1888-4dd8-9400-5562ec68172b" TYPE="xfs"
    /dev/sda5: UUID="202a5349-b907-40f3-ac16-80944c40a6a1" TYPE="swap"
    view raw 2016050608.sh hosted with ❤ by GitHub
  • 傳統的磁碟分割工具:fdisk
    [root@rhel7 ~]# fdisk -l
    Disk /dev/sdb: 136.4 GB, 136365211648 bytes, 266338304 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk /dev/sda: 136.4 GB, 136365211648 bytes, 266338304 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x0009d590
    Device Boot Start End Blocks Id System
    /dev/sda1 * 2048 411647 204800 83 Linux
    /dev/sda2 411648 41371647 20480000 83 Linux
    /dev/sda3 41371648 61851647 10240000 83 Linux
    /dev/sda4 61851648 266338303 102243328 5 Extended
    /dev/sda5 61853696 63901695 1024000 82 Linux swap / Solaris
    view raw 2016050601.sh hosted with ❤ by GitHub
    [root@rhel7 ~]# fdisk /dev/sdb
    Welcome to fdisk (util-linux 2.23.2).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0x082377de.
    Command (m for help): p
    Disk /dev/sdb: 136.4 GB, 136365211648 bytes, 266338304 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x082377de
    Device Boot Start End Blocks Id System
    Command (m for help):
    view raw 2016050602.sh hosted with ❤ by GitHub
    [root@rhel7 ~]# fdisk /dev/sda
    Welcome to fdisk (util-linux 2.23.2).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    Command (m for help): p
    Disk /dev/sda: 136.4 GB, 136365211648 bytes, 266338304 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x0009d590
    Device Boot Start End Blocks Id System
    /dev/sda1 * 2048 411647 204800 83 Linux
    /dev/sda2 411648 41371647 20480000 83 Linux
    /dev/sda3 41371648 61851647 10240000 83 Linux
    /dev/sda4 61851648 266338303 102243328 5 Extended
    /dev/sda5 61853696 63901695 1024000 82 Linux swap / Solaris
    Command (m for help):
    view raw 2016050603.sh hosted with ❤ by GitHub
  • 範例:gfisk:增一個磁碟分割區
    [root@rhel7 ~]# gdisk /dev/sdb
    GPT fdisk (gdisk) version 0.8.6
    Partition table scan:
    MBR: not present
    BSD: not present
    APM: not present
    GPT: not present
    Creating new GPT entries.
    Command (? for help): n
    Partition number (1-128, default 1):
    First sector (34-266338270, default = 2048) or {+-}size{KMGTP}:
    Last sector (2048-266338270, default = 266338270) or {+-}size{KMGTP}: +1G
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300):
    Changed type of partition to 'Linux filesystem'
    Command (? for help): p
    Disk /dev/sdb: 266338304 sectors, 127.0 GiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 57920DFF-C2D3-46BF-B1E1-A45181220B6C
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 266338270
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 264241085 sectors (126.0 GiB)
    Number Start (sector) End (sector) Size Code Name
    1 2048 2099199 1024.0 MiB 8300 Linux filesystem
    Command (? for help): w
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/sdb.
    The operation has completed successfully.
    view raw 2016050610.sh hosted with ❤ by GitHub

2016年5月1日 星期日

Linux RHEL7 查看所有開機過程

[root@localhost ~]# vi /boot/grub2/grub.cfg
#linux16 /vmlinuz-3.10.0-123.el7.x86_64 root=UUID=8ede3c03-88e9-4189-92b2-0d77b2e44d9d ro rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos/root crashkernel=auto vconsole.keymap=us rhgb quiet net.ifnames=0
linux16 /vmlinuz-3.10.0-123.el7.x86_64 root=UUID=8ede3c03-88e9-4189-92b2-0d77b2e44d9d ro rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos/root crashkernel=auto vconsole.keymap=us net.ifnames=0
#將 rhgb quiet 拿掉即可
view raw 20160509.sh hosted with ❤ by GitHub

Linux Script 大量查詢 Domain MX RR

domain=$(cat /root/mx.txt)
for domain in $domain
do
dig @8.8.8.8 $domain mx|grep -v ";"|grep -v '^\s*$'
done
view raw 20160508.sh hosted with ❤ by GitHub

Cisco Route OSPF

建立 Area
R1#conf t
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#router ospf 1
R1(config-router)#netwo
R1(config-router)#network 192.168.1.0 255.255.255.0 area 1
R1(config-router)#network 10.30.0.0 255.255.255.0 area 13
R1(config-router)#do wr
Building configuration...
[OK]
view raw 2016050106 hosted with ❤ by GitHub
查看鄰居
R1#show ip ospf neighbor
Neighbor ID Pri State Dead Time Address Interface
10.10.0.1 128 FULL/BDR 00:00:33 192.168.1.1 FastEthernet0/0
10.20.0.1 128 FULL/DR 00:00:32 192.168.1.2 FastEthernet0/0
view raw 2016050107.sh hosted with ❤ by GitHub
R1 Config
!
hostname R1
!
interface FastEthernet0/0
ip address 192.168.2.1 255.255.255.0
duplex auto
speed auto
!
interface FastEthernet0/1
ip address 10.1.0.1 255.255.255.0
duplex auto
speed auto
!
!
router ospf 1
log-adjacency-changes
network 10.1.0.0 0.0.0.255 area 1
network 192.168.2.1 0.0.0.0 area 0
!
view raw 2016050104 hosted with ❤ by GitHub
R2 Config
!
hostname R2
!
!
interface FastEthernet0/0
ip address 192.168.2.2 255.255.255.0
duplex auto
speed auto
!
interface FastEthernet0/1
ip address 10.2.0.1 255.255.255.0
duplex auto
speed auto
!
router ospf 1
log-adjacency-changes
network 10.2.0.0 0.0.0.255 area 2
network 192.168.2.2 0.0.0.0 area 0
!
view raw 2016050105 hosted with ❤ by GitHub

Cisco 介面設定 IP

設定IP、啟用網卡
R1#conf
R1(config)#int f0
R1(config-if)#ip address 192.168.1.3 255.255.255.0
R1(config-if)#no sh
view raw 2016050101.sh hosted with ❤ by GitHub
R1(config-if)#do sh ip int br
Interface IP-Address OK? Method Status Protocol
Ethernet0 unassigned YES unset administratively down down
Ethernet1 unassigned YES unset administratively down down
FastEthernet0 192.168.1.3 YES manual up up
view raw 2016050102.sh hosted with ❤ by GitHub
連線測試
R1(config-if)#do ping 192.168.1.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/2/4 ms
R1(config-if)#do ping 192.168.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
view raw 2016050103.sh hosted with ❤ by GitHub