Saturday, March 30, 2013

How to comment in bat file ?

http://www.robvanderwoude.com/comments.php


IF EXIST C:\AUTOEXEC.BAT (
 :: Comment line 1
 ECHO Do something
 :: Comment line 2
 :: Comment line 3
)

Delete Files Older Than a Number of Days From DOS Read more at http://technicallyeasy.net/2011/02/delete-files-older-number-days-dos/#vqfrZB4vjOpMzy2e.99

From: http://technicallyeasy.net/2011/02/delete-files-older-number-days-dos/


dfsdfThere is a small executable called ForeFiles that can easily allow you to get the files in a directory that meet certain time criteria. In my case I want to find all files that were older than 7 days.

To do this, I added the following command to a batch file:

forfiles.exe /p D:\Files /s /m *.* /d -7 /c "cmd /c del @file"

The above line executes the forefiles.exe program with the following parameters:

Parameter Description

  • /p This parameter specifies the path that contain the files I wish to delete.
  • /s This parameter tells the program to recurse into any subfolders to look for additional files.
  • /m If you want to specify a specific file type, this parameter will allow you to limit the search to specific files, such as *.doc for Word documents. In my case, I looked for all files (*.*).
  • /d This one is the key parameter – it specifies the last modified date value. In my example I specify “-7? which indicates that the files need to have a modified date 7 days less than the current date.
  • /c This is the command that I execute on the files found by the program. The delete command is executed in a command window for each file.


Read more at http://technicallyeasy.net/2011/02/delete-files-older-number-days-dos/#vqfrZB4vjOpMzy2e.99


Related

Friday, March 22, 2013

How to for UFW script configures firewall iptables

ทำไมผมใช้ sudo ufw allow
How to for UFW script configures firewall iptables
(important note: UFW is not the firewall. UFW just configures your iptables)

in most cases I recommend doing the following immediately: ในกรณีส่วนมากแนะนำให้ตั้งค่า เริ่มต้นของ UFW เป็น deny (default ในที่นี้จะเป็น incoming package)
sudo ufw default deny
sudo ufw default deny incoming

Turn on the firewall : : เปิดการทำงาน UFW
sudo ufw enable

Turn off the firewall : ปิดการทำงาน UFW
sudo ufw disable

You can also delete all the rules with a single command : ล้างกฏทั้งหมด
sudo ufw reset

To get the current status of your UFW rules : แสดงกฏที่ตั้งไว้
sudo ufw status
sudo ufw status verbose
sudo ufw status numbered

To add deny rules : เพิ่มกฏปฏิเสธ
blocking aport
sudo ufw deny port
sudo ufw deny port 22

blocking an ip address : กั้นการเข้าถึงโดยระบุหมายเลขไอพี
sudo ufw deny from
sudo ufw deny from 10.10.10.10

blocking a specific ip address and port : กั้นการเข้าถึงโดยระบุหมายเลขไอพีและพอร์ต
sudo ufw deny from to port
sudo ufw deny from 10.10.10.10 to port 22

advanced deny example fro denying access from an ip address range 10.120.0.1 - 10.120.0.255 for SSH port 22 : ตัวอย่างการปฏิเสธการเข้าถึงพอร์ต 22 ด้วยช่วงไอพี 10.120.0.1 - 10.120.0. 255
sudo ufw deny from 10.0.0.1/24 to any port 22

To remove a deny or allow rule : ถอนกฏการปฏิเสธและการอนุญาต
sudo ufw delete from to any port

Example add and remove rule : ตัวอย่างการถอนกฏ แค่เพิ่ม delete ก่อนคำสั่ง deny หรือ allow
sudo ufw deny from 10.10.10.10
sudo ufw delete deny from 10.10.10.10

sudo ufw allow to any port 5900
sudo ufw delete allow to any port 5900

sudo ufw allow from 10.0.0.1/24 to any port 22
sudo ufw delete allow from 10.0.0.1/24 any port 22

SECNARIO : ลำดับขึ้นตอนการตั้งกฏ
เมื่อเราต้องการ block port 22 จาก ip 192.168.0.1 และ 192.168.0.7 แต่อนุญาตสำหรับ ip อื่นๆ ที่อยู่ในช่วง ip นั้น
192.168.0.x ips ให้สามารถ access port 22 ได้
sudo ufw deny from 192.168.0.1 to any port 22
sudo ufw deny from 192.168.0.7 to any port 22
sudo ufw allow from 192.168.0.0/24 to any port 22

เมื่อเราลองแสดง status rules จะได้ผลดังนี้
sudo ufw status
To                         Action  From
--                         ------  ----
22:tcp                     DENY    192.168.0.1
22:udp                     DENY    192.168.0.1
22:tcp                     DENY    192.168.0.7
22:udp                     DENY    192.168.0.7
22:tcp                     ALLOW   192.168.0.0/24
22:udp                     ALLOW   192.168.0.0/24

กฏของ UFW
คำสั่ง deny ต้องอยู่เหนือ allow เสมอ ถ้าเมื่อใดคำสั่ง allow อยู่เหนือ deny แล้ว คำสั่ง deny จะไม่ประมวลผลและใช้งานคำสั่งได้

In exist allow rule and want add deny rule : ในกรณีที่เรามีคำสั่ง allow อยู่แล้วต้องการเพิ่ม deny ในภายหลัง
sudo ufw status
To                         Action  From
--                         ------  ----
22:tcp                     ALLOW   192.168.0.0/24
22:udp                     ALLOW   192.168.0.0/24

Use key insert : ใช้ insert ในการแทรกกฏ
Example 1
block this ip to any port & block this ip to any port
sudo ufw insert 1 deny from 192.168.0.1
sudo ufw insert 1 deny from 192.168.0.1 to any port 22

block range ip to any port & block range ip to this port
sudo ufw insert 1 deny from 192.168.0.0/16
sudo ufw insert 1 deny from 192.168.0.0/16 to any port 80

sudo ufw status
To                         Action  From
--                         ------  ----
22:tcp                     DENY    192.168.0.1
22:udp                     DENY    192.168.0.1
22:tcp                     ALLOW   192.168.0.0/24
22:udp                     ALLOW   192.168.0.0/24

Example 2
sudo ufw insert 3 deny from 192.168.0.7 to any port 22
sudo ufw status
To                         Action  From
--                         ------  ----
22:tcp                     DENY    192.168.0.1
22:udp                     DENY    192.168.0.1
22:tcp                     DENY    192.168.0.7
22:udp                     DENY    192.168.0.7
22:tcp                     ALLOW   192.168.0.0/24
22:udp                     ALLOW   192.168.0.0/24
-----------------------------------------------------
การตั้งค่าเหล่านี้ (ด้านบนที่เขียนมา) จะเป็น default คือ ขาเข้า (inbound)
ถ้าต้องการจัดการขาออก (outbound) ด้วยก็ตัวอย่างประมาณนี้ (ค่าเริ่มต้นของ outbound คิดว่าน่าจะเป็น allow อยู่แล้วนะ) ฉนั้นเมื่อต้องการปิด ไม่ให้ package ที่ request จาก server วิ่งออกได้ให้ใช้คำสั่งนี้
sudo ufw default deny outgoing

ค่อยๆ ไล่เปิด outbound ที่คิดว่าจำเป็น
ufw allow out to 8.8.8.8 port 53
ufw allow out port 53
ufw allow out port 80

ส่วนตัวผู้เขียนเลือกตั้งค่าเป็น ปิดทุกพอร์ตของ inbound และเปิดทุกพอร์ตของ outbound แล้วจึงค่อยๆ ไล่เปิดพอร์ต inbound ที่จำเป็นและปลอดภัย (ในระดับหนึ่ง) เท่านั้น
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow out to any port 22  # SSH
sudo ufw allow out to any port 123 # NTP
sudo ufw allow out to any port 80  # HTTP
sudo ufw allow out to any port 22  # HTTPS
sudo ufw allow out to any port 20  # FTP Data Message
sudo ufw allow out to any port 21  # FTP Control Message
sudo ufw allow out to any port 161 # SNMP

การถอนกฏก็ทำเหมือนตอนเพิ่มแค่ delete ก่อน deny หรือ allow หลังนั้นก็พิมพ์เหมือนเดิม
sudo ufw delete allow out to any port 161 # SNMP
----------------------------------------------------
อ้างอิง
man ufw
http://ubuntuforums.org/showthread.php?t=823741
http://forum.linode.com/viewtopic.php?p=35542
http://ubuntuforums.org/showthread.php?t=1893751
http://mondotech.blogspot.com/2010/11/ufw-block-outgoing-traffic.html
http://1000umbrellas.com/2010/04/29/how-to-set-up-the-firewall-using-ufw-on-ubuntu-lucid-lynx-server
http://www2.thaiadmin.org/board/index.php?topic=121572.0

UMASK FMASK DMASK

REFER: http://ubuntuclub.com/node/58


How to search copy paste in nano ?

http://www.linuxquestions.org/questions/linux-newbie-8/how-to-copy-paste-a-line-in-nano-editor-548925/


Copy(a line) = ALT-6
Cut = CTRL-U
Paste = CTRL-K
Search = CTRL-W
Help = CTRL-G

Popular Posts