Kitz ADSL Broadband Information
adsl spacer  
Support this site
Home Broadband ISPs Tech Routers Wiki Forum
 
     
   Compare ISP   Rate your ISP
   Glossary   Glossary
 
Please login or register.

Login with username, password and session length
Advanced search  

News:

Author Topic: Howto: execute telnet command, capture output from CLI: multiplatform,proxy,ssh  (Read 5152 times)

G.DMT

  • Member
  • **
  • Posts: 76

Hi folks,
 I am having a little trouble recovering CLI text output via telnet
and was hoping somebody in here might be able to help me out with a suggestion or 2.

 I dashed off a quick shell (bash) script to grab some broadcom stats via telnet,
reusing a simple pattern I have used with success in the past
like here: http://forum.kitz.co.uk/index.php/topic,17065.msg314608.html#msg314608

use putty profile with  plink (neatly removing the complexity of the http-connect proxy)
and sucking input from a simple text file

Quote
[user@k8 Netgear-DGN2200v3]$ cat adslctl-info-show.telnet
admin
password
adslctl info --show
exit

but the output I get back  is mangled.  :(

It seems  my telnet-fu has withered thru lack of use.  :(
cause most of my non-directly-attached cli has been  embedded/serial or remote/ssh with minicom/screen for the last decade and more !

I did wade through the manuals for telnet,putty and plink for a while, but the character-at-a-time versus 'old-line-at-once' stuff was a bit impenetrable!  :(

raw output pasted below here:

Code: [Select]
$ plink -telnet -load "DGN2200v3" < ./Netgear-DGN2200v3/adslctl-info-show.telnet
DGN2200v3 login: admin
Password: p
# adslctl info --show
�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������adslctl: ADSL driver and PHY status
Status: Showtime
Retrain Reason: 0
Last initialization procedure status:   0
Max:    Upstream rate = 1044 Kbps, Downstream rate = 4928 Kbps
Channel:        INTR, Upstream rate = 448 Kbps, Downstream rate = 4352 Kbps
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Link Power State:       L0
Mode:                   G.DMT
TPS-TC:                 ATM Mode
Trellis:                ON
Line Status:            No Defect
Training Status:        Showtime
                Down            Up
SNR (dB):        6.4             19.0
Attn(dB):        51.0            31.5
Pwr(dBm):        19.8            12.5
                        G.dmt framing
K:              137(0)          15
R:              16              16
S:              ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������1.0000                8.0000
D:              32              4

                        Counters
                        Bearer 0
SF:             216824          216838
SFErr:          244             37
RS:             14743998                1842179
RSCorr:         56840           34
RSUnCorr:       7875            0

                        Bearer 0
HEC:            6049            33
OCD:            38              0
LCD:            38              0
Total Cells:    37822078                0
Data Cells:     163286          0
Drop Cells:     0
Bit Errors:     0               0

ES:             94              0
SES:            0               0
UAS:            8022            8022
AS:             3674

                        Bearer 0
INP:            1.50            1.50
INPRein:                0               0
delay:          8               8
PER:            0.00            0.00
OR:             32.00           32.00

Bitswap:        14/14           411/411

�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������#
# �������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
��������������������������������������������������������������������������������
���������������������������������������������������������������� 

[edit:] renamed to more informative thread title.
« Last Edit: June 15, 2016, 04:06:33 PM by G.DMT »
Logged

petef

  • Reg Member
  • ***
  • Posts: 135
Re: Help with telnet CLI
« Reply #1 on: June 05, 2016, 12:48:21 PM »

You should get greater reliability driving the telnet session with expect or one of its alternatives.

https://en.wikipedia.org/wiki/Expect
Logged

G.DMT

  • Member
  • **
  • Posts: 76
Re: Help with telnet CLI
« Reply #2 on: June 15, 2016, 12:05:26 PM »

Thanks for that reply pete.

Ironically enough, I have been using expect for scripting telnet  / ssh for years, but decided a while ago as a matter of policy, to implement new scripts with something more windows / beginner friendly.

In case anyone is interested, and to preserve some knowlege for future forum searches, I shall make a note of what I found here.

Plink is a very poor solution, because the command line does not support options for using a proxy to communicate with the target device.  :(

To be clear - putty _does_ support proxies, but the user first has to create a profile in the putty gui and only then can this be called with
plink -load "$profile_name"
 
The solution to that has been to use ncat:
https://nmap.org/ncat/

A powerful, open source, multiplatform solution from the renowned networking experts who brought us nmap.

It transpires that the main issue that I encountered- the 'extra' chars in the output above
is caused by a flexibility in the telnet protocol specification, which was designed to allow transparent interoperability of a telnet session inside another telnet session.

The End Of Line convention is not rigidly specified, but rather is implementation dependant.

Easy explanation here:
Telnet End-of-Line Convention http://www.freesoft.org/CIE/RFC/1123/31.htm
 
Experimentation with various devices has led to the following solution in my scripts,
which I hope is portable and readable enough.  ;)

Code: [Select]
              # Char Oct Dec Hex Control-Key Control Action
CRLF='\015\012' # CR 15 13 d ^M Carriage Return
               # LF 12 10 a ^J
CRNUL='\015\00' # NUL 0 0 0 ^@ Null character

CR='\n'

EOL="$CRLF"
#EOL="$CRNUL"
#EOL="$CR"

Note1:
With some devices :cough:Netgear:cough:  sending the expected EOL is essential to progress past a login with username and password.

Example shell script:
Code: [Select]
# N.B plink has no options for proxy, so create a local port forward with ssh -L
# e.g. ssh -N -f -L 127.0.0.1:10023:192.168.0.3:23 -p 22 userssh@192.168.4.118
# see netgear-unlock.sh function ssh_port_forward1

function plink_telnet1 () {
## Note that login fails on Netgear DGN2200v3 with EOL=CRLF and EOL=CRNUL

# e.g. plink_telnet1 '\n' '1' '1' '1' 'admin' 'password' '23' '192.168.1.10' 'command string'

    local eol="$1"
    local SLEEP_SECS0="$2"
    local SLEEP_SECS1="$3"
    local SLEEP_SECS2="$4"
   
    local telnet_user="$5"
    local telnet_pass="$6"
   
    local login_user="$telnet_user""$eol"
    local login_pass="$telnet_pass""$eol"

    local telnet_port="$7"
    local telnet_ip="$8"

    shift 8
   
    local string_quit='exit'"$eol"
   
    (echo -e "$login_user";sleep "$SLEEP_SECS0";echo -e "$login_pass";sleep "$SLEEP_SECS1"; echo -e "$@$eol";sleep "$SLEEP_SECS2";echo -e "$string_quit") | plink -telnet -l "$telnet_user" -P "$telnet_port" "$telnet_user@$telnet_ip"
}

function dgn2200v3 () {

    plink_telnet1 '\n' '1' '1' '1' 'admin' 'password' '10123' '127.0.0.1' "$@"
}

Note2:
With some devices, particularly with slow responses, unreliable networking, or large data transfers, it is adviseable to insert sufficient pauses that each transaction can complete, before the script begins the next, or data _will_ be lost.

Happy hacking!  ;)
« Last Edit: June 15, 2016, 04:32:27 PM by G.DMT »
Logged

G.DMT

  • Member
  • **
  • Posts: 76
Re: Help with telnet CLI
« Reply #3 on: June 15, 2016, 01:09:41 PM »

By creating one shell function with the necessary parameters to access each device
 then one executeable script to call each shell function :
Code: [Select]
[user@k8 Netgear-DGN2200v3]$ cat dgn2200v3.sh
#! /bin/sh

source ./telnet-dgn2200v3.sh

dgn2200v3 "$@"

I can now execute a command on a remote modem thus:

Code: [Select]
[user@k8 Netgear-DGN2200v3]$ ./dgn2200v3.sh 'adslctl info --state'
DGN2200v3 login: admin
Password:

#
# adslctl info --state
adslctl: ADSL driver and PHY status
Status: Showtime
Retrain Reason: 1
Last initialization procedure status:   0
Max:    Upstream rate = 1028 Kbps, Downstream rate = 2336 Kbps
Channel:        INTR, Upstream rate = 448 Kbps, Downstream rate = 2048 Kbps

#
# exit
[user@k8 Netgear-DGN2200v3]$

 :)
Logged
 

anything