I needed a quick and dirty script to check if I had an account and valid password on a list of servers. Instead of manually logging on to each server to confirm my access, I created a Python script using Paramiko. The script reads in a text file with a list of servers and then logs on to each server and tests if I am able to successfully log on with my user account. In order for the script to execute successfully, you need to have Parmiko installed in addition to python.
Script:
import sys
import time
import paramiko
import getpass
my_id = 'johnsmith'
my_password = getpass.getpass('Password:')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
out_file = open('connection_results.txt','w')
in_file = open('list_of_servers.txt', 'r')
for host in in_file:
host=host.strip()
print ("Checking server",host)
try:
ssh.connect(host, username=my_id, password=my_password)
terminal = ssh.invoke_shell()
terminal.send('junk')
terminal.send('\n')
time.sleep(5)
print (terminal.recv(9999).decode('utf-8'))
command = 'hostname'
(stdin, stdout, stderr) = ssh.exec_command(command)
for line in stdout.readlines():
print ("Connected to",line)
out_file.write("connected to " + line + "\n")
terminal.send('exit')
terminal.send('\n')
time.sleep(5)
ssh.close()
except:
out_file.write("Could not connect to " + host + "\n")
in_file.close()
out_file.close()