- 论坛徽章:
- 0
|
本帖最后由 ccne 于 2010-04-13 16:19 编辑
python方法:
用我原来的改的,可能有问题,没测试!俺Q![](http://wpa.qq.com/pa?p=1:236730311:1)
ad.sh
- #!/bin/bash
- USER=zsf
- PASS=123456
- FILE=$1
- while read ip
- do
- pass=$PASS`echo $ip|awk -F'.' '{print $4}'`
- echo -e "\n==============================================="
- echo -e "\tRunning $ip ..."
- echo -e "===============================================\n"
- python exec.py "$ip" "$USER" "$pass" "$FILE"
- done < ip.txt
复制代码 exec.py
- #!/usr/bin/python
- # -*- coding: UTF-8 -*-
- import paramiko
- import time
- import re
- import getpass
- import sys
- IP=sys.argv[1]
- PORT=22
- USER=sys.argv[2]
- PASS=sys.argv[3]
- FILE=sys.argv[4]
- t=paramiko.Transport((IP,PORT))
- t.connect(username=USER,password=PASS)
- chan=t.open_session()
- chan.get_pty()
- chan.invoke_shell()
- chan.send('sudo su - \n')
- time.sleep(1)
- chan.send(PASS+'\n')
- chan.send("scp -r 你自己的IP:$FILE /tmp/test\n")
- time.sleep(0.5)
- chan.send("yes\n")
- time.sleep(0.5)
- chan.send("你本机的密码\n")
- time.sleep(0.5) //根据你CP文件的大小来改时间
- print chan.recv(14096)
- chan.send("chmo a+x /tmp/test;/tmp/test\n")
- time.sleep(0.5)
- print chan.recv(4096)
- chan.close()
- t.close()
复制代码 ip.txt自动KEY方法:
主要是在安装系统的时候没有把KEY做进去,一台一台的做又麻烦。
如果每台机器用户密码不一样,自己可以简单的修改一下。
我是在我原来的基础上改的,没测试!
- #!/bin/sh
- ssh-keygen -t dsa -f ~/.ssh/id_dsa -N ""
- for ip in `awk '{print $1}' ip.txt`
- do
-
- expect scp.exp $ip
- done
复制代码 scp.exp
- [code]
- #!/usr/bin/expect -f
- set ip [lindex $argv 0]
- log_file log/$ip.log
- spawn scp -r /home/zsf/.ssh/id_dsa.pub zsf@192.168.0.$ip:~/.ssh/authorized_keys
- set timeout 10
- expect {
- "yes/no" { send "yes\r";exp_continue }
- "password:" { send "123456\r" }
- }
- interact
复制代码 |
|