Skip to content
WONGCW 網誌
  • 首頁
  • 論壇
  • 微博
  • 壁紙下載
  • 免費圖床
  • 視頻下載
  • 聊天室
  • SEO工具
  • 支援中心
  • 表格製作
  • More
    • 在線名片
    • 網頁搜索
    • 天氣預報
    • 二維碼生成器
  • Search Icon

WONGCW 網誌

記錄生活經驗與點滴

shell脚本自动执行telnet命令并返回结果

shell脚本自动执行telnet命令并返回结果

2018-10-12 Comments 0 Comment

某些时候需要定时获取下设备的指定信息,一个个telnet这个时候就显的不够用了,可以用脚本方式去批量执行

只要知道如何在shell脚本中自动执行你的命令,就可以实现了批量化


 

举例:登录路由器查看某个网口的流量情况

#脚本内容[间隔时间自已看情况调整]:
#!/bin/bash
(sleep 1;echo "test";sleep 1;echo "passwd";sleep 1;echo "dis int g0/0/0";sleep 1;echo " ";sleep 1;echo "quit")|telnet 192.168.1.1


#执行过程/结果[登录和命令都是自动输入]:
[root@localhost ~]# sh telnet.sh 
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.


Login authentication


Username:test
Password:
  -----------------------------------------------------------------------------     
  User last login information:     
  -----------------------------------------------------------------------------
  Access Type: Telnet      
  IP-Address : 192.168.1.10     
  Time       : 2018-08-08 16:03:55+08:00     
  -----------------------------------------------------------------------------
<Huawei>dis int g0/0/0
GigabitEthernet0/0/0 current state : UP
Line protocol current state : DOWN
Description:HUAWEI, AR Series, GigabitEthernet0/0/0 Interface
Route Port,The Maximum Transmit Unit is 1500
Internet protocol processing : disabled
IP Sending Frames' Format is PKTFMT_ETHNT_2, Hardware address is 68cc-6e8a-9939
Last physical up time   : 2018-07-10 11:18:53 UTC+08:00
Last physical down time : 2018-07-10 11:04:15 UTC+08:00
Current system time: 2018-08-08 16:11:15+08:00
Port Mode: FORCE COPPER
Speed : 1000,  Loopback: NONE
Duplex: FULL,  Negotiation: ENABLE
Mdi   : AUTO,  Clock   : -
Last 300 seconds input rate 7159936 bits/sec, 943 packets/sec
Last 300 seconds output rate 2765752 bits/sec, 689 packets/sec
Input peak rate 208110760 bits/sec,Record time: 2018-07-24 12:18:16
Output peak rate 175132424 bits/sec,Record time: 2018-08-06 11:45:05

Input:  1694029507 packets, 1392763516032 bytes
  Unicast:         1688764340,  Multicast:               20219
  Broadcast:          5244948,  Jumbo:                       0
  Discard:                  0,  Total Error:                 0

  CRC:                      0,  Giants:                      0
  Jabbers:                  0,  Throttles:                   0
  Runts:                    0,  Symbols:                     0
  Ignoreds:                 0,  Frames:                      0

Output:  1566820622 packets, 1170387114814 bytes
  Unicast:         1566820620,  Multicast:                   0
  Broadcast:                2,  Jumbo:                   27249
  Discard:                  0,  Total Error:                 0

  Collisions:               0,  ExcessiveCollisions:         0
  Late Collisions:          0,  Deferreds:                   0

    Input bandwidth utilization threshold : 100.00%
    Output bandwidth utilization threshold: 100.00%
    Input bandwidth utilization  : 0.48%
    Output bandwidth utilization : 0.28%

<Huawei>
<Huawei>Connection closed by foreign host.

 

需要指定的信息,只用把结果导入到文本中用命令截取打印出来就行

#脚本内容:
#!/bin/bash
(sleep 1;echo "test";sleep 1;echo "passwd";sleep 1;echo "dis int g0/0/0";sleep 1;echo " ";sleep 1;echo "quit")|telnet 192.168.1.1 >route.txt

egrep "^Last|^Input|^Output" route.txt

[root@localhost ~]# sh telnet.sh 
Connection closed by foreign host.
Last physical up time   : 2018-07-10 11:18:53 UTC+08:00
Last physical down time : 2018-07-10 11:04:15 UTC+08:00
Last 300 seconds input rate 6722328 bits/sec, 965 packets/sec
Last 300 seconds output rate 3974880 bits/sec, 825 packets/sec
Input peak rate 208110760 bits/sec,Record time: 2018-07-24 12:18:16
Output peak rate 175132424 bits/sec,Record time: 2018-08-06 11:45:05
Input:  1694537633 packets, 1393198165577 bytes
Output:  1567259062 packets, 1170657942563 bytes

 

把查询的结果发送到你的邮箱

#发送结果到邮件脚本内容:
#!/bin/bash
GK="g0/0/0"

(sleep 1;echo "test";sleep 1;echo "passwd";sleep 1;echo "dis int $GK";sleep 1;echo " ";sleep 1;echo "quit")|telnet 192.168.1.1 >route.txt

egrep "^Last|^Input|^Output" route.txt> info.txt
python /shell/e.py admin@jinchuang.org 路由器-$GK-网口信息 "`cat info.txt`"

[root@localhost ~]#sh telnet.sh 
Connection closed by foreign host.
邮件发送成功

分享此文:

  • 按一下即可分享至 X(在新視窗中開啟) X
  • 按一下以分享至 Facebook(在新視窗中開啟) Facebook
  • 分享到 WhatsApp(在新視窗中開啟) WhatsApp
  • 按一下以分享到 Telegram(在新視窗中開啟) Telegram
  • 分享到 Pinterest(在新視窗中開啟) Pinterest
  • 分享到 Reddit(在新視窗中開啟) Reddit
  • 按一下即可以電子郵件傳送連結給朋友(在新視窗中開啟) 電子郵件
  • 點這裡列印(在新視窗中開啟) 列印

相關


教學資源

Post navigation

PREVIOUS
Google BBR禁用方法
NEXT
Python使用Requests下载资源获取实时下载进度

發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料。

More results...

Generic filters
Exact matches only
Search in title
Search in content
Search in excerpt
Filter by 分類
網站公告
Featured
限時免費
Windows 軟件下載
系統軟件
辦公軟件
圖像處理
影音媒體
網絡軟件
應用軟件
Mac 軟件下載
安卓軟件下載
網絡資訊
Mac資訊
Linux資訊
VPS資訊
NASA資訊
WordPress資訊
WeChat資訊
PHP資訊
教學資源
開源程序
網頁工具
SEO工具
醫療健康
其他資訊
Content from
Content to
2018 年 10 月
一 二 三 四 五 六 日
1234567
891011121314
15161718192021
22232425262728
293031  
« 9 月   11 月 »

分類

  • 網站公告
  • 限時免費
  • Windows 軟件下載
  • 系統軟件
  • 辦公軟件
  • 圖像處理
  • 影音媒體
  • 網絡軟件
  • 應用軟件
  • Mac 軟件下載
  • 安卓軟件下載
  • 網絡資訊
  • Mac資訊
  • Linux資訊
  • VPS資訊
  • NASA資訊
  • WordPress資訊
  • WeChat資訊
  • PHP資訊
  • 教學資源
  • 開源程序
  • 網頁工具
  • SEO工具
  • 醫療健康
  • 其他資訊

彙整

近期文章

  • 蘋果智慧家庭中心配備類似Pad的顯示器和機械手臂售價或達1000美元 2025-05-13
  • 研究發現人體手指皮膚浸水後產生的皺紋存在圖案的重複性 2025-05-13
  • 數十家歐盟公司起訴Google 要求其賠償120億歐元 2025-05-13
  • 華為Pura 80工程機鏡頭膜首曝仍是三角排列 2025-05-13
  • 在建大壩成功取出12層樓高混凝土芯樣 2025-05-13
  • Windows 11測試「進階設定」項目以加強對檔案總管等的控制 2025-05-13
  • SonyXperia 1 VII發布:1080P螢幕+驍龍8 Elite 售價過萬 2025-05-13
  • 研究證明有些人就是愛“抬槓” 2025-05-13
  • 消息稱日產將暫停日本部分工廠運營 2025-05-13
  • 《異塵餘生》真人劇第二季前導預告前往拉斯維加斯、已續訂第三季 2025-05-13

熱門文章與頁面︰

  • 您可以在Windows 11 24H2 中找回WordPad
  • 台積電美國三座新廠產能預訂一空蘋果、NVIDIA、AMD搶著要
  • 巴西總統盧拉見證美團簽署10億美元投資協議Keeta宣布進入巴西市場
  • 海爾Leader三筒懶人洗衣機發表國補後3,999元起
  • 2024全球晶片公司排行:NVIDIA居首英飛凌、義法半導體跌出前十
  • Manus回應開放註冊:海外用戶已取消等候名單國內產品尚未發布
  • 台積電將於明年第四季在新竹Fab 20廠生產基於2nm GAA的晶圓
  • 全球首個氮化鎵量子光源晶片發表2026年可望實現多場景驗證
  • 無毒無污染:長六改火箭成功發射遙感四十號02組衛星
  • 日本研究稱手機毀了孩子腦:成績下滑發展停滯

投遞稿件

歡迎各界人士投遞稿件到admin@wongcw.com

請提供以下資料:

1.你的名字

2.你的電郵

3.分類目錄

4.文章標題

5.文章摘要

6.文章內容

7.文章來源

 

聯繫我們

查詢,投稿,商務合作:
​admin@wongcw.com
​技術支援:
​support@wongcw.com
​客户服務:
​cs@wongcw.com

QQ群:833641851

快帆

MALUS

極度掃描

DMCA.com Protection Status

WONGCW 網誌

  • 免責聲明
  • 捐助我們
  • ThemeNcode PDF Viewer
  • ThemeNcode PDF Viewer SC
  • Events

服務器提供

本站使用之服務器由ikoula提供。

聯繫我們

查詢,投稿,商務合作:
​admin@wongcw.com
​技術支援:
​support@wongcw.com
​客户服務:
​cs@wongcw.com

QQ群:833641851

© 2025   All Rights Reserved.