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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LunaW/article/details/50426855
需要安装putty,用到的命令是plink:
  1. PuTTY Link: command-line connection utility  
  2. Unidentified build, Jun 23 2015 11:43:58  
  3. Usage: plink [options] [user@]host [command]  
  4.        ("host" can also be a PuTTY saved session name)  
  5. Options:  
  6.   -V        print version information and exit  
  7.   -pgpfp    print PGP key fingerprints and exit  
  8.   -v        show verbose messages  
  9.   -load sessname  Load settings from saved session  
  10.   -ssh -telnet -rlogin -raw -serial  
  11.             force use of a particular protocol  
  12.   -P port   connect to specified port  
  13.   -l user   connect with specified username  
  14.   -batch    disable all interactive prompts  
  15. The following options only apply to SSH connections:  
  16.   -pw passw login with specified password  
  17.   -D [listen-IP:]listen-port  
  18.             Dynamic SOCKS-based port forwarding  
  19.   -L [listen-IP:]listen-port:host:port  
  20.             Forward local port to remote address  
  21.   -R [listen-IP:]listen-port:host:port  
  22.             Forward remote port to local address  
  23.   -X -x     enable / disable X11 forwarding  
  24.   -A -a     enable / disable agent forwarding  
  25.   -t -T     enable / disable pty allocation  
  26.   -1 -2     force use of particular protocol version  
  27.   -4 -6     force use of IPv4 or IPv6  
  28.   -C        enable compression  
  29.   -i key    private key file for authentication  
  30.   -noagent  disable use of Pageant  
  31.   -agent    enable use of Pageant  
  32.   -m file   read remote command(s) from file  
  33.   -s        remote command is an SSH subsystem (SSH-2 only)  
  34.   -N        don't start a shell/command (SSH-2 only)  
  35.   -nc host:port  
  36.             open tunnel in place of session (SSH-2 only)  
  37.   -sercfg configuration-string (e.g. 19200,8,n,1,X)  
  38.             Specify the serial configuration (serial only)  

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

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

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

ssh登陆运行ls命令
echo yplink -ssh [email protected] -pw password ls

ssh登陆依次执行command.txt的命令:
echo y| plink -ssh [email protected] -pw password -m command.txt

telnet登陆运行ifconfig并退出:
echo 'ifconfig ;exit'| plink -telnet 192.168.1.1
 
另外还有   -ssh -telnet -rlogin -raw -serial

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

学无止境

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

猜你喜欢

转载自blog.csdn.net/LunaW/article/details/50426855