-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc2_ssh.py
41 lines (38 loc) · 1.15 KB
/
c2_ssh.py
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
#!/usr/bin/python
import paramiko
import json
import os
def conn(host,command,user="root",pwd=None,port=22):
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "host:%s,command:%s,user:%s,pwd:%s,port:%s" % (host,command,user,pwd,port)
PRIVATE_KEY='/root/.ssh/id_rsa'
ssh.connect(host,port,user,pwd,key_filename=PRIVATE_KEY)
stdin,stdout,stderr=ssh.exec_command(command)
error=stderr.readlines()
output=stdout.readlines()
ssh.close()
if not error:
return output
else:
errot_list=json.dumps(error)
print "An error happened by:%s" % errot_list
return error
import socket
def conn2(host,command,user="root",pwd=None,port=22):
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if socket.gethostname()=="m1.local":
PRIVATE_KEY='/Users/huming/.ssh/id_rsa'
else:
PRIVATE_KEY='/root/.ssh/id_rsa'
ssh.connect(host,port,user,pwd,key_filename=PRIVATE_KEY)
#,timeout=300
stdin,stdout,stderr=ssh.exec_command(command)
error=stderr.readlines()
output=stdout.readlines()
ssh.close()
if not error:
return output
else:
return "An _error_ happened by:%s" % error