我正在运行 bash 脚本以便从客户端获取一些数据。那是脚本:
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
当我运行脚本时,似乎有些命令是在 telnet 会话之外运行的
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
有什么建议吗?
答案1
对于远程作业,请查看 nc 或 Expect。 nc 用于更简单的工作,并期望更复杂的交互交互。 nc 要简单得多,您可以确定没有控制命令发送到远程系统。
从安全角度来看,通过网络使用 telnet 也不是最好的主意,更不用说通过互联网了,因为内容以明文形式传输并且很容易捕获。
我强烈建议使用expect 通过 ssh 进行对话。
来自文章:
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"
答案2
为了回答您的确切问题,以下命令使用反引号:
cmd4=`cat /proc/meminfo`
cmd5=`cat /proc/slabinfo`
cmd6=`cat /proc/modules`
因此,它们设置为在运行脚本的主机上完成分配时打印cmd4
的值,而不是您通过 telnet 进入的主机。cat /proc/meminfo
你可能想要这个:
cmd4='cat /proc/meminfo'
cmd5='cat /proc/slabinfo'
cmd6='cat /proc/modules'