2

hi i am trying to do this as mentioned in the answer using perl but it's not working.

#!/usr/bin/perl -w
$pipe="data";
$username="mukesh";
$password="mukesh";
open my $pipe, '|chpasswd' or die "can't open pipe: $!";
print {$pipe} "$username:$password";
close $pipe

[root@testgfs2 user_mgmt]# ./tess3.sh
Name "main::pipe" used only once: possible typo at ./tess3.sh line 2.
chpasswd: line 1: line too long
chpasswd: error detected, changes ignored

can any body suggest.whats wrong.

since --stdin is not avalable on all *nix i am trying to use this

EDIT : Is there any program in C to do this.cause that maigh be usefull for all versision if *nixes

Community
  • 1
  • 1
munish
  • 4,505
  • 14
  • 53
  • 83
  • Works for me as is. Maybe it's something with your version of `chpasswd`. Maybe it needs final `"\n"` at the end of input string, or maybe that "line too long" is about `/etc/shadow`, not your input. Does chpasswd work by itself, with `mukesh:mukesh` as input? – Anton Kovalenko Jan 18 '13 at 12:54
  • Why are you assigning a scalar value to $pipe and then trying to use it as a process handle? – Ilion Jan 18 '13 at 13:21
  • @Ilion That's not the same `$pipe`, though he probably thinks it is. It is `$main::pipe`, and it still holds the string value it was assigned. – TLP Jan 18 '13 at 13:41
  • You can also think of `expect` script. but not sure about portability. The problem with `pipe` is that, some commands (e.g. ssh) may reject stdin redirected from pipe, when they are waiting for password. – anishsane Jan 18 '13 at 14:09

2 Answers2

2

You could try this

system("$USERNAME:$PASSWORD | chpasswd");

Based off this answer

Community
  • 1
  • 1
samdunne
  • 306
  • 1
  • 2
  • 14
0

The code is okay as you've written it works fine for me. Try checking the quotes matching and I don't think it's necessary to have the file in.sh extension since you have #!/usr/bib/perl

Gideon Maina
  • 849
  • 10
  • 25