forked from TrellixVulnTeam/Python-Automation_KTIW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_ssh.py
More file actions
47 lines (40 loc) · 1.66 KB
/
Copy pathpython_ssh.py
File metadata and controls
47 lines (40 loc) · 1.66 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
import paramiko
import sys
results = []
def ssh_conn():
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
#hostname = input("Enter the hostname:-")
# if not hostname:
# raise ValueError('Hostname is not defined')
# sshpath=input("Enter the path to your ssh key:-")
# if not sshpath:
# raise ValueError('SSH Path is not defined')
# user=input("Enter the username:-")
# if not user:
# raise ValueError('Username is not defined')
k = paramiko.RSAKey.from_private_key_file('C:/Users/gaurav.sikka01/.ssh/opensshkey')
client.connect('172.16.100.217',port=22, username='gsikka', pkey=k)
ssh_stdin, ssh_stdout, ssh_stderr = client.exec_command('ls -al; ls -l')
except IOError as e:
print("Invalid input",e)
except ValueError as e:
print(e)
except paramiko.SSHException as sshException:
print("Unable to establish SSH connection: %s" % sshException)
except paramiko.AuthenticationException:
print("Authentication Error!!")
except:
print("Unexpected error:", sys.exc_info()[0])
raise
else:
for line in ssh_stdout:
results.append(line.strip('\n'))
ssh_conn()
for i in results:
print(i.strip())
sys.exit()
#172.16.100.217
#C:/Users/gaurav.sikka01/.ssh/opensshkey