Wednesday, July 26, 2017

Python 3 + Paramiko UPDATE

I can hear the voice over from Unreal Tournament. back in the day, "MU MU MU MULTIKILL!"

I figured out the correct syntax for my SSH/paramiko script thus completing my second actual Python script.  WOOT. (With vast amounts of help from the guys in an IRC channel I frequent)

So my hangup on all of this boiled down to trying to use a Python2 command in Python3.. Ok well two instances of that: raw_input() was changed to just input().  And my print stdout() should have been print(stdout.read())
Once I got those values changed she ran like a well-oiled machine.  Here's what I've been working on:


import sys, paramiko, getpass


user = input("Username:") #Prompts for username#passwd = getpass.
getpass("Password for " + user + ":") #Prompts for password
key_file = input("Publickey full path:") #Prompts for full path to 
key file
name = input("Target Hostname:") #Prompt for target 
hostnamec
command = input("Enter target command:") #Prompt for 
remote command

#Calling the paramiko ssh function
ssh = paramiko.SSHClient() 
#Define value 'ssh' as calling the paramiko.sshclient
print('calling paramiko')ssh.set_missing_host_key_policy(paramiko.
AutoAddPolicy()) #Must come before connect line to add hosts 
to known_hosts#ssh.connect("name", username="user",
password="passwd")
ssh.connect(hostname = name, username = user, key_filename = 
key_file) # key_filename means to specify, 
pkey on the#other hand means to actually paste in the key text 
itself 
print('trying to connect')
ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command (command)
print(stdout.read())
#ssh.close()

I will keep this, if anything as a paramiko snippet.  It works, it shows me how to SSH into a remote host and run a command.  At this early stage, I can't ask for much more.

No comments:

Post a Comment