Skip to content

Instantly share code, notes, and snippets.

@andreaswork
Created September 25, 2017 19:31
Show Gist options
  • Save andreaswork/c13b01d26b4311ea79540ab0b017abf9 to your computer and use it in GitHub Desktop.
Save andreaswork/c13b01d26b4311ea79540ab0b017abf9 to your computer and use it in GitHub Desktop.
# Sitename User IP/URL ssh_port data_dir use data_size
Rovaniemi pul pulrov.dyndns.org 22 home/pul/p 0 27400
Oulu pul puloul.dyndns.org 222 home/pul/p 0 27400
Jyvaskyla pul puljyv.dyndns.org 22 home/pul/p 0 27400
import os
import sys
import subprocess
import logging
class Data:
""" class for instance variables """
def __init__(self, data):
self.data = data
"""
def get_commands(self):
if sys.platform.startswith('linux'):
pass # do linux stuff
elif sys.platform.startswith('win32'):
pass # do windows stuff
elif sys.platform.startswith('freebsd'):
pass # do unix stuff
else:
pass # do something
def run():
HOST = "www.example.org"
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND = "uname -a"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print(sys.stderr, "ERROR: %s" % error)
else:
print(result)
def main():
"""Entry point for app script."""
try:
run()
except KeyboardInterrupt:
print('\nSee you the next time.')
except Exception:
logging.error(())
if __name__ == '__main__':
main()
from paramiko import client
""" testing stuff """
class Ssh:
client = None
def __init__(self, address, username, password):
print("Connecting to server.")
self.client = client.SSHClient()
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
self.client.connect(address, username=username, password=password, look_for_keys=False)
def sendCommand(self, command):
if(self.client):
stdin, stdout, stderr = self.client.exec_command(command)
while not stdout.channel.exit_status_ready():
# Print data when available
if stdout.channel.recv_ready():
alldata = stdout.channel.recv(1024)
prevdata = b"1"
while prevdata:
prevdata = stdout.channel.recv(1024)
alldata += prevdata
print(str(alldata, "utf8"))
else:
print("Connection not opened.")
def main(connection_info):
get_info = read_connection_data(connection_info)
run = run_cmd(get_info)
def read_connection_data(filename):
s_list = []
try:
with open(filename) as file:
for line in file:
line = line.split()
if line[0] is not '#':
s_list.append({'name': line[0], 'user': line[1], 'ip': line[2], 'port': line[3],
'data_dir': line[4], 'use': line[5], })
except Exception as e:
print(e)
finally:
return s_list
def run_cmd(stations):
for line in stations:
address = line['ip']
user = line['user']
passwd = 'pul-10'
connection = Ssh(address, user, passwd)
connection.sendCommand('ls - al')
#
if __name__ == "__main__":
station_addresses = "addresses.conf"
#header_data = glob.glob('./headers/*.txt')
main(station_addresses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment