linux遠端出觸發windows目錄下的批處理
阿新 • • 發佈:2018-11-12
背景需求:有一臺linux編譯伺服器,需要在編譯完成後自動將該檔案部署到遠端window的tomcat下並重啟該伺服器!!!
解決方案:telnet+pscp
解決過程:首先linux編譯機器想要觸發window的批處理,在這裡選擇使用window的telnet服務
linux下的指令碼如下:stop_9095.py(停止服務並觸發遠端指令碼del_ROOT_9095.bat)
#!/usr/bin/env python
def auto_telnet(ip, username, password, finish):
import telnetlib
# 連線Telnet伺服器
tn = telnetlib.Telnet(Host, port=23)
tn.set_debuglevel(1)
#輸入使用者名稱
tn.read_until('login: ')
tn.write(username + '\r\n')
#輸入密碼
tn.read_until('password: ')
tn.write(password + '\r\n')
#登入執行命令
tn.read_until(finish)
tn.write('net stop tomcat9095\r\n')
tn.read_until(finish)
tn.write('pushd E:\Java\deploy\r\n')
tn.read_until(finish)
tn.write('del_ROOT_9095.bat\r\n')
tn.read_until('finished copy')
tn.write('net start tomcat9095\r\n')
tn.read_until(finish)
time.sleep(1)
tn.close()
if __name__=='__main__':
ip = '192.168.1.30'
username = 'administrator'
password = '123456'
finish = '>'
auto_telnet(ip, username, password, finish)
遠端伺服器下批處理:del_ROOT_9095.bat (刪除原有資料夾遠端上傳檔案)
@echo off
set tomcat_dir=%cd%\..\cluster\tomcat_9095\bin
pushd %tomcat_dir%\..\webapps
if exist eye (
call rmdir /s /q eye/*
)
pscp -l root -pw 123456 -r [email protected]:/home/deploy/tomcat9095/code/eye/* E:\Java\cluster\tomcat_9095\webapps\eye\
echo "finished copy"
pause