Bash
Telnet BASH 腳本 - 某些命令已用完 telnet 會話
我正在執行一個 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
有什麼建議嗎?
為了回答您的確切問題,以下命令使用反引號:
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'
對於遠端工作,看看 nc 或期待。nc 用於更簡單的工作,並期望用於更複雜的互動互動。nc 要簡單得多,您可以確定沒有控制命令發送到遠端系統。
從安全的角度來看,在網路上使用 telnet 也不是最好的想法,更不用說在 Internet 上,因為內容以明文形式傳輸並且很容易被擷取。
我強烈建議使用 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"