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.
# 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:
# 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!