<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kangzj &#187; useradd</title>
	<atom:link href="http://kangzj.net/tag/useradd/feed/" rel="self" type="application/rss+xml" />
	<link>http://kangzj.net</link>
	<description>记录技术和生活~</description>
	<lastBuildDate>Thu, 09 Feb 2012 00:57:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>交互式shell编程利器expect</title>
		<link>http://kangzj.net/interact-shell-expect/</link>
		<comments>http://kangzj.net/interact-shell-expect/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 03:03:53 +0000</pubDate>
		<dc:creator>kangzj</dc:creator>
				<category><![CDATA[服务器OS]]></category>
		<category><![CDATA[expect]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[spawn]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[useradd]]></category>

		<guid isPermaLink="false">http://kangzj.net/?p=1505</guid>
		<description><![CDATA[expect是交互式shell编程的利器，可以根据返回值来确定下面发送什么命令，特别好用。]]></description>
			<content:encoded><![CDATA[<p>手里有几台Linux服务器需要经常添加用户，每次都要登录到相应的机器上去添加，特别麻烦。于是想，可不可以在一台机器上写一个脚本来远程管理其它服务器呢？</p>
<p>目标首先瞄准了我熟悉的PHP-CLI，它有一个开发中的模块ssh2，可以完成相应的功能。这个不想说了，因为用了半天都不行，Bug还太多，建议大家如非必要还是不要用这个模块的好。</p>
<p>没了PHP，很迷茫，然后很幸运地发现了expect。expect是交互式shell编程的利器，可以根据返回值来确定下面发送什么命令，特别好用。我把自己编写的远程增加用户的shell跟大家分享下（需要机器装有expect，没有的自己装吧），脚本如下：<br />
<span id="more-1505"></span></p>
<pre lang="bash">
#!/usr/bin/expect
#脚本第一个参数是远程服务器IP
set IP     [lindex $argv 0]
#远程服务器用户名（通常用root）
set USER [lindex $argv 1]
#远程服务器用户名的密码
set PASSWD [lindex $argv 2]
#添加的新用户
set Nuser [lindex $argv 3]
#新用户的密码
set Npasswd [lindex $argv 4]
#用spawn启动一个ssh客户端
spawn ssh -l $USER $IP
#如果是第一次连接，要保存密钥再输入密码，如果不是第一次连接则输入密码
expect {
 "yes/no" { send "yes\r"; exp_continue }
 "password:" { send "$PASSWD\r" }
}
#如果不是root，要expect "$"，下面不讲了，很简单
expect "*#"
send "useradd -s /bin/sh -d /home/$Nuser $Nuser\r"
expect "*#"
send "passwd $Nuser\r"
expect "*password:"
send "$Npasswd\r"
expect "*password:"
send "$Npasswd\r"
expect "*#"
send "exit\r"
</pre>
<p style='text-align:left'>&copy; 2009, <a href='http://kangzj.net'>kangzj</a>. 版权所有.  </p>
<hr />
<p><small>© kangzj for <a href="http://kangzj.net">Kangzj</a>, 2009. |
<a href="http://kangzj.net/interact-shell-expect/">http://kangzj.net/interact-shell-expect/</a> |
<a href="http://kangzj.net/interact-shell-expect/#comments">50 条评论</a> |
Add to
<a href="http://del.icio.us/post?url=http://kangzj.net/interact-shell-expect/&title=交互式shell编程利器expect">del.icio.us</a>
<br/>
Post tags: <a href="http://kangzj.net/tag/expect/" rel="tag">expect</a>, <a href="http://kangzj.net/tag/linux/" rel="tag">linux</a>, <a href="http://kangzj.net/tag/shell/" rel="tag">shell</a>, <a href="http://kangzj.net/tag/spawn/" rel="tag">spawn</a>, <a href="http://kangzj.net/tag/ssh/" rel="tag">ssh</a>, <a href="http://kangzj.net/tag/useradd/" rel="tag">useradd</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://kangzj.net/interact-shell-expect/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
	</channel>
</rss>

