I tried to search for an answer online but found it hard to phrase the search criteria to find what I'm looking for. For starters I'm a total noob to coding/scripting.
Where I have gotten is understanding how to use the read function inside a script to create variables from user input which is great. My question is can you run a script followed by a keyword which would signify a hostname(target) device. This would look something like this.
sample.sh hostname
Currently my understanding only allows me add the hostname into a variables after the script is ran but this seems a little slower. A case of what I'd like is a generic script that checks interface status on a networking switch. It would be great to have the script setup in a way that I could run it followed by a hostname to just quickly check that single device. I think I am missing some fundamental understanding.
I hope I described that clearly, thanks for any help in advance. I have a sample bash script below and the expect script that it's passed onto
#!/bin/bash
echo "What device do you want to check TRUNK links?"
read -e host
echo "Username:"
read -e user
echo "Password:"
read -s -e password
./show-trunk.exp $host $user $password;
#!/usr/bin/expect -f
set host [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
#spawn echo "Running Script..."
log_user 0
spawn ssh $username@$devices
expect "*assword:"
send "$password\r"
expect "#"
log_user 1
send "show int desc | in TRUNK\r"
expect "#"
send "exit\r"
There are special variables in bash that signify the arguments put into them. You can specify them like this:
#!/usr/bin/env bash
echo $1 $2 $3
Then to run the script on the command line:
~> ./test.sh hello bash user
hello bash user
If you need to test for the correct number of arguments, you can do a simple if statement like this:
#!/usr/bin/env bash
if [[ "$#" -eq 3 ]]; do
echo $1 $2 $3
else
echo "Not enough arguments"
fi
Then if you call it on the command line:
~> ./test.sh hello bash user
hello bash user
~> ./test.sh oops
Not enough arguments