Visit Our UNIX and Linux User Community


Using SSH to run a "BASH" script.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using SSH to run a "BASH" script.
# 1  
Old 02-22-2011
Using SSH to run a "BASH" script.

Experts,

Hello !

Im running into something that I cant seem to find an answer to. I have a script and it does the following set of actions -

1. Cd to one specific directory
2. Look for all the files that contain a string and once a occurence of the string is found,
3. Pipe that output to awk as input and print $3.

Upto this point, I was able to do it without any issue and the script runs well.

Now, here comes the challenge. I need to run the above script on more than one machine and I just don't know how to do it.
Im sure there must be many folks out there who must be doing this on a daily basis.

Here are the following steps that I undertook
Method 1 :
1. I generated a public & private key pair and I did happen to add authroized_keys file with the contents of id_rsa.pub and they both have the same size.
2. After the above step, I ran this, ssh user@machine and strangely enough, it asks for my password AGAIN ! So, I enter that one more time assuming that I didnt correlate my userid/password combo with the machine that Im connecting to.
3. So, I try to connect to the machine again by keying ssh user@machien & it only lets me in if I entered the correct password...not the key value that I entered. This lets me believe that generating the public & private keys is not in any shape or form easing the situation.

Method 2:
Code:
while read machine user password
do
expect -c "set timeout -1;\
spawn ssh $user@$machine $1;\
match_max 100000;\
expect *password:*;\
send -- $password;\
interact;"
done < "passfile.txt

When I run the above lines, it throws the following errors -
Code:
-bash: spawn: command not found
-bash: match_max: command not found
couldn't read file "*?assword:*": no such file or directory
-bash: send: command not found
-bash: send: command not found

This makes me believe that it is not the right path that I should undertake.

Also, when I do this, "ssh user@machine ls " and after entering password, it is showing me all of the contents on machine without any issue.

Now, the admin is out of town and I honestly have no clue what Im doing other than googling stuff out and if I need any help, there is offshore but getting any real help is pretty much an exercise in pulling teeth out.

Any and all help is greatly appreciated & will be cherished.

regards,
Lee.

Last edited by Franklin52; 02-22-2011 at 04:54 AM.. Reason: Please use code tags, thank you
# 2  
Old 02-22-2011
I would analyze the problems one at a time.

1. PKI setup.

Quote:
1. I generated a public & private key pair and I did happen to add authroized_keys file with the contents of id_rsa.pub and they both have the same size.
2. After the above step, I ran this, ssh user@machine and strangely enough, it asks for my password AGAIN !
I would like to know:

1.1. Which OS are you using: local/remote.
1.2. What are the permissions of the .ssh directory and the .ssh/authorized_keys file.

Could you post the output of the following command:

Code:
ls -la "$HOME"/.ssh

# 3  
Old 02-22-2011
Agree with radoulov. First you need to generate keys to make your ssh passwordless across machines. Once you are done with this then you can write a script that does ssh to different machines and perform certain actions.
# 4  
Old 02-22-2011
Experts,

Here are the answer to your questions. Still I havent made any progress on this.

Please advise !

regards,
Lee.

Code:
$ ls -la "$HOME"/.ssh

drwx------    6 leemc     staff   204 Feb 22 00:15 .
drwxrwxrwx  167 leemc  staff  5678 Feb 22 00:46 ..
-rw-------    1 leemc     staff   405 Feb 22 00:15 authorized_keys
-rw-------    1 leemc     staff  1671 Feb 22 00:13 id_rsa
-rw-r--r--    1 leemc     staff   405 Feb 22 00:13 id_rsa.pub
-rw-r--r--    1 leemc     staff  5350 Feb 21 11:29 known_hosts
-a:~/Users/leemc/


1.1. Which OS are you using: local/remote.
MAC OSX 10.6
1.2. What are the permissions of the .ssh directory and the .ssh/authorized_keys file.
see above.

Last edited by radoulov; 02-22-2011 at 03:52 PM.. Reason: Code tags, please!
# 5  
Old 02-22-2011
OK,
could you please confirm that the output above is from the remote host?

Could you also post the output from the following command:

Code:
ssh -vvv <remote_host>

# 6  
Old 02-22-2011
Experts,

After fiddling/googling for couple of hours, here is where I am. I ran the following script, with parameters, "ls -ltr", it shows me the list of files & directories on the remote machine.
Code:
./script.sh "ls -ltr"

Now, the question is, how do I do more than this ? i.e. the following,

1. Cd to one specific directory
2. Look for all the files that contain a string and once a occurence of the string is found,
3. Pipe that output to awk as input and print $3.

I tried this
Code:
./script.sh "ls -ltr; mv /src/20101129_170410*.log /src/backup"

and it is throwing the following error
Code:
invalid command name "mv"
    while executing
"mv /src/20101129_170410*.log /src/backup"

How should I run the original script now ?

Please advise !

regards,
Lee.

Code:
#!/bin/bash

HOST="egrd.com"
USER="admin"
PASS="abcd"
CMD=$@

VAR=$(expect -c "
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "==============="
echo "$VAR"

---------- Post updated at 03:10 PM ---------- Previous update was at 03:04 PM ----------

Radoulov,

Here is what I get when I ran this.

regards,
Lee.
Code:
-a:~/Users/leemc/ssh -vvv egrd.com
OpenSSH_5.2p1, OpenSSL 0.9.8l 5 Nov 2009
debug1: Reading configuration data /etc/ssh_config
debug2: ssh_connect: needpriv 0
debug1: Connecting to egrd.com
debug1: Connection established.
debug1: identity file /Users/leemc/.ssh/identity type -1
debug1: identity file /Users/leemc/.ssh/id_rsa type -1
debug1: identity file /Users/leemc/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.2
debug1: match: OpenSSH_5.2 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.2
debug2: fd 3 setting O_NONBLOCK
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib

Moderator's Comments:
Mod Comment Please use code tags
# 7  
Old 02-22-2011
I still think it would be better to fix your PKI setup first, because
you really don't need expect in this case.

So, with a correct PKI setup, to print the filenames that start with sh in /etc,
you could simply run the following command:

Code:
$ ssh 0 'printf "%s\n" /etc/sh*'
/etc/shadow
/etc/shadow-
/etc/shells

With expect you need something like this:
Code:
$ ./s 'printf "%s\n" /etc/sh*'
/etc/shadow
/etc/shadow-
/etc/shells

This is the content of the script s:

Code:
#!/usr/bin/expect --
set Machine 0
set Username "<your_username>"
set Password "<your_password>"
set Cmd [lindex $argv 0]
log_user 0
spawn ssh "$Username@$Machine"
while 1 {
  expect {
    "\(yes/no\)\?*" {
     send "yes\r"
     continue
    }
    "assword:" {
      send "$Password\r"
      break
    }
  }
}
expect "# "
send "$Cmd\r"
expect -re "\[^\n]*\n(.*)\n.*# "
puts $expect_out(1,string)

* You'll need to adjust the expect patterns in order to match your prompt.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #932
Difficulty: Easy
Most first-generation personal computers did not keep track of dates and times.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

3. UNIX for Dummies Questions & Answers

What is the significance of sh -s in ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh?

Please can you help me understand the significance of providing arguments under sh -s in > ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh (4 Replies)
Discussion started by: Sree10
4 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

"Cannot allocate memory" error when run from script

hi in my application there is a line with open(/dev/mydevice,0); it work good when it run manually, but when try to run it within script /////////////////////////////////////////// #!/bin/sh ./device_test 0 0 0 1 1 1 0 0 1 ///////////////////////////////////////// I receive 'Failed... (3 Replies)
Discussion started by: majeed
3 Replies

6. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

7. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies

8. Shell Programming and Scripting

catalina.sh : need combination from "start" and "run"

heya, can someone help me with following problem. i am not sure how far you know the catalina.sh script from tomcat. when i start my tomcat with "catalina.sh run" then the startup-process-output will be printed out on the console, but the tomcat process is started in current shell/session, so... (1 Reply)
Discussion started by: Filly
1 Replies

9. Shell Programming and Scripting

How to make a script run for a maximum of "x" number of hours only

How to make a script run for a maximum of "x" number of hours only (7 Replies)
Discussion started by: ScriptDummy
7 Replies

10. UNIX for Dummies Questions & Answers

Run away "bootpgw" & "inetd"

Hello All. I'm get the following messages posted to the /var/adm/syslog file ever second and not sure on how to stop the process. May 14 15:50:52 a3360 bootpgw: version 2.3.5 May 14 15:50:52 a3360 inetd: /etc/bootpgw exit 0x1 As said about this gets logged every second only thing that... (4 Replies)
Discussion started by: cfaiman
4 Replies

Featured Tech Videos