LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-11-2015, 07:39 AM   #1
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Rep: Reputation: Disabled
bash: command not found error


Hi guys!
I am writing a script for changing one user's password on all servers he/she registered on. I used ssh and sudo su - to connect the machine from ip list. (root cannot establish ssh connection due to default settings (prohibited)). Script is just line below:

#password_change.sh:

password_change(){

filename="$1"

read -p 'Username: ' uservar

read -sp 'New Password: ' passvar

for i in $(cat $1);

do

ssh -vvv -p 44 $uservar@$i 'sudo su -' | echo -e "$passvar\n$passvar" | passwd $uservar


done < "$filename"
}
password_change ip.txt

ip.txt file includes the remote machines' ip addresses. When I run this script there is no problem about establishing a connection with ssh and becoming root with sudo su -. It gave me the error below:

-bash: line 1: ip_address_1: command not found

Why is it perceiving the text file's content as command ?

Thanks in advance.

Last edited by Revenge7; 06-11-2015 at 07:41 AM.
 
Old 06-11-2015, 08:13 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Can't tell. It could depend on what is in the ip.txt file...

I also suggest using the code blocks so that things are easier to read. HTML formatting can remove things that are important.
 
Old 06-11-2015, 08:21 AM   #3
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
Can't tell. It could depend on what is in the ip.txt file...

I also suggest using the code blocks so that things are easier to read. HTML formatting can remove things that are important.
It just includes ip addresses like;

10.38.47.19
125.36.28.23
34.21.67.88
 
Old 06-11-2015, 08:25 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by Revenge7 View Post
It just includes ip addresses like;

10.38.47.19
125.36.28.23
34.21.67.88
So why do you have:
Code:
done < "$filename"
Because $filename is the same.

What commands are supposed to be executed by the "su -"? There are none listed.

Have you tried using the "-vx" to see what is happening?
 
Old 06-11-2015, 08:47 AM   #5
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
So why do you have:
Code:
done < "$filename"
Because $filename is the same.

What commands are supposed to be executed by the "su -"? There are none listed.

Have you tried using the "-vx" to see what is happening?
done < "$filename" is used for redirecting, when I delete "$filename" the script is hanged on and not finished.

sudo su - is used for passwd command to change password of user because of I cannot directly connect as root with ssh.

Do you mean verbose output ? (-vvv)

debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug3: start over, passed a different list publickey,gssapi-with-mic,password
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug3: no such identity: /root/.ssh/identity
debug1: Offering public key: /root/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug2: input_userauth_pk_ok: SHA1 fp 5a:18:60:05:db:53:69:db:c5:9e:79:8b:be:09:24:ea:ab:ef:f9:f9
debug3: sign_and_send_pubkey
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
debug2: fd 4 setting O_NONBLOCK
debug2: fd 5 setting O_NONBLOCK
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug1: Sending environment.
debug3: Ignored env HOSTNAME
debug3: Ignored env TERM
debug3: Ignored env SHELL
debug3: Ignored env HISTSIZE
debug3: Ignored env USER
debug3: Ignored env LS_COLORS
debug3: Ignored env PATH
debug3: Ignored env MAIL
debug3: Ignored env PWD
debug3: Ignored env INPUTRC
debug3: Ignored env JAVA_HOME
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug3: Ignored env HOME
debug3: Ignored env SHLVL
debug3: Ignored env LOGNAME
debug3: Ignored env LESSOPEN
debug3: Ignored env DISPLAY
debug3: Ignored env G_BROKEN_FILENAMES
debug3: Ignored env HISTTIMEFORMAT
debug3: Ignored env _
debug1: Sending command: sudo su -
debug2: channel 0: request exec confirm 0
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel 0: read<=0 rfd 4 len 0
debug2: channel 0: read failed
debug2: channel 0: close_read
debug2: channel 0: input open -> drain
debug2: channel 0: ibuf empty
debug2: channel 0: send eof
debug2: channel 0: input drain -> closed
debug2: channel 0: rcvd ext data 46
-bash: line 1: ip_address1: command not found
debug2: channel 0: written 46 to efd 6
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: channel 0: close_write
debug2: channel 0: output drain -> closed
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug3: channel 0: status: The following connections are open:
#0 client-session (t4 r0 i3/0 o3/0 fd -1/-1 cfd -1)

debug3: channel 0: close_fds r -1 w -1 e 6 c -1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 0.1 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 127

By the way, after this process the script modified the password for local server, not remote server. I just understand that it connected to remote server, then connection is closed and passwd command is worked for local server only.

Last edited by Revenge7; 06-11-2015 at 09:00 AM.
 
Old 06-11-2015, 09:29 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
you need to start your script with:
Code:
#!/bin/bash
set -vx
.....
And you will see what's happening
 
Old 06-11-2015, 09:44 AM   #7
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Original Poster
Rep: Reputation: Disabled
After set -vx

password_change ip.txt
+ password_change ip.txt
+ filename=ip.txt
+ read -p 'Username: ' uservar
Username: user1
+ read -sp 'New Password: ' passvar
New Password: cat $1 ?
++ cat ip.txt ?
+ for i in '$(cat $1)'
+ ssh -vvv -p 44 user1@ip_address1 'sudo su -'
+ echo -e 'newpassword\nnewpassword'
+ passwd user1

I think the lines I put a question marks are causing the problem.
 
Old 06-11-2015, 09:49 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
Code:
ssh -vvv -p 44 $uservar@$i 'sudo su -' | echo -e "$passvar\n$passvar" | passwd $uservar

#in this case only the first part is executed on $i, the echo and passwd will be executed on the localhost.
#You ought to write:
ssh $uservar@$i 'the whole command with pipes'
but I'm not really sure if passwd accepts input from stdin at all.
 
Old 06-11-2015, 10:05 AM   #9
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Original Poster
Rep: Reputation: Disabled
After I wrote the whole command in quotes,

(current) UNIX password: debug2: channel 0: written 25 to efd 6
debug2: channel 0: rcvd ext data 46
-bash: line 1: 10.11.92.94: command not found
debug2: channel 0: written 46 to efd 6
debug2: channel 0: rcvd ext data 48
passwd: Authentication token manipulation error
debug2: channel 0: written 48 to efd 6
Changing password for user e_kirtiloglu.
Changing password for e_kirtiloglu
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: channel 0: close_write
debug2: channel 0: output drain -> closed
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug3: channel 0: status: The following connections are open:
#0 client-session (t4 r0 i3/0 o3/0 fd -1/-1 cfd -1)

debug3: channel 0: close_fds r -1 w -1 e 6 c -1
debug1: fd 0 clearing O_NONBLOCK
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 2.3 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 1

I just returned to beginning. I could not solve the auth. manip. error with both

mount -o remount,rw /

and cp shadow- shadow

solutions stated on forum, but it did not work.
 
Old 06-11-2015, 10:07 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
would be nice to see what have you tried exactly (that is your new script)
 
Old 06-11-2015, 01:01 PM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
passwd requires a terminal by default, but if you use "passwd --stdin"

you might try using "ssh -t" ...

And drop the -vvv. That is for debugging ssh authentication/login. For anything else it is just noise.

Using the man pages would help you understand what is going on.

And use code blocks for any listings. It makes it much easier to read.
 
Old 06-11-2015, 01:07 PM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by pan64 View Post
Code:
ssh -vvv -p 44 $uservar@$i 'sudo su -' | echo -e "$passvar\n$passvar" | passwd $uservar

#in this case only the first part is executed on $i, the echo and passwd will be executed on the localhost.
#You ought to write:
ssh $uservar@$i 'the whole command with pipes'
but I'm not really sure if passwd accepts input from stdin at all.
What would be clearer would be:
Code:
echo "$passvar\n$passvar" | ssh -p 44 $uservar@$i "sudo su - passwd --stdin $uservar"
Which is shorter and easier to read. The --stdin option on passwd is designed to accept the password from a pipe.

What I don't know is if that pipe has to include the name twice. I suspect it doesn't but have not tried it out.

I just noticed another problem. The ssh is logging in using the users account, which would require the users authentication - meaning it will need the old password before being able to sudo (which it likely would NOT have permission to do as that would be allowing the user to change ANY password at a minimum, and since it is using "su -" that means it would be granting root to anyone).

The login to the remote host must use the administrators login... That means
the ssh command would have to be:
Code:
echo "$passvar" | ssh -p 44 $i "sudo su - passwd --stdin $uservar"
And if additional quotes are needed for the su command then
Code:
echo "$passvar" | ssh -p 44 $i "sudo su - 'passwd --stdin $uservar'"
would do.

Last edited by jpollard; 06-11-2015 at 01:20 PM.
 
Old 06-12-2015, 02:57 AM   #13
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
What would be clearer would be:
Code:
echo "$passvar\n$passvar" | ssh -p 44 $uservar@$i "sudo su - passwd --stdin $uservar"
Which is shorter and easier to read. The --stdin option on passwd is designed to accept the password from a pipe.

What I don't know is if that pipe has to include the name twice. I suspect it doesn't but have not tried it out.

I just noticed another problem. The ssh is logging in using the users account, which would require the users authentication - meaning it will need the old password before being able to sudo (which it likely would NOT have permission to do as that would be allowing the user to change ANY password at a minimum, and since it is using "su -" that means it would be granting root to anyone).

The login to the remote host must use the administrators login... That means
the ssh command would have to be:
Code:
echo "$passvar" | ssh -p 44 $i "sudo su - passwd --stdin $uservar"
And if additional quotes are needed for the su command then
Code:
echo "$passvar" | ssh -p 44 $i "sudo su - 'passwd --stdin $uservar'"
would do.
I am using key authentication, so there is no need to know the old password of user. Also I have no permit to ssh with root, so the command you offer below asks me to enter with root's password. I tried the below command but it gave me the same error.

ssh -t -p 44 $uservar@$i 'sudo su - | echo -e "$passvar\n$passvar" | passwd $uservar '

Pseudo-terminal will not be allocated because stdin is not a terminal.
passwd: Authentication token manipulation error
 
Old 06-12-2015, 06:00 AM   #14
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
You are using key authentication?

And you have ALL the users keys?

Also your quotes are backwards. The apostrophe quotes where you used them prevent the substitution of the user name.

You would have seen that if you had used the "-vx" as you were told to.

Did you try the last entry I suggested?

Code:
echo "$passvar" | ssh -p 44 $i "sudo su - 'passwd --stdin $uservar'"
This uses YOUR credentials to go to the server specified.

Your example uses "$uservar@$i" which requires you to have the users credentials...

Last edited by jpollard; 06-12-2015 at 06:06 AM.
 
Old 06-12-2015, 07:24 AM   #15
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
You are using key authentication?

And you have ALL the users keys?

Also your quotes are backwards. The apostrophe quotes where you used them prevent the substitution of the user name.

You would have seen that if you had used the "-vx" as you were told to.

Did you try the last entry I suggested?

Code:
echo "$passvar" | ssh -p 44 $i "sudo su - 'passwd --stdin $uservar'"
This uses YOUR credentials to go to the server specified.

Your example uses "$uservar@$i" which requires you to have the users credentials...
There is no problem about key authentication. I've already used -vx and tried your suggestion. The result is;

+ password_change ip.txt
+ filename=ip.txt
+ read -p 'Username: ' uservar
Username: user1
+ read -sp 'New Password: ' passvar
New Password: cat $1
++ cat ip.txt
+ for i in '$(cat $1)'
+ echo xxxxxxx (new password)
+ ssh -t -t -p 44 user1@ip_address1 'sudo su - '\''passwd --stdin user1'\'''

tcgetattr: Invalid argument
su: user passwd --stdin user1 does not exist
Connection to ip_address1 closed.

Thank you fyi.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
please help to fix the bash command not found error in tinyos mahewish Linux - Newbie 6 09-26-2012 07:52 AM
[SOLVED] command not found error when running my bash script thras0 Programming 3 07-28-2012 03:43 PM
[SOLVED] Centos 5 Error :- -bash: yum: command not found sachinsud Linux - Server 56 06-21-2012 08:41 PM
When logging into Xserver I get a bash command not found error. AaronKillingsworth Slackware 3 12-29-2009 11:20 PM
bash: command not found error ksgill Linux - Newbie 10 07-01-2003 04:47 PM


All times are GMT -5. The time now is 02:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration