我想在shell中切换用户而不用输入密码,应该怎么做?

干饭人之路 2009-05-13 08:51:24
su - 用户名
老是要提示输入密码
...全文
1670 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
LeMingFei 2021-06-28
  • 打赏
  • 举报
回复

/etc/passwd里用户对应的行直接把密码位去掉,

即原:test1:x:1000:1000::/home/test1:/bin/bash

改为:test1::1000:1000::/home/test1:/bin/bash
 

小马迷途 2009-07-14
  • 打赏
  • 举报
回复
我也问个弱的:
windows下怎么写个bat脚本,把密码给输入了

ssh2 user@ip

这里会提示输入密码,怎么把密码写在脚本里直接提交?

once_and_again 2009-05-13
  • 打赏
  • 举报
回复





#include <utmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <linux/limits.h>
#include <pty.h>


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 <yes/no> < password> \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: %s\n", 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 <= 0 ){
break;
}

write( fileno(stdout), buffer, ret );
fflush( stdout );

if ( buffer[ret-1] == '\n' ){
continue;
}

if ( !input_passwd ){
write( pty, passwd, strlen(passwd) );
write( pty, "\n", 1 );
write( fileno(stdout), passwd, strlen(passwd) );
input_passwd = 1;
}
#if 1
else{
write( pty, yes_no, strlen(yes_no) );
write( pty, "\n", 1 );
write( fileno(stdout), yes_no, strlen(yes_no) );
}
#endif
}

return;
}









可以试试.
puheavy123 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bzcnc 的回复:]
如果是密码是空的,就不用输入密码了,我试过,不过这样不安全
[/Quote]

密码可以是空的?????怎么设??
  • 打赏
  • 举报
回复
root要密码,你试下设置密码为空看下
bzcnc 2009-05-13
  • 打赏
  • 举报
回复
如果是密码是空的,就不用输入密码了,我试过,不过这样不安全
puheavy123 2009-05-13
  • 打赏
  • 举报
回复
[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 "$passwd\n";
exp_continue;
}
"su: incorrect password" {
send_user "su: bad user password!\n"
return 1;
}
"*]#" { }
"*]$" { }
}
return 0;
}

expect在我的空间有下载,包含了安装方法和说明,你试试看吧,不行的话再找我
puheavy123 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 ecardttt 的帖子:]
su - 用户名
老是要提示输入密码
[/Quote]

从root切换到其他用户就可以不输入密码

如果你真的不想输入密码,只能通过expect来实现
morris88 2009-05-13
  • 打赏
  • 举报
回复
从 root 切换到其他普通用户倒是不用输入口令的...
morris88 2009-05-13
  • 打赏
  • 举报
回复
呵呵,貌似不支持...

19,610

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧