對遠端Unix主機進行編程
筆者目前的工作
是在IBM的AIX作業系統上面進行開發
但是基本上筆者比較偏好的IDE還是微軟的Visual studio 2010
可是我使用的版本並沒有象pspad或是notepad++一樣
提供提供遠端編輯檔案的功能
所以我使用了python來補足這項缺憾
以下這支程式執行時
會將本地端指定目錄下的檔案上傳到 AIX的ftp上
然後再使用telnet的協定
去遙控遠端編譯
然後再傳回編譯結果
其中比較需要特別說明一下的是
我將編譯的命令寫成一的shell檔– compile.sh
但是在AIX上面編譯時
由於檔案編碼的關係
會多出一個^M
造成了編譯的失敗
所以我在python中加了一道移除 ^M 的程序
# -*- coding: utf-8 -*-
import os
from telnetlib import Telnet
from ftplib import FTP
#init telnet data
tlnData = {}
tlnData["hs"] = "192.168.0.1" # hostname
tlnData["id"] = "user" # login account
tlnData["pw"] = "1234" # pass word
tlnData["sn"] = "compile.sh" # shell script file name
tlnData["sp"] = "/home/userProject" # shell script file path
#init ftp data
ftpData = {}
ftpData["hs"] = "192.168.0.1" # hostname
ftpData["id"] = "user" # login account
ftpData["pw"] = "134" # pass word
ftpData["ld"] = "D:\\userProject\\src" # soruce code are in the local host
ftpData["ftp"] = "/home/userProject" # the path which source code will be uploaded
def pressEnter():
return "\r\n"
"""control the telnet to compile"""
def controlTelnet():
# connect
tn = Telnet(tlnData["hs"])
#tn.set_debuglevel(2)
# login
tn.expect(["login:"], 10)
tn.write(tlnData["id"])
tn.write(pressEnter())
tn.expect(["Password:"], 10)
tn.write(tlnData["pw"])
tn.write(pressEnter())
# change to root
tn.expect(["$"], 10)
tn.write("cd /")
tn.write(pressEnter())
# change to target folder
tn.expect(["$"], 10)
tn.write("cd " + tlnData["sp"])
tn.write(pressEnter())
tn.expect(["$"], 10)
tn.write("pwd")
tn.write(pressEnter())
# make a tmp file to remove ^M
tn.expect(["$"], 10)
szCmd = ' cat ./' + tlnData["sn"] + ' | tr -d "\\015" > ./tmp.sh'
tn.write(szCmd)
tn.write(pressEnter())
# modify preview
tn.expect(["$"], 10)
tn.write("chmod -R 777 ./*.*")
tn.write(pressEnter())
# compile
print "starting compile"
tn.expect(["$"], 10)
tn.write("./tmp.sh")
tn.write(pressEnter())
# remove the tmp file
tn.expect(["$"], 10)
tn.write("rm ./tmp.sh")
tn.write(pressEnter())
# exectute binary file
tn.expect(["$"], 10)
tn.write("./bin/program")
tn.write(pressEnter())
tn.expect(["$"], 10)
tn.write("exit")
tn.write(pressEnter())
szTelnet = tn.read_all()
file = open('CompileLog.txt', 'w')
file.write(szTelnet)
file.close()
print szTelnet
print "finished compile"
print "press any key to exit"
tn.close()
return True
"""update the source code to ftp server"""
def exchangeFile():
bFlag = False
ftp=FTP()
#ftp.set_debuglevel(2)
#ftp.set_pasv(True)
ftp.connect(ftpData["hs"], 21, 10)
ftp.login(ftpData["id"], ftpData["pw"])
ftp.cwd(ftpData["ftp"])
print "starting upload"
try:
recurrence(ftp, ftpData["ld"], ftpData["ftp"])
bFlag = True
except :
pass
print "finished upload"
ftp.quit()
return bFlag
def recurrence(ftp, localFile, ftpFile):
for szName in os.listdir(localFile):
currLocal = localFile + '\\' + szName
currFtp = ftpFile + '/' + szName
if os.path.isdir(currLocal):
try:
ftp.mkd(currFtp)
except:
pass
ftp.cwd(currFtp)
recurrence(ftp, currLocal, currFtp)
else:
hFile = open(currLocal,'rb')
ftp.cwd(ftpFile)
ftp.storbinary("STOR "+ szName, hFile, 1024)
print "uploaded " + currLocal
hFile.close()
def main():
if( exchangeFile() == False ):
print "upload file failed"
return
if( controlTelnet() == False ):
print "telnet control failed"
return
raw_input()
if __name__ == "__main__":
main()
然後可以加入專案中方便管理
接下來再方案總管中按右鍵,選擇"開啟方式"
選擇”Python”
接下來只要在方案總管點兩下,就可以在遠端進行編譯並且將結果傳回來