码迷,mamicode.com
首页 > 其他好文 > 详细

远程连接主机,并用文件匹配方式创建用户

时间:2016-12-15 18:24:02      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:用户   password   expect   

#!/usr/bin/expect-f

set ip [lindex $argv 0]

set passwd [lindex $argv 1]

set path [lindex $argv 2]

set file [lindex $argv 3]

set cmd [lindex $argv 2]

 

spawn  scp$file password username root@${ip}:$path

expect "password:*"

send "${passwd}\r"

 

spawn /usr/bin/ssh root@${ip}

expect "password:"

send "${passwd}\r"

 

expect "#"

send "chmod +x $path$file\r"

expect "#"

send "$path$file  username password"

expect "#"

send "rm $path$file username password"

expect "#"

send "exit\r"

expect eof

 

 

 

#!/bin/bash

if [ -n "$1" -a -n "$2" ]

then

       if [-e "$1" -a -e "$2" ]

       then

         MAXUSER=`wc -l $1 | cut -d " " -f1`

          MAXPASS=`wc -l $2 | cut -d " " -f 1`

       ["$MAXUSER" -eq "$MAXPASS" ]&&( for NUM in $( seq 1$MAXUSER )

       do

         USERNAME= `sed -n ${NUM}p $1`

          PASSWORD= `sed -n ${NUM}p $2`

          CKUSER=`getent passwd $USERNAME`

           [ -z "$CKUSER" ] &&(

           useradd $USERNAME

              echo $PASSWORD |passwd --stdin $USERNAME

          )||echo "$USERNAME Exist !!"

       done

      )||(  echo $1 and $2 havedifferent lines )

elif [ ! -e "$1" ]

   then

   echo"ERROR:$1 is not exsit"

   else

   echo"ERROR:$2 is not exit"

   fi

else

echo "ERROR:Please input userfile and passwordfile after command !!"

fi

 

useradd MAXUSER=`wc -l $1 | cut -d " " -f 1`

passwd MAXPASS=`wc -l $2 | cut -d " " -f 1`

 

 

 


远程连接主机,并用文件匹配方式创建用户

标签:用户   password   expect   

原文地址:http://12117148.blog.51cto.com/12107148/1882898

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!