Jghost

jeiao, In me the tiger sniffs the rose !

导航

ubuntu sudo自动切换root 无需输入密码

个人PC 用ubuntu12.04 lts版  在终端操作的时候切换root 每次都需要输入密码, 这个是个很烦人的事情, 在网上找发现了一个更好的方法  通过执行脚本 自动切换root.

这个使用的软件是expect.

sudo apt-get install expect

然后开始写我们的代码.

vi changeRoot.sh

#!/usr/bin/expect  -f  

spawn sudo -s   
send "123456\r"       
interact

这个的密码是123456, 可以根据个人修改.

给文件加可执行权限

chmod +x changeRoot.sh
./changeRoot.sh //执行这个 切换root.

每次执行./changRoot.sh 也挺麻烦的 ,可以下在快捷命令里.

.bashrc 文件里最后一行填写以下代码

alias ss='./home/geiao/changeRoot.sh';
source .bashrc

下次在终端 直接 ss 回车 就切换root 了  so easy!  

 

另外提供自动ssh连接的方法:

autossh.sh:

#!/usr/bin/expect -f set user [lindex $argv 0 ] set ip [lindex $argv 1 ] set password [lindex $argv 2 ] spawn ssh $user@$ip expect { "*yes/no" { send "yes\r"; exp_continue} "*password:" { send "$password\r" } } interact

执行./autossh.sh username ip password

如果主机IP固定 可以个性定制:

#!/usr/bin/expect -f 

 set ip 192.168.3.29  
 set password 123456    
 spawn ssh jghost@$ip  
 expect {  
 "*yes/no" { send "yes\r"; exp_continue}  
 "*password:" { send "$password\r" }  
 }  
 interact

 也可以将这写执行的命令添加到.bashrc中  快捷执行.

 

参考:http://blog.51yip.com/linux/1462.html

 

 

 

 

 

 

 

 

 

posted on 2013-09-14 10:31  Jghost  阅读(603)  评论(0编辑  收藏  举报