public class Play3{
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("123345");
System.out.println(sb);
System.out.println("長度"+sb.length());
System.out.println("等下要比對次數"+sb.length()/2);
int f=0;
int l=sb.length()-1;
for(int i= 0 ; i<sb.length()/2;i++)
{
if(sb.toString().charAt(f) == sb.toString().charAt(l))
{
System.out.print("第"+(i+1)+"個跟後面數來第"+(i+1)+"個有一樣");
break;
}
f++;
l--;
}
}
}
2012年12月20日 星期四
Java-寫出一個過濾陣列的Method
public int[] deleteSameNumber(int[] ary)
{
int[] a = new int[ary.length];
int k = 0;
for (int i = 0 ; i<ary.length ; i++)
{
int exist = 0;//此值用來檢查是否有重複性
for(int j = 0; j<a.length;j++)
{
if(ary[i] == a[j]) exist =1;
}
if (exist == 0) a[k++]=ary[i];//表示傳進去的ary[i]在a陣列裡沒有
}
return a;
}
{
int[] a = new int[ary.length];
int k = 0;
for (int i = 0 ; i<ary.length ; i++)
{
int exist = 0;//此值用來檢查是否有重複性
for(int j = 0; j<a.length;j++)
{
if(ary[i] == a[j]) exist =1;
}
if (exist == 0) a[k++]=ary[i];//表示傳進去的ary[i]在a陣列裡沒有
}
return a;
}
2012年11月27日 星期二
手動建置一台虛擬的SwitchHub
TUN/TAP 原本就是 Linux 系統所提供的虛擬網路介面裝置,只是預設並不是每套 Linux 的發行版本都有內建此功能。
查看是否有安裝
$tunctl
操控TUN/TAP的套件名稱為"uml_utilities"
$sudo apt-get install uml-utilities
用BashScript寫一隻程式,可以快速建立網卡
建立橋接裝置br01(這時候建立的只是一個空殼)
$sudo brctl addbr br01
指定一個ip給br01
$sudo ifconfig br01 192.168.10.1/24 up
這時候的bridge抽象是沒有port也就是說沒有洞讓別人聯接進來
底下手洞建立三張網卡br01p1、br0p2、br0p3
$sudo tunctl -t br0p1 -u student
$sudo tunctl -t br0p2 -u student
$sudo tunctl -t br0p3 -u student
$sudo ifconfig br0p1 up
$sudo ifconfig br0p2 up
$sudo ifconfig br0p3 up
然後把網卡加進br01,一台虛擬的SwitchHub就完成了!!!
$sudo brctl addif br01 br0p1
$sudo brctl addif br01 br0p1
$sudo brctl addif br01 br0p1
查看是否有安裝
$tunctl
操控TUN/TAP的套件名稱為"uml_utilities"
$sudo apt-get install uml-utilities
建立一張網路卡名稱為p1只有student可以用
$sudo
tunctl -t p1 -u user用BashScript寫一隻程式,可以快速建立網卡
建立橋接裝置br01(這時候建立的只是一個空殼)
$sudo brctl addbr br01
指定一個ip給br01
$sudo ifconfig br01 192.168.10.1/24 up
這時候的bridge抽象是沒有port也就是說沒有洞讓別人聯接進來
底下手洞建立三張網卡br01p1、br0p2、br0p3
$sudo tunctl -t br0p1 -u student
$sudo tunctl -t br0p2 -u student
$sudo tunctl -t br0p3 -u student
$sudo ifconfig br0p1 up
$sudo ifconfig br0p2 up
$sudo ifconfig br0p3 up
然後把網卡加進br01,一台虛擬的SwitchHub就完成了!!!
$sudo brctl addif br01 br0p1
$sudo brctl addif br01 br0p1
$sudo brctl addif br01 br0p1
2012年11月20日 星期二
Linux核心虛擬系統
KVM:Kernel-based Virtual Machine
關於KVM相關資訊:http://www-03.ibm.com/systems/virtualization/infrastructure/open/index.html
完全虛擬化(Full virtualization) 例如:VMware、VirtualBox。
半虛擬化(Paravirtualization)此虛擬化會去改元系統的Kernel,此技術是XEN創造。
寄居架構(Hosted Architecture)
裸機架構(Bare-Metal)系統裝完就內建虛擬技術,KVM、VMware ESX
Nested VM巢狀系統
查看是否有支援VT-x ※有些硬體support but BIOS沒開
$sudo kvm-ok
探索核心模組
$modprobe -l |grep kvm 探索核心模組

kvm主要是負責虛擬CPU、Memory
quem負責周邊hard disk、N
$sudo apt-get install qemu-kvm
啟動虛擬機
$kvm -m 128 -cdrom ISO/tsc32.iso -boot d
-m 記憶體128M
cdrom掛到/ISO/底下
這裡採用的Linux為Tiny core的Live CD版本此作業系統特色是檔案極小,大約是10MB
出現以下畫面就代表虛擬機啟動成功but 沒live cd版本
底下將建置有HardDisk的虛擬機
Step1(終端機執行)
先產生一個資料夾並產生虛擬硬碟檔
產生虛擬硬碟檔
啟動 Tiny Server Core 虛擬電腦
關於KVM相關資訊:http://www-03.ibm.com/systems/virtualization/infrastructure/open/index.html
完全虛擬化(Full virtualization) 例如:VMware、VirtualBox。
半虛擬化(Paravirtualization)此虛擬化會去改元系統的Kernel,此技術是XEN創造。
寄居架構(Hosted Architecture)
裸機架構(Bare-Metal)系統裝完就內建虛擬技術,KVM、VMware ESX
Nested VM巢狀系統
查看是否有支援VT-x ※有些硬體support but BIOS沒開
$sudo kvm-ok
探索核心模組
$modprobe -l |grep kvm 探索核心模組

kvm主要是負責虛擬CPU、Memory
quem負責周邊hard disk、N
$sudo apt-get install qemu-kvm
啟動虛擬機
$kvm -m 128 -cdrom ISO/tsc32.iso -boot d
-m 記憶體128M
cdrom掛到/ISO/底下
這裡採用的Linux為Tiny core的Live CD版本此作業系統特色是檔案極小,大約是10MB
出現以下畫面就代表虛擬機啟動成功but 沒live cd版本
底下將建置有HardDisk的虛擬機
Step1(終端機執行)
先產生一個資料夾並產生虛擬硬碟檔
產生虛擬硬碟檔
$ kvm-img create -f raw vmdisk/tsc32.img 20m
啟動 Tiny Server Core 虛擬電腦
$kvm -m 128 -cdrom ISO/tsc32.iso -hda vmdisk/tsc32.img -boot d &
-m ─ memory
-cdrom ─ cdrom掛載的位置
-hda ─ Hard Disk掛載的位置
-boot d ─ 先從硬碟開始讀
& ─ 啟動後在背後執行
Step2(QEMU視窗內執行)
建立分割區並格式化ext4檔
$sudo fdisk /dev/sda
底下是進入硬碟分隔模式的指令
---
|--n 新增一個分割區
|
|--p (primary)建立主要分割區)
| |--1 | |--1 | |--1 | |--w 確定寫入
$sudo mkfs.ext4 /dev/sda1
重開機
$sudo reboot
底下用Shell Script語法寫一個檔案,使其可以快速地開啟多部虛擬機 $sudo nano myring.sh
----------------------------------------myring.sh---------------------------------------------------
#!/bin/bash
[ ! -f vmdisk/myring01.img ] && cp vmdisk/tsc32.img vmdisk/myring01.img
#-f=find 中括號裡判斷file是否存在 不存在時執行後面的指令
kvm -m 64 -hda vmdisk/myring01.img -cdrom ISO/tsc32.iso -boot d &
[ ! -f vmdisk/myring02.img ] && cp vmdisk/tsc32.img vmdisk/myring02.img
kvm -m 64 -hda vmdisk/myring02.img -cdrom ISO/tsc32.iso -boot d &
[ ! -f vmdisk/myring03.img ] && cp vmdisk/tsc32.img vmdisk/myring03.img
kvm -m 64 -hda vmdisk/myring03.img -cdrom ISO/tsc32.iso -boot d &
-----------------------------------------------------------------------------------------------------------
2012年11月18日 星期日
Ubuntu 12.04 server版本進階
選擇安裝的版本為 : ubuntu-12.04.1-server-amd64.iso
VM設定:VMWorksation 9
網路設定 :在VMWorksation 9網路設定為NAT
建立群組
sudo groupadd aaa
將使用者a01 a02 加入aaa群組
sudo /etc/group 進程式底下 把a01,a02加到aaa那行最後面
讓檔案開啟所有權限
sudo chmod 777 FileName
複製檔案
cp Source Destination
※補充
A 拷貝到 B 假設B原來有該檔案 就會繼承B權限
如果B沒有該檔案 就會繼承A檔案的權限,另外如果下cp 前面有加 sudo 則owner為root
改變檔案所屬群組chgrp
chgrp aaa mya01
群組 檔案
自創命令
---------------bashrc-----------------
alias ping='ping -c 4'
alias ipconfig='/etc/update-motd.d/50-landscape-sysinfo'
export PS1="\u@\h:"
alias bye'sudo shutdown -h +2"2分鐘後關機"'
-----------------------------------------
Sticky Bit
a01使用者到/home/student/創一個mysbit資料夾並更改權限
chmod 1777 mysbit
1.a01進到mysbit建檔Hello1.txt Hello.txt2 Hello3.txt
2三個使用者分別刪檔案
結構如下
home
|
|--student
| |
| |--mysbit
| |
| |-Hello1.txt <--student 可以刪
| |
| |-Hello2.txt <--a02 不能刪
| |
| |-Hello3.txt <--a01 可以刪
sudo apt-get install apache2
sudo /etc/init.d/apache2 restart 啟動apache
nano /var/www/index.html 編輯網站
特殊指令
bash
pstree
----------自創程式test.sh-------------
read -p "" a
read -p "" b
declare -i sum=$(($a+$b))
echo $sum
VM設定:VMWorksation 9
網路設定 :在VMWorksation 9網路設定為NAT
建立群組
sudo groupadd aaa
將使用者a01 a02 加入aaa群組
sudo /etc/group 進程式底下 把a01,a02加到aaa那行最後面
讓檔案開啟所有權限
sudo chmod 777 FileName
複製檔案
cp Source Destination
※補充
A 拷貝到 B 假設B原來有該檔案 就會繼承B權限
如果B沒有該檔案 就會繼承A檔案的權限,另外如果下cp 前面有加 sudo 則owner為root
改變檔案所屬群組chgrp
chgrp aaa mya01
群組 檔案
自創命令
---------------bashrc-----------------
alias ping='ping -c 4'
alias ipconfig='/etc/update-motd.d/50-landscape-sysinfo'
export PS1="\u@\h:"
alias bye'sudo shutdown -h +2"2分鐘後關機"'
-----------------------------------------
Sticky Bit
a01使用者到/home/student/創一個mysbit資料夾並更改權限
chmod 1777 mysbit
1.a01進到mysbit建檔Hello1.txt Hello.txt2 Hello3.txt
2三個使用者分別刪檔案
結構如下
home
|
|--student
| |
| |--mysbit
| |
| |-Hello1.txt <--student 可以刪
| |
| |-Hello2.txt <--a02 不能刪
| |
| |-Hello3.txt <--a01 可以刪
sudo apt-get install apache2
sudo /etc/init.d/apache2 restart 啟動apache
nano /var/www/index.html 編輯網站
特殊指令
bash
pstree
----------自創程式test.sh-------------
read -p "" a
read -p "" b
declare -i sum=$(($a+$b))
echo $sum
Java 讀取網頁內容並用HashSet過濾資料
import java.io.*;
import java.net.*;
import java.util.*;
public class MyLog01 {
/**
* 讀取網頁內容
*
* @(Yu-Yu Chen)
* @version(V 0.1 2012/11/18)
*/
public static void main(String[] args) throws IOException {
URL url = new URL("http://tobala.net/download/wiki.myruby.net-May-2012");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(
huc.getInputStream(), "UTF-8"));
Set<String> ip = new HashSet<String>();
String str = "";
StringBuffer sb = new StringBuffer();
while (null != ((str = br.readLine()))) {
// 相同 IP 字串不會加入 ip 這個 HashSet 集合物件中
ip.add(str.substring(0, str.indexOf(' ')));
}
br.close();
System.out.println("共 " + ip.size() + " 個 ip 位址");
// 列印出所有不相同 IP 位址
Iterator It = ip.iterator();
while (It.hasNext()) {
System.out.print(It.next() + " , ");
}
}
}
import java.net.*;
import java.util.*;
public class MyLog01 {
/**
* 讀取網頁內容
*
* @(Yu-Yu Chen)
* @version(V 0.1 2012/11/18)
*/
public static void main(String[] args) throws IOException {
URL url = new URL("http://tobala.net/download/wiki.myruby.net-May-2012");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(
huc.getInputStream(), "UTF-8"));
Set<String> ip = new HashSet<String>();
String str = "";
StringBuffer sb = new StringBuffer();
while (null != ((str = br.readLine()))) {
// 相同 IP 字串不會加入 ip 這個 HashSet 集合物件中
ip.add(str.substring(0, str.indexOf(' ')));
}
br.close();
System.out.println("共 " + ip.size() + " 個 ip 位址");
// 列印出所有不相同 IP 位址
Iterator It = ip.iterator();
while (It.hasNext()) {
System.out.print(It.next() + " , ");
}
}
}
2012年11月9日 星期五
Ubuntu 12.04 server 版本基本安裝設定
選擇安裝的版本為 : ubuntu-12.04.1-server-amd64.iso
VM設定:VMWorksation 9
網路設定 :在VMWorksation 9網路設定為NAT
由於server本機不支援UTF-8所以要改程式
在使用者目錄打ll, 然後會看到bashrc,修改它
sudo nano .bashrc
到程式最下方加上
-----------------------------------------------------------------------------------------------------------
if [ -z "$SSH_TTY" ]; then
export LANG=en_US.UTF-8
export LANGUAGE=en_US
export LC_ALL=en_US.UTF-8
else
export LC_ALL=zh_TW.UTF-8
fi
-----------------------------------------------------------------------------------------------------------
帳號建立,密碼設定
sudo useradd -m -s /bin/bash a01 # 設定a01帳號,一般帳號設定(建立自己home
目錄,/home/a01,建立shell環境)
-m 強制建立使用者家目錄 -s 指定路徑
sudo passwd a01 # 設定a01帳號的密碼
TTY開啟
Ctrl + ALT + F2 # 第二個終端機開啟 / 切換
網路設定
ifconfig -a # 看網卡所有設定資訊
netstat -rn # 看Kernal ip routing table(包含Getway資訊)
cat /etc/resolv.conf # 看DNS
網路手動設定
sudo nano /etc/network/interfaces # 網路介面設定
Mark掉dhcp以下這一行(關閉Vmware Worksation 9 dhcp設定,改設靜態IP)
# iface eth0 inet dhcp
設定以下網路設定(設定靜態IP,這邊網路ip並非一成不變,請看VM給的設定)
-----------------------------------------------------------------------------------------------------------
iface eth0 inet static
address 192.168.X.X # 192.168.X.1被VM預設占用
netmask255.255.255.0
gateway 192.168.X.2 # 192.168.X.2被VM設定為gateway
dns-nameservers 168.95.1.1
sudo /etc/init.d/networking restart # 重新啟動網卡,網路才能生效
-----------------------------------------------------------------------------------------------------------
建立SSH連線
sudo apt-get install ssh #server端安裝ssh
再到Client測試連線
1.
2.

手動更改apt-get伺服器
sudo nano /etc/apt/sources.list
更改登入畫面
sudo nano /etc/issue
\r作業系統版本
\d顯示日期
更改登入後的畫面 (CentOS及Fedora,Ubuntu需外加一支程式)
sudo nano /etc/motd
Ubuntu必須到/etc/update-motd.d/99-hello
-----------------------------------------------------------------------------------------------------------
#!/bin/bash
echo "Hello Friend"
-----------------------------------------------------------------------------------------------------------
程式碼打完須變更權限
sudo chmod +x /etc/update-motd.d/99-hello
2012年11月8日 星期四
Ubuntu-12.04.1 基本操作
選擇安裝的版本為 : ubuntu-12.04.1-desktop-amd64.iso
VM設定:VMWorksation 9
網路設定 :在VMWorksation 9網路設定為NAT
更新系統套件
sudo apt-get update
sudo apt-get upgrade
安裝gnome桌面環境
sudo apt-get install gnome
sudo apt-get install gnome-tweak-tool
安裝kde桌面環境
sudo apt-get install kubuntu-desktop
kde桌面環境中文化
sudo apt-get install kde-l10n-zhtw
使用者忘記密碼
sudo nona /etc/passwd
更改本機名稱 (Ctrl+O 存檔 Ctrl+X離開)
sudo nano /etc/hostname
sudo nano /etc/hosts
系統負載狀態
top
要加密解密前要載的東西
sudo apt-get install seahorse-nautilus
VM設定:VMWorksation 9
網路設定 :在VMWorksation 9網路設定為NAT
更新系統套件
sudo apt-get update
sudo apt-get upgrade
安裝gnome桌面環境
sudo apt-get install gnome
sudo apt-get install gnome-tweak-tool
安裝kde桌面環境
sudo apt-get install kubuntu-desktop
kde桌面環境中文化
sudo apt-get install kde-l10n-zhtw
使用者忘記密碼
sudo nona /etc/passwd
更改本機名稱 (Ctrl+O 存檔 Ctrl+X離開)
sudo nano /etc/hostname
sudo nano /etc/hosts
系統負載狀態
top
要加密解密前要載的東西
sudo apt-get install seahorse-nautilus
訂閱:
文章 (Atom)