Last active
September 22, 2019 08:21
-
-
Save ohumeagle/4b9f8603874507f1452bc91e903db199 to your computer and use it in GitHub Desktop.
by iSSHtool bash script, Network Engineers can connect to network devices simply and save credentials in Linux Os.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Name: SSH Client for Network Engineers | |
# Author: Saman Jalilian | |
# Mail: ohumeagle@gmail.com | |
# introduction: This simple script helps you to connect to your network devices with SSH and let you save your passwords as an enrypted file. | |
# - the session log will be generated day by day in the root directory which scrip runs. | |
# Usage: put your device information and credentials in root directory file with .conf extention (you can have multiple file) in below format and then encrypt it | |
# With gpg tool, so the final encrypted extension should be like this *.conf.pgp | |
# use this command to encrypt your file: gpg --yes --batch --passphrase=123 -c testfile.conf | |
# Format: | |
# Number,Device_name,IP_address,Username,Password | |
# Sample Format: | |
# 1,SW-HA-D1,192.168.0.1,username,P@ssw0rd | |
# 2,R1-DE-CE,192.168.0.2,username,P@ssw0rd | |
# 3,R2-DC-DC,192.168.0.3,username,P@ssw0rd | |
# 4,FW-HJ-SD,192.168.10.4,username,P@ssw0rd | |
# Tested on Ubuntu 18.04 | |
trap "rm *.conf 2> /dev/null ;rm temp_file 2> /dev/null ; exit" 2 | |
clear | |
########### Printing Banner ############### | |
echo "#################################################################" | |
echo "iSSH (interactive SSH tool) for Network Engineers" | |
echo "Author:Saman Jalilian" | |
echo "Mail:ohumeagle@gmail.com" | |
echo "#################################################################" | |
echo "" | |
########### End of Printing Banner ######## | |
########### Funding conf.pgp files in . directory ############### | |
ls -1 | grep .conf.gpg > temp_file | |
input=(`cat "temp_file"`) | |
while IFS= read -r line ; do | |
files_name+=($line) | |
done < "temp_file" | |
########### End of Funding conf.pgp files in . directory ############### | |
########### Printing Menu and save inventory files in dictionary ############### | |
s_id=0 | |
declare -A dic | |
for i in "${files_name[@]}"; do | |
s_id=`echo $(( $s_id+1 ))` | |
dic[ $s_id ]=$i | |
echo "$s_id --> ${dic[ $s_id ]}" | |
done | |
########### End of Printing Menu and save inventory files in dictionary ############### | |
echo "" | |
file_id=0 | |
x=${#dic[@]} #getting total length of dictionary | |
var1=0 #loop control variable | |
while [[ var1 -eq 0 ]] ; do | |
read -p "Enter the Inventory file numeber: " file_id | |
if [[ file_id -le x ]]; then | |
var1=1 | |
else | |
echo "Entered out of range" | |
fi | |
done | |
f_id=1 | |
while [[ f_id -ne 0 ]] ; do | |
read -s -p "Enter the Passphrase: " passid | |
gpg --yes --batch --passphrase=$passid ${dic[ $file_id ]} | |
f_id=`echo $?` | |
done | |
conf_file_gpg=${dic[ $file_id ]} | |
conf_file_wogpg=${conf_file_gpg%.gpg} | |
#sleep 1 | |
#Reading file | |
device_sample=(`cat "$conf_file_wogpg"`) | |
#Printing the colums except password | |
echo "" | |
awk -F "," '{print $1" --> "$2" " $3" "}' $conf_file_wogpg | |
#Puting lines in dictionary | |
declare -A devices | |
d_id=0 | |
for i in "${device_sample[@]}"; do | |
d_id=`echo $(( $d_id+1 ))` | |
devices[ $d_id ]=$i | |
done | |
#getting input from user | |
echo "" | |
read -p "Enter the desired device numeber: " var1 | |
var2=${devices[ $var1 ]} | |
IFS=',' read -r -a array <<< "$var2" | |
Pass=${array[4]} | |
User=${array[3]} | |
Host=${array[2]} | |
clear | |
date=$(date +%m-%d-%y) | |
########### connecting to device with SSH ############### | |
echo "******* Connecting to ${array[1]} ..." | |
sshpass -p $Pass ssh -o ConnectTimeout=5 -oStrictHostKeyChecking=no $User@$Host | tee -a log_file_$date.log | |
echo "############################# Starting Another Session ##################################" >> log_file_$date.log | |
error=`echo $?` | |
if [[ $error -eq 255 ]]; then | |
echo "######### Device is unreachable ##########" | |
echo "##########################################" | |
else | |
echo "******* You are disconnected from ${array[1]}" | |
fi | |
# Removing Unused files | |
rm *.conf | |
rm temp_file | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment