当前位置: 技术问答>linux和unix
我想在shell中切换用户而不用输入密码,应该怎么做?
来源: 互联网 发布时间:2016-06-17
本文导语: su - 用户名 老是要提示输入密码 | [test1@RHEL4_U5 software]$ /tools/bin/expect su.exp test2 manifold spawn su - test2 (...............标准输出,不用输入的) Password:(................标准输出,不用输入的...
su - 用户名
老是要提示输入密码
老是要提示输入密码
|
[test1@RHEL4_U5 software]$ /tools/bin/expect su.exp test2 manifold
spawn su - test2 (...............标准输出,不用输入的)
Password:(................标准输出,不用输入的)
[test1@RHEL4_U5 software]$ cat su.exp
#!/tools/bin/expect
set user [lindex $argv 0]
set passwd [lindex $argv 1]
spawn su - $user
expect {
"Password: " {
send "$passwdn";
exp_continue;
}
"su: incorrect password" {
send_user "su: bad user password!n"
return 1;
}
"*]#" { }
"*]$" { }
}
return 0;
}
expect在我的空间有下载,包含了安装方法和说明,你试试看吧,不行的话再找我
spawn su - test2 (...............标准输出,不用输入的)
Password:(................标准输出,不用输入的)
[test1@RHEL4_U5 software]$ cat su.exp
#!/tools/bin/expect
set user [lindex $argv 0]
set passwd [lindex $argv 1]
spawn su - $user
expect {
"Password: " {
send "$passwdn";
exp_continue;
}
"su: incorrect password" {
send_user "su: bad user password!n"
return 1;
}
"*]#" { }
"*]$" { }
}
return 0;
}
expect在我的空间有下载,包含了安装方法和说明,你试试看吧,不行的话再找我
|
#include
#include
#include
#include
#include
#include
#include
#include
void do_passwd( int pty, const char *passwd, const char *yes_no );
int main( int argc, char *argv[] )
{
argv[1]="yes";
argv[2]="momentx";
int pty, slave;
char pty_name[PATH_MAX];
int ret;
pid_t child;
if ( argc !=1 ){
fprintf( stderr, "Usage: %s n", argv[0] );
exit( EXIT_FAILURE );
}
ret = openpty( &pty, &slave, pty_name, NULL, NULL );
if ( ret == -1 ){
perror( "openpty" );
exit( EXIT_FAILURE );
}
child = fork();
if ( child == -1 ){
perror( "fork" );
exit( EXIT_FAILURE );
}
else if ( child == 0 ){
close( pty );
login_tty( slave );
execl( "/usr/bin/sshfs", "sshfs","work@IP:/home/work/work ","./temp", NULL );
}
close( slave );
printf( "pty name: %sn", pty_name );
do_passwd( pty, argv[2], argv[1] );
exit( EXIT_SUCCESS );
}
void do_passwd( int pty, const char *passwd, const char *yes_no )
{
char buffer[80];
int ret;
fd_set reads;
int input_passwd = 0;
while(1){
FD_ZERO( &reads );
FD_SET( pty, &reads );
ret = select( pty+1, &reads, NULL, NULL, NULL );
if ( ret == -1 ){
perror( "select" );
break;
}
ret = read( pty, buffer, sizeof(buffer) );
if ( ret