2018年2月16日 星期五

[Python 3.6] pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available

venv建立新環境後,安裝套件又發生問題

[root@python newproject]# ./bin/pip3 install requestspip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.Collecting requests  Could not fetch URL https://pypi.python.org/simple/requests/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping  Could not find a version that satisfies the requirement requests (from versions: )No matching distribution found for requests

參考以下網站,為Python 3.6增加SSL能力(https://stackoverflow.com/questions/41489439/pip3-installs-inside-virtual-environment-with-python3-6-failing-due-to-ssl-modul)

(1) 安裝openssl套件
yum install -y openssl openssl-devel (CentOS 7.4)

(2) 修改Python編譯設定檔
cd /usr/src/Python-3.6.4
./configure
vi ./Module/Setup (uncomment below lines #209~212)

SSL=/usr/lib64/openssl
_ssl _ssl.c \
        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
        -L$(SSL)/lib -lssl -lcrypto

make
make altinstall


[Python 3.6] Error: Command '['/home/newproject/bin/python3.6', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1


python3.6下使用venv建立新專案環境時發生錯誤:

[root@python Home]# python3.6 -m venv /home/newproject

Error: Command '['/home/newproject/bin/python3.6', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

經搜尋,了解python3.6預設未安裝pip,且缺了ensurepip模組
(https://github.com/ContinuumIO/anaconda-issues/issues/6917)

實際執行模組確認,發生以下問題
[root@python Home]# python3.6 -m ensurepip.....(略).....
zipimport.ZipImportError: can't decompress data; zlib not available

再經過一番搜尋,
確認必須重編python3.6支援zlib解壓縮
(網路上有其他論壇寫到./configure --with-zlib-dir=... ,此參數試過不支援)
參考下方網頁Alex Zhou的作法,重新編譯
(http://zhoujianghai.iteye.com/blog/1521993):

注意:最後步驟我改為make altinstall,保留MacOS的Python2.7,

cd /usr/src/Python-3.6.4 (我的source解開後的位置)
./configurevi Modules/Setup
(找到下面這段,將註解符號去除讓它生效)
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
make
make altinstall
完成!




2017年6月11日 星期日

Windows內建FTP client與passive mode

國內外論壇都說 Windows 內建 FTP client 不支援 passive mode


實際下指令 pasv,果然不行



這麼一來,FTP server 放在 NAT 路由器後方就不好處理了

幸運查到 可透過本地下 literal 指令將 pasv 指令遞給 server 處理:

literal pasv 5000

如果 FTP server 支援 passive mode
收到這指令後會嘗試以 TCP:5000 建立資料傳輸連線

(路由器須啟用 Virtual Server 或 Port Forwarding 功能,加一筆允許外部電腦連線至 路由器 TCP port 5000 對應至內網 FTP server IP 位址的設定。跟當初為 FTP server 服務加的那筆 TCP port 21 設定一樣)

成功後會看到訊息:

227 Entering Passive Mode (192,168,1,100,19,136).

括號內的資訊這樣讀:

192,168,1,100 就是 FTP server 位址 192.168.1.100
後方接著 19,136 是埠號 19*256 + 136 = 5000

沒認真查是 FTP 定義的寫法或是 Microsoft 獨創...

反正結果是傳檔OK!

PostgresSQL將table內容匯出為csv格式

PostgreSQL version : 9.2.18-1


COPY mytable TO '/tmp/mytable.csv' DELIMITER ',' CSV HEADER;

簡易說明:

1.須以管理者身分登入postgresql
2.將 mytable 換成你要匯出的 實際table名
3./tmp/mytable.csv 換成匯出後儲存的csv檔名即可(注意寫入權限)
4.最後的 CSV HEADER 拿掉,csv檔就不會有標題列

2017年5月18日 星期四

PHP PDO無法存取PostgreSQL在CentOS的問題

作業環境如下-

   CentOS Linux 7.3
   Apache/2.4.6 (CentOS)
   PostgreSQL 9.2.18

一支簡單的PHP程式,連線本機的PostgreSQL資料庫發生錯誤

   ....could not connect to server: Permission denied.... 


Google查到可能是SELinux保護功能阻擋了連線

確認SELinux狀態:

指令: 
   /usr/sbin/sestatus

   SELinux status:               enabled
   SELinuxfs mount:              /sys/fs/selinux
   SELinux root directory:       /etc/selinux
   Loaded policy name:           targeted
   Current mode:                 enforcing
   Mode from config file:        enforcing
   Policy MLS status:            enabled
   Policy deny_unknown status:   allowed
   Max kernel policy version:    28



確實SELinux是作用中,不太懂,只好查查設定值:

指令: 
   /usr/sbin/getsebool -a | grep httpd

   httpd_can_network_connect --> off


果然! 預設不允許httpd主動發起連線



修改設定:

指令: 
/usr/sbin/setsebool -P httpd_can_network_connect on


等了大約30秒,shell prompt終於回來了(上面getsebool指令可確認結果)


再測試PHP網頁,成功連線讀取PostgreSQL資料!

ciao~




  
   

2017年5月11日 星期四

Debian 6.0 VM 複製到 ESXi 6.0 網路卡問題

Debian VM原運作於VMWare Player,因匯入失敗,乾脆將整個目錄(含vmdk檔等)直接上傳到ESXi資料存放區

由於檔案格式不同,先用以下指令轉換為ESXi相容格式

   vmkfstools -i <原vmdk檔> <新vmdk檔>


建立VM後成功啟動系統,發現網路卡都沒出現



Google搜尋結果:

正確運作的系統會由udev於開機時偵測到網路卡,自動建立設定檔為/etc/udev/rules.d/70-persistent-net.rules。但我的/etc/udev/rules.d/下沒這個檔案!

執行 ifconfig -a 有看到 eth0 裝置,不過 ifup eth0 失敗

重新開機數次,確認系統不會自動找回網路卡



一番瞎搞後,終於成功救活,紀錄如下:

(此非正規作法,請斟酌參考,專家勿罵,留言批評者請順便提供正確教學)
(作法參考某論壇國外先進討論串,但已搜尋不到該文,對不起~)



1.手動讓它產生正確的 70-persistent-net.rules 檔

   a.下指令 ifconfig -a 找出正確的 eth0 mac 位址如(00:a0:11:22:33:44)

   b.複製原script(幫不上忙的那個)準備手動執行

      cp /lib/udev/write_net_rules /lib/udev/my_net_rules


   c.編輯手動版script (vi /lib/udev/my_net_rules),加上


      INTERFACE=eth0

      MATCHADDR="00:a0:11:22:33:44" << 步驟a 抄下來的 mac位址
 

   d.修改權限

      chmod 744 /lib/udev/my_net_rules

   e.執行!

      /lib/udev/my_net_rules

   f.檢查結果

      /etc/udev/rules.d/70-persistent-net.rules  出現!!!




 

2.測試新的設定檔

   a.重新載入設定檔

      /sbin/udevadm control --reload-rules

   b.手動啟動 eth0

      ifup eth0

   c.看看結果

      ifconfig
   (此時應該要有eth0了,沒有就是.....失敗)


 
其實還有些細節,寫多了偏離主題,下方網路設定檔供參考

/etc/network/interfaces 檔案內容

auto eth0

iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.254
dns-nameserver 168.95.192.1

如果 ifup eth0 得到 don't seem to have all the variables... 錯誤,檢查上面這個檔的內容





2017年3月15日 星期三

VirtualBox匯出之OVF,嘗試匯入ESXi 6.0失敗解法

遭遇這種問題真的很無奈

還好老外對於研究結果很大方的分享

主要參考網頁: www.itsecurenet.com
本文係參考多個網頁/論壇整理而成,可點選連結閱讀原文

親自試成功後 整理成中文版給需要的人參考

步驟1.
從VirtualBox匯出VM,選項如下圖(勾選Write Metafest file)

建議匯成OVF格式(副檔名直接加.ovf,匯出後有3個檔案),





















步驟2.
將匯出的3個檔案複製到本地硬碟(如果前一步驟儲存在server上的話)


步驟3.
嘗試用vSphere Client部署OVF檔 (ESXi 6.5後只有WEB介面),運氣好的話,系統只會偵測出Metafile裡有2或3組不支援的硬體定義,導致部署失敗。解決對策如下方(i) (ii) (iii)

說明: 部署OVF的過程,遇到問題就會馬上暫停。因此,第n個問題就是部署第n次時才會遇到的。


   (i) 虛擬硬體不相容 :

   錯誤訊息:  

   OVF 套件要求不支援的硬體  
   詳細資料: Line 25: Unsupported hardware family 'virtualbox-2.2'  











   對策: 

   以純文字編輯器(如 notepad++)編輯ovf檔,找到下面這段

      <System>
        <vssd:ElementName>Virtual Hardware Family</vssd:ElementName>
        <vssd:InstanceID>0</vssd:InstanceID>
        <vssd:VirtualSystemIdentifier>Windows Server 2012</vssd:VirtualSystemIdentifier>
        <vssd:VirtualSystemType>virtualbox-2.2</vssd:VirtualSystemType>
      </System>

   將 virtualbox-2.2 至換成 vmx-07 (請注意: 原本沒空白就別自己添加)


   (ii) 不支援VirtualBox定義的音效卡 :

   錯誤訊息:  

   OVF套件要求不支援的硬體  
   詳細資料: Line 92: OVF hardware element 'ResourceType' with instance ID '8': No support for the virtual hardware device type '35'  













   對策:

   同一個ovf檔內,移除以下這段定義

      <Item>
        <rasd:AddressOnParent>3</rasd:AddressOnParent>
        <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
        <rasd:Caption>sound</rasd:Caption>
        <rasd:Description>Sound Card</rasd:Description>
        <rasd:ElementName>sound</rasd:ElementName>
        <rasd:InstanceID>8</rasd:InstanceID>
        <rasd:ResourceSubType>ensoniq1371</rasd:ResourceSubType>
        <rasd:ResourceType>35</rasd:ResourceType>

      </Item>

   整段全部刪除 (包括 <Item> 及 </Item> 都刪掉)

   (iii) 不支援AHCI硬體

   錯誤訊息 :  

   Line 111: No space left for device '11' on parent controller '5'.  
   Line 66: Unsupported virtual hardware device 'AHCI'.  




















   對策 :

   同一個ovf檔內,找到這一段

      </Item>
      <Item>
        <rasd:Address>0</rasd:Address>
        <rasd:Caption>sataController0</rasd:Caption>
        <rasd:Description>SATA Controller</rasd:Description>
        <rasd:ElementName>sataController0</rasd:ElementName>
        <rasd:InstanceID>5</rasd:InstanceID>
        <rasd:ResourceSubType>AHCI</rasd:ResourceSubType>
        <rasd:ResourceType>20</rasd:ResourceType>
      </Item>

   改成這樣

      <Item>
        <rasd:Address>0</rasd:Address>
        <rasd:Caption>SCSIController</rasd:Caption>
        <rasd:Description>SCSI Controller</rasd:Description>
        <rasd:ElementName>SCSIController</rasd:ElementName>
        <rasd:InstanceID>5</rasd:InstanceID>
        <rasd:ResourceSubType>lsilogic</rasd:ResourceSubType>
        <rasd:ResourceType>6</rasd:ResourceType>
      </Item>

   修改完成後,儲存ovf檔。

   基於安全考量,匯出VM時選擇產生Metafile後,系統會將對應VM的vmdk硬碟檔及ovf定義檔做checksum計算寫入.mf檔。因此必須為修改過的新ovf檔更新mf內的資訊(沒有勾選Write Metafile就沒有.mf檔,匯入時好像就不檢查。請自行查詢)


步驟4.
更新.mf檔以對應新的.ovf檔 (如果匯出時有勾選Write Metafile)

下載微軟提供的checksum計算工具 (載點)

開啟DOS Shell(命令提示字元),移至剛才下載工具程式的目錄下

(如果你不是 DOS 時代長大的,指定大概是 C:\>cd /d d:\downloads 這樣)

計算新ovf檔的checksum


D:\DOWNLOADS>fciv -sha1 c:\temp\w2012svr.ovf

//
// File Checksum Integrity Verifier version 2.05
//
01970fab299090ec43355f6059f51a391d629b6f c:\temp\w2012svr.ovf

把舊的(下方xxx代表)用這一段 "01970fab299090ec43355f6059f51a391d629b6f" 換掉

SHA (w2012svr.ovf)=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx




儲存後再部署一次,成功了! 恭喜你!

如果還是有問題,用相同的方式繼續調整,應該很有機會。







VirtualBox V4.1.8 r75467