0

I'm running a bash script in order to get some data from a client. That is the script:

host=2.218.71.67
port=23
login=xxx
passwd=xxx
DATE=date
cmd1=ps
cmd2=dumpmdm
cmd3=sh
cmd4=`cat /proc/meminfo`
cmd5=`cat /proc/slabinfo`
cmd6=`cat /proc/modules`
cmd7=ifconfig
cmd8=`brctl show`
cmd9=`route -n`
cmd10=`cat /proc/net/arp`
cmd11=`cat /proc/net/nf_conntrack`
cmd12=`wlanconfig ath0 list`
cmd13=`wlanconfig ath0 list ap`
cmd14=top 

$DATE
(
 echo open ${host}
sleep 3
echo ${login}
sleep 1
echo ${passwd}
sleep 1
echo ${cmd1}
sleep 1
echo ${cmd2}
sleep 3
echo ${cmd3}
sleep 1
echo ${cmd4}
sleep 1
echo ${cmd5}
sleep 1
echo ${cmd6}
sleep 1
echo ${cmd7}
sleep 1
echo ${cmd8}
sleep 1
echo ${cmd9}
sleep 1
echo ${cmd10}
sleep 1
echo ${cmd11}
sleep 2
echo ${cmd12}
sleep 1
echo ${cmd13}
sleep 1
echo ${cmd14}
sleep 2
) | telnet
$DATE

When I run the script, seems that some commands are run out of the telnet session

root@raspberrypi:/etc/my_scripts# ./telnet.sh
cat: /proc/net/nf_conntrack: No such file or directory
./telnet.sh: line 27: wlanconfig: command not found
./telnet.sh: line 28: wlanconfig: command not found
Wed Nov 25 12:57:20 GMT 2015
telnet> Trying 2.218.71.67...
Connected to 2.218.71.67.
Escape character is '^]'.
BCM96348 xDSL Router
Login: ^C

root@raspberrypi:/etc/my_scripts# bash -x telnet.sh
+ host=2.218.71.67
+ port=23
+ login=admin
+ passwd=Sky.2015
+ DATE=date
+ cmd1=ps
+ cmd2=dumpmdm
+ cmd3=sh
++ cat /proc/meminfo
+ cmd4='MemTotal:         445032 kB
MemFree:           25696 kB
MemAvailable:     328996 kB
Buffers:           16848 kB
Cached:           233056 kB
SwapCached:            4 kB
Active:           147000 kB
Inactive:         169248 kB
Active(anon):      21840 kB
Inactive(anon):    44848 kB
Active(file):     125160 kB
Inactive(file):   124400 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:        102396 kB
SwapFree:         102392 kB
Dirty:                32 kB
Writeback:             0 kB
AnonPages:         66356 kB
Mapped:             8048 kB
Shmem:               344 kB
Slab:              89504 kB
SReclaimable:      84460 kB
SUnreclaim:         5044 kB
KernelStack:         952 kB
PageTables:         1700 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      324912 kB
Committed_AS:     517536 kB
VmallocTotal:     565248 kB
VmallocUsed:        1076 kB
VmallocChunk:     325400 kB
CmaTotal:           8192 kB
CmaFree:              24 kB'
++ cat /proc/slabinfo
+ cmd5='slabinfo - version: 2.1
# name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
UDPLITEv6              0      0    768   10    2 : tunables    0    0    0 : slabdata      0      0      0
UDPv6                 10     10    7

Any suggestion?

  • use expect or perl's Expect.pm or python's pexpect. – cas Nov 25 '15 at 22:24
  • Try "degenerately" long sleeps in order to rule out that the existing ones are not sufficient. That's currently my best guess, fwiw. – EdwardGarson Aug 4 '16 at 19:44
0

To answer your exact question, the following commands use backquotes:

cmd4=`cat /proc/meminfo`
cmd5=`cat /proc/slabinfo`
cmd6=`cat /proc/modules`

They thus set cmd4 to the value of what cat /proc/meminfo prints at the time the assignment is done, on the host where the script is run, rather than the one which you telnet into. You probably want this instead:

cmd4='cat /proc/meminfo'
cmd5='cat /proc/slabinfo'
cmd6='cat /proc/modules'
1

For remote jobs, have a look at nc or expect. nc for simpler jobs, and expect for more complicated interactive interactions. nc is far simpler, and you can be sure there are no control commands sent to the remote system.

From the security viewpoint, it is also not the best of the ideas to use telnet over the network, much less over Internet as the contents travel in cleartext form and are easily captured.

I would strongly advise using expect to talk over ssh.

From the article:

http://www.admin-magazine.com/Articles/Automating-with-Expect-Scripts

#!/usr/bin/expect -f
spawn ssh aspen
expect "password: "
send "PASSWORD\r"
expect "$ "
send "ps -ef |grep apache\r"
expect "$ "
send "exit\r"
  • Thanks, It is something that I' ll look into for sure :) – Federi Nov 25 '15 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.