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.

Tuesday, July 25, 2017

Python + Paramiko = SSH unhappiness :(

Been reading the book, watching random YouTube videos on Python.. reading articles and blog posts. Figure one of the best ways to learn something is by doing, so I thought and thought and what's one of the most used parts of a script?

Connecting to a remote machine!

So I went with Paramiko as it seems to be the defacto SSHing inside Python .. thing.  Following examples is still tricky.  I created a new EC2 instance for this.. churned out a short script or snippet as I like to call it.  A paramiko_snippet on using the SSH client but am having issues sending remote commands.

I can connect, and invoke_shell() returns an active channel so I'm good there.

But when sending commands, 'channel.send('command')' returns nothing but a number.. in this case a 2.. next time I ran it gave me a 3.

Maybe it's some Python 2 versus 3 issue.  It's not clear yet, but my Google-Fu is strong and I will figure it out.

channel.send('ls')
2
channel.send('ls\n')
3

Thursday, July 13, 2017

Learning Python!

It's been a while since my last post.  Just been horribly busy with work and home stuff, and being in Montana it's damn hard to stay indoors!

Anyway for the past three weeks I've devoted myself to picking up a working knowledge of Python.  So far I like it!

The IRC channel I frequent is chock full of current|ex Amazon folks, Expedia, Disney, Mojo and a few other places that I'd consider 'Enterprise' level.  More than one handful of devops live in this channel so under a few actual, real DevOps Engineers I chose to start this adventure off with a book;

Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic.

It' easy to read and follow.  I typically read some while at lunch and spend nights watching the Udemy videos that follow 'Automate The Boring Stuff with Python'.  Once the Udemy videos are done I'm considering paying for a month of Lynda.com and checking out what they have.

In any case, why did I start this? After doing the whole sysadmin thing for almost 15 years I'm feeling stagnant.  I mean sure every time you change employers or introduce some new application you are learning new things.  And while I don't want to switch paths straight into development, I do really enjoy pulling together some code and forming something new with it.

Also under advisement from the crew I chose PyCharm as an IDE.  Absolutely love it!  Very intuitive, helpful, and damn easy to learn.  Discovered virtenv's the other day and last week I wrote my first actual script and used it at work!

Anyway I've got my eyes set on a new price in town which would come with a new title.  We'll see.