#! /usr/bin/env python2.6
# 2013-1-23
# Tong.yang
#
"""
This runs a command on a remote host using SSH/SCP. At the prompts enter hostname, user, password and the command
"""
import pexpect
import os
import getpass
from time import sleep
def ssh_command(files, user, host, password):
ssh_key = 'Are you sure you want to continue connecting'
# child = pexpect.spawn('ssh -l %s %s %s' % (user, host, command))
child = pexpect.spawn('/usr/bin/scp %s@%s:%s /tmp/123/' % (user, host, files))
i = child.expect([pexpect.TIMEOUT, ssh_key, 'password: '])
if i == 0: # TIMEOUT
print 'Error!'
print 'SSH could not login, Here is what SSH said:'
print child.before, child.after
return None
if i == 1: # SSH does not have the public key, just accept it.
child.sendline('yes')
child.expect('password: ')
i = child.expect([TIMEOUT, 'password: '])
if i == 0: # TIMEOUT
print 'Error!'
print 'SSH could not login, Here is what SSH said:'
print child.before, child.after
print None
# input password
child.sendline(password)
sleep(80)
return child
def main():
# host = raw_input('Hostname:')
# user = raw_input('user:')
# password = getpass.getpass()
# files = raw_input('Enter file the absolute path:')
# for i in range(1, 10):
# host = '192.168.0.' + str(i)
# print host
host = 'yourIP'
user = 'youruser'
password = 'yourpassword'
files = '/tmp/1234.tar.gz'
child = ssh_command(files, user, host, password)
child.expect(pexpect.EOF)
print child.before
if __name__ == '__main__':
try:
main()
os.system('sh /bash/Cacti_html.sh') # 註銷掉(沒有特殊要求的)
except Exception, e:
print str(e)
#traceback.print_exc()
os._exit(1)
------------------------------------------------------------------------------------------------
cat Cacti_html.sh
#! /bin/bash
# 2013-1-23
# 你們沒有用,但是還是寫出來參考
Tar_Dir="/tmp/123"
Dir="/data/www/cacti"
rm -rf /data/www/cacti/1234/
cd $Tar_Dir
tar zxf 1234.tar.gz -C $Dir
rm -rf /tmp/123/1234.tar.gz
備註:注意os.system是調用的shell腳本,沒有特殊需要,不用這個,在這裏是用Cacti_html.sh做了一個處理。
run: python2.6 script-name
object-python-交互式登錄(scp/ssh)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
Python3高级核心技术97讲
wy53780
2020-04-23 15:37:39
python網絡爬蟲
aabcdqt
2019-05-06 20:35:05
Python與家國天下
豌豆花下貓
2019-02-24 22:22:40
seaborn 安裝成功 + ImportError: DLL load failed: 找不到指定的模塊 問題解決
耗子
2019-02-24 19:55:04
使用 Sphinx 撰寫技術文檔並生成 PDF 總結
qiwihui
2019-02-24 16:25:14
Python實現二叉樹相關算法
backbp
2019-02-24 15:35:06
Ubuntu16.04安裝Python3.7及其pip3並切換爲默認版本
ipromise
2019-02-24 14:15:00
Python2+Selenium入門05-關於WebElement類
白雪
2019-02-24 13:45:04
Python——數據類型
陳晨工程師
2019-02-24 13:43:55
Python 操作PDF庫介紹之PDFMiner
iCloudEnd
2019-02-24 13:39:59
Python reportlab 之 draw函數介紹
iCloudEnd
2019-02-24 13:39:59
Python reportlab之pdfgen 介紹(含代碼)
iCloudEnd
2019-02-24 13:39:59
Python手繪圖瞭解一下!
python攻城獅
2019-02-24 13:34:06
Python繪製圖表【matplotlib】
流域哈哈
2019-02-24 13:20:58
Python2+Selenium入門04-關於WebDriver類
白雪
2019-02-24 12:45:06