Score:0

How to get the right text with bash

bz flag

I am new in bash scripts. I try to make script who check devices ( tp-link, cisco, lynksys... ) to connect via telnet and get some info.

So the first script work with no problem:

#!/bin/sh
NOW=$(date +"%m-%d-%Y")
HOST='IPADDRES'
USER='USER'
PASSWD='PASSWORD'
CMD='enable'
CMD2='show mac address-table'
CMD3='terminal length 0'

(echo "$HOST - " ; (
    echo open "$HOST"
    sleep 1
    echo "$USER"
    sleep 1
    echo "$PASSWD"
    sleep 1
    echo "$CMD"
    sleep 1
    echo "$CMD3"
    sleep 1
    echo "$CMD2\n"
    sleep 30
    echo "exit"
    ) | telnet   ) > name.$NOW.txt

When I make: cat name.date.txt I get this resulst


hostname1 -
telnet> Trying hostname1...
Connected to hostname1.
Escape character is '^]'.
User Access Verification

Username: 
Password:








                 Welcome to BDCOM P3310C EPON OLT




hostname1>enable
hostname1#terminal length 0
hostname1#show mac address-table
        Mac Address Table (Total 311)
------------------------------------------

Vlan    Mac Address       Type       Ports
----    -----------       ----       -----
All     8479.735b.9132    STATIC     CPU
300     44d9.e776.d8b7    DYNAMIC    g0/3
300     4c5e.0cff.6dea    DYNAMIC    g0/3
300     d4ca.6d9e.3280    DYNAMIC    g0/3

So I need export just this line 1 2 and 4 its like this:

300     44d9.e776.d8b7    DYNAMIC    g0/3
300     4c5e.0cff.6dea    DYNAMIC    g0/3
300     d4ca.6d9e.3280    DYNAMIC    g0/3

I don`t need anything before "Vlan Mac Address Type Ports"

So I make this script:

cat name.date.txt | awk '$1<"ALL"{print $1"    ",$2"    ",$3"  ",$4}'

And I get this result:

hostname1    -














             -----------------------------

----     -----------     ----   -----
300     44d9.e776.d8b7     DYNAMIC   g0/3
300     0002.9b80.7f28     DYNAMIC   g0/3
300     0002.9b65.7b66     DYNAMIC   g0/3
300     4c5e.0cff.6dea     DYNAMIC   g0/3
300     d4ca.6d9e.3280     DYNAMIC   g0/3

Who to fix ?

I just export file only this info:

300     44d9.e776.d8b7     DYNAMIC   g0/3
300     0002.9b80.7f28     DYNAMIC   g0/3
300     0002.9b65.7b66     DYNAMIC   g0/3
300     4c5e.0cff.6dea     DYNAMIC   g0/3
300     d4ca.6d9e.3280     DYNAMIC   g0/3

If will be better if I can export to table or csv

Thanks for answers

terdon avatar
cn flag
Eeek! Don't use CAPS for variable names in shell scripts. Caps are used for global environment variables so using the same name can cause unexpected issues. For example, `$USER` is a global environment variable set to the current user's name so you _really_ don't want to be changing that in a script.
Petar Petrov avatar
bz flag
Yes, i don`t use USER is like admin or petar Just for here i put USER
terdon avatar
cn flag
I am talking about the names of your variables. You should use `$user` not `$USER` and `$host`, not `$HOST` etc. It is bad practice to use CAPS for shell variable names.
Petar Petrov avatar
bz flag
I understand you're right. I will fix it, thank you
Score:4
hr flag

Assuming you want to print lines from where $1 is equal to All until the end of the file using awk:

awk '$1=="All" {p=1; next} p' name.date.txt

(omit the next if you want to include the matching line).

To make the output comma separated, set the output field separator to "," and force reconstruction of the record by evaluating $1=$1 for example:

awk '$1=="All" {p=1; OFS=","; next} p {$1=$1; print}' name.date.txt
Score:1
vn flag

There are several options, but you haven't defined your clear criteria - so I'm mostly guessing here.

If the output you need is consistently in the bottom of your file, you can use tail to get the last X lines, e.g.:

tail -n 6 name.date.txt 

... will give you the last 6 lines (the table).

Vlan    Mac Address       Type       Ports
----    -----------       ----       -----
All     8479.735b.9132    STATIC     CPU
300     44d9.e776.d8b7    DYNAMIC    g0/3
300     4c5e.0cff.6dea    DYNAMIC    g0/3
300     d4ca.6d9e.3280    DYNAMIC    g0/3

Another option is to use grep to search for a pattern, e.g.:

grep 'STATIC\|DYNAMIC' name.date.txt

... will match those lines that include either the word STATIC or DYNAMIC.

All     8479.735b.9132    STATIC     CPU
300     44d9.e776.d8b7    DYNAMIC    g0/3
300     4c5e.0cff.6dea    DYNAMIC    g0/3
300     d4ca.6d9e.3280    DYNAMIC    g0/3
Petar Petrov avatar
bz flag
Hello, thanks for answer. The variant "grep 'STATIC\|DYNAMIC' name.date.txt" is perfect for me. Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.