自动化脚本ssh以及telnet发送命令并退出(windows和linux都适用)

半兽人 发表于: 2017-11-14   最后更新时间: 2023-11-08 15:25:09  
{{totalSubscript}} 订阅, 6,883 游览

需要安装putty,用到的命令是plink:

PuTTY Link: command-line connection utility  
Unidentified build, Jun 23 2015 11:43:58  
Usage: plink [options] [user@]host [command]  
       ("host" can also be a PuTTY saved session name)  
Options:  
  -V        print version information and exit  
  -pgpfp    print PGP key fingerprints and exit  
  -v        show verbose messages  
  -load sessname  Load settings from saved session  
  -ssh -telnet -rlogin -raw -serial  
            force use of a particular protocol  
  -P port   connect to specified port  
  -l user   connect with specified username  
  -batch    disable all interactive prompts  
The following options only apply to SSH connections:  
  -pw passw login with specified password  
  -D [listen-IP:]listen-port  
            Dynamic SOCKS-based port forwarding  
  -L [listen-IP:]listen-port:host:port  
            Forward local port to remote address  
  -R [listen-IP:]listen-port:host:port  
            Forward remote port to local address  
  -X -x     enable / disable X11 forwarding  
  -A -a     enable / disable agent forwarding  
  -t -T     enable / disable pty allocation  
  -1 -2     force use of particular protocol version  
  -4 -6     force use of IPv4 or IPv6  
  -C        enable compression  
  -i key    private key file for authentication  
  -noagent  disable use of Pageant  
  -agent    enable use of Pageant  
  -m file   read remote command(s) from file  
  -s        remote command is an SSH subsystem (SSH-2 only)  
  -N        don't start a shell/command (SSH-2 only)  
  -nc host:port  
            open tunnel in place of session (SSH-2 only)  
  -sercfg configuration-string (e.g. 19200,8,n,1,X)  
            Specify the serial configuration (serial only)

本来一直用plink做隧道代理翻墙用,闲来无聊看了看帮助,不看不知道,一看吓一跳
顺利的解决了以前没有实现的功能,特来记录。

实用场景1:

产品发给用户以后发现有bug,但是安装的位置只能远程,而且数量很多,这个时候需要对产品升级怎么办,不可能一个个ssh,或者网页配置吧,那么自动化ssh发送命令就派上用途了,一个脚本循环ssh,wget固件并并升级一气呵成,之前公司就遇到过

实用场景2:

现在有1000块板子出货要贴标签,而之前MAC地址因为某种原因没记录,这个时候怎么办,也不可能一个个去登陆复制粘贴,再保存到打印机吧,一个脚本打印出来,就是这么任性

ssh登陆运行ls命令:

echo y| plink -ssh root@192.168.1.1 -pw password ls

ssh登陆依次执行command.txt的命令:

echo y| plink -ssh root@192.168.1.1 -pw password -m command.txt

telnet登陆运行ifconfig并退出:

echo 'ifconfig ;exit'| plink -telnet 192.168.1.1

另外还有-ssh -telnet -rlogin -raw -serial

基于这几个核心命令,可以尽情发挥了

测试发现ssh 最多传入 1024个字符

更新于 2023-11-08
在线,1小时前登录

查看Linux更多相关的文章或提一个关于Linux的问题,也可以与我们一起分享文章