|
| 1 | +#!/usr/bin/expect |
| 2 | +#/DELL-FORCE10 |
| 3 | +# Copyright 2018, Dell EMC, Inc. |
| 4 | + |
| 5 | +############FUNCTIONS############ |
| 6 | +#access the switch CLI via Dell 'f10do' shell command and execute FTOS command |
| 7 | +proc execFtosCommand {cmd_str} { |
| 8 | + set str [exec f10do "$cmd_str"] |
| 9 | + print_output $str |
| 10 | +} |
| 11 | +########Logging Functions######## |
| 12 | +#print to terminal and to log file |
| 13 | +proc print_output {str {fd ""}} { |
| 14 | + puts $str |
| 15 | + if {$fd != ""} { |
| 16 | + puts $fd $str |
| 17 | + } |
| 18 | +} |
| 19 | +#starts logging status |
| 20 | +proc startLogging {} { |
| 21 | + global status_file |
| 22 | + global fp |
| 23 | + set status_file "/tmp/rackhd.log" |
| 24 | + #open status file |
| 25 | + set fp [open $status_file w] |
| 26 | +} |
| 27 | +#stop logging status |
| 28 | +proc stopLogging {} { |
| 29 | + global fp |
| 30 | + print_output "========================================\n" $fp |
| 31 | + close $fp |
| 32 | +} |
| 33 | +#####Prereq Check Functions###### |
| 34 | +#resets watchdog timer |
| 35 | +proc resetTimer {} { |
| 36 | + print_output [exec rstimer 30] |
| 37 | + print_output "\nReset Timer Complete!\n" |
| 38 | +} |
| 39 | +########Config Functions######### |
| 40 | +#write changes to startup-config |
| 41 | +proc doWrite {} { |
| 42 | + execFtosCommand "write" |
| 43 | + #wait 2 seconds |
| 44 | + after 2000 |
| 45 | +} |
| 46 | +#configures management interface with IP address |
| 47 | +proc setManagement {} { |
| 48 | + global fp |
| 49 | + print_output "Configuring ManagementEthernet <%=mgmtPort%> ...\n" $fp |
| 50 | + execFtosCommand "configure terminal" |
| 51 | + execFtosCommand "interface managementEthernet <%=mgmtPort%>" |
| 52 | + execFtosCommand "ip address <%=ipAddr%>" |
| 53 | + execFtosCommand "no shut" |
| 54 | + execFtosCommand "end" |
| 55 | +} |
| 56 | +#enable ssh |
| 57 | +proc enableSsh {} { |
| 58 | + global fp |
| 59 | + print_output "Configuring ssh ...\n" $fp |
| 60 | + execFtosCommand "configure terminal" |
| 61 | + execFtosCommand "ip ssh server enable" |
| 62 | + execFtosCommand "end" |
| 63 | +} |
| 64 | +#create an account |
| 65 | +proc createAccount {} { |
| 66 | + global fp |
| 67 | + print_output "Create an new account ...\n" $fp |
| 68 | + execFtosCommand "configure terminal" |
| 69 | + execFtosCommand "username <%=username%> password <%=userPassword%>" |
| 70 | + execFtosCommand "end" |
| 71 | +} |
| 72 | +#set admin password |
| 73 | +proc updateAdminPassword {} { |
| 74 | + global fp |
| 75 | + print_output "update admin password ...\n" $fp |
| 76 | + execFtosCommand "configure terminal" |
| 77 | + execFtosCommand "enable password level 15 0 <%=adminPassword%>" |
| 78 | + execFtosCommand "end" |
| 79 | +} |
| 80 | + |
| 81 | +################################# |
| 82 | +#translate CR and LF correctly |
| 83 | +fconfigure stdout -translation crlf |
| 84 | +fconfigure stderr -translation crlf |
| 85 | +#flag variables to confirm if initial check and configuration pass/succeed |
| 86 | +set checkFail 0 |
| 87 | +set configFail 0 |
| 88 | +#name to set the switch to |
| 89 | +set hostname <%=hostname%>" |
| 90 | +print_output "!!!Executing Config Script!!!\n" |
| 91 | +#set Unix watchdog timer to 30 minutes |
| 92 | +if [catch resetTimer] { |
| 93 | + print_output "Error encountered during watchdog timer reset!\n" |
| 94 | + set checkFail 1 |
| 95 | + exit 2 |
| 96 | +} |
| 97 | +#start logging |
| 98 | +if [catch startLogging] { |
| 99 | + print_output "Error encountered during start of logging!\n" |
| 100 | +} else { |
| 101 | + print_output "Status logging for bmp successfully started!\n" $fp |
| 102 | +} |
| 103 | +print_output "========================================\n" $fp |
| 104 | +print_output " Status: $hostname\n" $fp |
| 105 | +print_output "========================================\n" $fp |
| 106 | +#set the management IP address |
| 107 | +if [catch setManagement] { |
| 108 | + print_output "Error configuring management!\n" $fp |
| 109 | + set configFail 1 |
| 110 | +} |
| 111 | +#Enable SSH |
| 112 | +if [catch enableSsh] { |
| 113 | + print_output "Error enabling ssh" |
| 114 | + set configFail 1 |
| 115 | +} |
| 116 | +#create a new account |
| 117 | +if [catch createAccount] { |
| 118 | + print_output "Error creating a new account" |
| 119 | + set configFail 1 |
| 120 | +} |
| 121 | +#updating admin password |
| 122 | +if [catch updateAdminPassword] { |
| 123 | + print_output "Error updaing admin password" |
| 124 | + set configFail 1 |
| 125 | +} |
| 126 | +#write the configuration to startup-config |
| 127 | +if [catch doWrite] { |
| 128 | + print_output "Error saving configuration!\n" $fp |
| 129 | + set configFail 1 |
| 130 | +} |
| 131 | + |
| 132 | +#NOTIFY IF CONFIGURATION FAILED OR PASSED |
| 133 | +if {$configFail} { |
| 134 | + print_output "\n!!!!!Errors were encountered during configuration - check log file!!!!!\n" $fp |
| 135 | +} else { |
| 136 | + print_output "\n!!!!!Configuration Successfully Applied!!!!!\n" $fp |
| 137 | +} |
| 138 | + |
| 139 | +#stop logging to status file |
| 140 | +if [catch stopLogging] { |
| 141 | + print_output "Error upon stopping logging!\n" $fp |
| 142 | + exit 2 |
| 143 | +} |
| 144 | +#IF CONFIGURATION FAILED, EXIT ON ERROR, OR, IF PASSED, EXIT ON SUCCESS |
| 145 | +if {$configFail} { |
| 146 | + exit 2 |
| 147 | +} else { |
| 148 | + exit 0 |
| 149 | +} |
| 150 | + |
0 commit comments