-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparamiko_python.py
More file actions
69 lines (56 loc) · 1.48 KB
/
paramiko_python.py
File metadata and controls
69 lines (56 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
import paramiko
import os,sys,time
hostname="192.168.1.21"
username="root"
password="SKJh935yft#"
blip="192.168.1.23"
bluser="root"
blpasswd="SKJh935yft#"
tmpdir="/tmp"
remotedir="/data"
localpath="/home/nginx_access.tar.gz"
tmppath=tmpdir+"/nginx_access.tar.gz"
remotepath=remotedir+"/nginx_access_hd.tar.gz"
port=22
passinfo='\'s password: '
paramiko.util.log_to_file('syslogin.log')
t = paramiko.Transport((blip, port))
t.connect(username=bluser, password=blpasswd)
sftp =paramiko.SFTPClient.from_transport(t)
sftp.put(localpath, tmppath)
sftp.close()
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=blip,username=bluser,password=blpasswd)
#new session
channel=ssh.invoke_shell()
channel.settimeout(10)
buff = ''
resp = ''
channel.send('scp '+tmppath+' '+username+'@'+hostname+':'+remotepath+'\n')
while not buff.endswith(passinfo):
try:
resp = channel.recv(9999)
except Exception,e:
print 'Error info:%s connection time.' % (str(e))
channel.close()
ssh.close()
sys.exit()
buff += resp
if not buff.find('yes/no')==-1:
channel.send('yes\n')
buff=''
channel.send(password+'\n')
buff=''
while not buff.endswith('# '):
resp = channel.recv(9999)
if not resp.find(passinfo)==-1:
print 'Error info: Authentication failed.'
channel.close()
ssh.close()
sys.exit()
buff += resp
print buff
channel.close()
ssh.close()