Open PuTTY to telnet into multiple devices using Python

Introduction

The article explains and provide python script to open Putty terminal and telnet to multiple devices in a single click. 

Prerequisites

Basic Knowledge on Python scripting and installing modules. 

Lets Start ! 

1. Install module 'pywinauto'.

Module 'pywinauto' is a predefined library to open system applications. 

  • Open python folder:

  • Write CMD in the path and enter:


 
  • Install module 'pywinauto' using CMD:


C:\Users\pashukl2\AppData\Local\Programs\Python\Python39\Scripts>pip install pywinauto


2. Create an excel file and save it as .csv 


   
 

3. Python Script: 

Import Modules.

'pywinauto.application' module is used for opening an existing application on the system. 
'time' module for adding sleep time when pushing data to telnet sessions. 
'csv' module for opening and reading CSV files.

from pywinauto.application import Application
import time
import csv

 

Convert CSV file into Dictionary and storing network device credentials into variables called as x,y,z and w. 

 
dictionary = {}

with open(r"C:\Users\pashukl2\Desktop\Telnet1.csv") as f1:
    csv_file = csv.DictReader(f1)
   
for row in csv_file:
        dictionary =
dict(row)
       
print(dictionary)
        x = dictionary[
"IP"]
        y = dictionary[
"Username"]
        z = dictionary[
"Password"]
        w = dictionary[
"Enable Password"]


Following code will open PuTTY application, telnet to IP address and will start inserting credentials automatically. 

        app = Application().Start(cmd_line=u'putty telnet@'f"{x}")
        putty = app.PuTTY
        time.sleep (
1)
        putty.TypeKeys (y)
        putty.TypeKeys (
"{ENTER}")
        time.sleep (
1)
        putty.TypeKeys (z)
        putty.TypeKeys (
"{ENTER}")
        time.sleep (
1)
        putty.TypeKeys (
"en")
        putty.TypeKeys (
"{ENTER}")
        putty.TypeKeys (w)
        putty.TypeKeys (
"{ENTER}")

 

4. Complete Python Code

import time
from pywinauto.application import Application
import csv

dictionary = {}

with open(r"C:\Users\pashukl2\Desktop\Telnet1.csv") as f1:
    csv_file = csv.DictReader(f1)
   
for row in csv_file:
        dictionary =
dict(row)
       
print(dictionary)
        x = dictionary[
"IP"]
        y = dictionary[
"Username"]
        z = dictionary[
"Password"]
        w = dictionary[
"Enable Password"]

        app = Application().Start(
cmd_line=u'putty telnet@'f"{x}")
        putty = app.PuTTY
        time.sleep (
1)
        putty.TypeKeys (y)
        putty.TypeKeys (
"{ENTER}")
        time.sleep (
1)
        putty.TypeKeys (z)
        putty.TypeKeys (
"{ENTER}")
        time.sleep (
1)
        putty.TypeKeys (
"en")
        putty.TypeKeys (
"{ENTER}")
        putty.TypeKeys (w)
        putty.TypeKeys (
"{ENTER}")    


5. Run the script






  • Debugger Output

C:\Users\pashukl2\AppData\Local\Programs\Python\Python39\python.exe C:/Users/pashukl2/IdeaProjects/begng/20/4/21.py
{'IP': '10.197.211.62', 'Username': 'cisco123', 'Password': 'cisco123', 'Enable Password': 'cisco'}
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:17: DeprecationWarning: Method .Start() is deprecated, use .start() instead.
  app = Application().Start(cmd_line=u'putty telnet@'f"{x}")
C:\Users\pashukl2\AppData\Local\Programs\Python\Python39\lib\site-packages\pywinauto\application.py:1085: UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)
  warnings.warn(
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:20: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (y)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:21: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:23: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (z)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:24: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:26: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("en")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:27: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:28: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (w)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:29: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:17: DeprecationWarning: Method .Start() is deprecated, use .start() instead.
  app = Application().Start(cmd_line=u'putty telnet@'f"{x}")
C:\Users\pashukl2\AppData\Local\Programs\Python\Python39\lib\site-packages\pywinauto\application.py:1085: UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)
  warnings.warn(
{'IP': '10.197.210.39', 'Username': 'cisco', 'Password': 'cisco', 'Enable Password': 'cisco'}
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:20: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (y)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:21: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:23: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (z)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:24: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:26: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("en")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:27: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:28: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (w)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:29: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
{'IP': '10.197.211.98', 'Username': 'cisco', 'Password': 'cisco', 'Enable Password': 'cisco'}
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:17: DeprecationWarning: Method .Start() is deprecated, use .start() instead.
  app = Application().Start(cmd_line=u'putty telnet@'f"{x}")
C:\Users\pashukl2\AppData\Local\Programs\Python\Python39\lib\site-packages\pywinauto\application.py:1085: UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)
  warnings.warn(
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:20: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (y)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:21: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:23: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (z)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:24: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:26: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("en")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:27: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:28: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys (w)
C:\Users\pashukl2\IdeaProjects\begng\20\4\21.py:29: DeprecationWarning: Method .TypeKeys() is deprecated, use .type_keys() instead.
  putty.TypeKeys ("{ENTER}")



Comments

Popular posts from this blog

Inter-VRF Multicast without unicast Inter-VRF route leaking

L2VPN EVPN VXLAN border leaf handoff with MPLS L3VPN peer