EDIT: Thanks to the two users below, my script is now working. I have updated my code below for anyone who is looking to use Python/Paramiko prompt a user for username/password and SCP files to destinations.
I'm working on a small automation tool to automate the delivery of update packages via SCP from a Linux server. The idea is to create a txt file ('hostfile.txt') that consists of 10-20 IP addresses, and then have the python script run through the list and open an SSH/SCP session, in sequential order, on the list, and deliver the package.
Note: This is my first actual piece of python programming outside of a few small projects in a guide book. Feel free to provide helpful suggestions to improve the code :)
I clearly haven't mastered the concept of while loops in python. The script works when I have explicitly defined the destination IP located in the
ssh_client.connect(hostname=server,username=user,password=passw)
line, but not within the while loop.
Below is the entire script, in the event anyone from the future wants to use this:
## 29 July 2019
## This script is used for automating the transfer of files to remote devices.
import os
import getpass
import paramiko
import logging
from scp import SCPClient
from pprint import pprint
os.system("clear")
print("############################################")
print(" PROGRAM SCP to IOS - Script 29 Jul 2019 ")
print("############################################")
#user=('silkyjohnson')
user=raw_input("what is your user name?: ")
passw=getpass.getpass("What is your password?: ")
##Source file selection:
dirlist = os.listdir("/scp/")
pprint(dirlist)
src=raw_input("Which file would you like to transfer?: ")
##Destination selection:
dst=raw_input("Please enter destiantion path: ")
##Summary:
print("############################################")
print("You have chosen to load "+src+" to "+dst)
print("The above choices will be appled to the following devices: ")
with open('/scp/hostfile.txt', 'r') as hf:
print(hf.read())
hf.close()
### ssh session
with open('/scp/hostfile.txt', 'r') as hf:
ips = hf.readlines()
for ip in ips:
ssh_client =paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ips,username=user,password=passw)
##scp
scp = SCPClient(ssh_client.get_transport())
scp.put(src, recursive=True, remote_path=dst)
I expect the script to run through the IP's of the list, but I have grown frustrated with these trials, which is why I created an account. I'd be forever grateful if someone could walk me through this :)
Forgot to include error code, sorry!
Traceback (most recent call last):
File "iosupdate.py", line 50, in <module>
ssh_client.connect(hostname=ips,username=user,password=passw)
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 296, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 200, in _families_and_addresses
addrinfos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
TypeError: getaddrinfo() argument 1 must be string or None
Could you try ips = hf.readlines()
to read lines from the file and see if it works?