-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh-test.py
More file actions
executable file
·105 lines (87 loc) · 2.49 KB
/
Copy pathssh-test.py
File metadata and controls
executable file
·105 lines (87 loc) · 2.49 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
import shutil
import sys
#from fabric.api import *
import logging
import paramiko
import os
logging.basicConfig( level=logging.INFO )
#ssh = paramiko.SSHClient()
import sys
import time
import select
import paramiko
host = '192.168.1.5'
i = 1
#
# Try to connect to the host.
# Retry a few times if it fails.
#
while True:
print "Trying to connect to %s (%i/30)" % (host, i)
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host=host,username='default',password='Carb0nDay')
print "Connected to %s" % host
break
except paramiko.AuthenticationException:
print "Authentication failed when connecting to %s" % host
sys.exit(1)
except:
print "Could not SSH to %s, waiting for it to start" % host
i += 1
time.sleep(2)
# If we could not connect within time limit
if i == 30:
print "Could not connect to %s. Giving up" % host
sys.exit(1)
# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("show mac")
# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
# Only print data if there is data to read in the channel
if stdout.channel.recv_ready():
rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
if len(rl) > 0:
# Print data from stdout
print stdout.channel.recv(1024),
#
# Disconnect from the host
#
print "Command done, closing SSH connection"
ssh.close()
'''
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('192.168.1.5', username='default', password='Carb0nDay')
channel = client.get_transport().open_session()
channel.exec_command("show mac")
while True:
if channel.exit_status_ready():
break
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
print channel.recv(1024)
#ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#ssh.connect('192.168.1.5', username='default', password='Carb0nDay')
#stdin, stdout, stderr = ssh.exec_command('show mac')
account = read_login()
conn = SSH2()
conn.connect('192.168.1.5')
conn.login(account)
#conn.execute('show mac')
#type(stdin)
#data=stdout.readlines()
for line in data:
line.translate(",", " ")
print line
#ssh.exec_command('show mac \n')
env.host_string='192.168.1.5'
env.user='default'
env.password='Carb0nDay'
env.hosts='192.168.1.5'
out=run('set vlan create 2')
#print out
#print data
'''