- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: script regarding ssh
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 02:48 AM
11-28-2011 02:48 AM
script regarding ssh
Hello,
Suppose i am logged in server 1. I want to ssh to server 2. There I want to check for the presence of a particular type of files ( for eg .dbf files in /somepath). Then if the file is present, it has to exit ssh and trigger another script (for eg backup.ssh at /somepath2) which is there in server 1. But if the file is not present in server 2 then nothing should happen.
- Tags:
- ssh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 04:30 AM
11-28-2011 04:30 AM
Re: script regarding ssh
You should be able to use ssh and ls to list the files you want.
Then grep the output and if present run that other script:
ssh server2 ls "/somepath/*.dbf" | grep -q "/somepath/.*\.dbf"
if [ $? -eq 0 ]; then
/somepath2/backup.ssh
fi
Note: This assumes that you can use ls instead of using find to search a whole tree.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 05:04 AM
11-28-2011 05:04 AM
Re: script regarding ssh
ssh server2 ls "/somepath/*.dbf" | grep -q "/somepath/.*\.dbf"
can i use :
ssh server2 test -f /somepath/*.dbf
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 05:15 AM
11-28-2011 05:15 AM
Re: script regarding ssh
>can I use: ssh server2 test -f /somepath/*.dbf
No. -f only takes one file, not a pattern.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 05:22 AM
11-28-2011 05:22 AM
Re: script regarding ssh
May i know the meaning of this line :
if [ $? -eq 0 ];
What will $? -eq 0 do?
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 05:25 AM
11-28-2011 05:25 AM
Re: script regarding ssh
>What will $? -eq 0 do?
This checks the exit status of the previous command. In this case, grep found some matches in the ls output.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 05:33 AM
11-28-2011 05:33 AM
Re: script regarding ssh
Just one more point :
I want to ssh to the server and want to login using root credentials. May i know how in every ssh it will login with root and will not ask for password. If the password need to be given, then how can it be applied in the script.
Regards..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 05:39 AM
11-28-2011 05:39 AM
Re: script regarding ssh
>I want to ssh to the server and want to login using root credentials.
You need to search for topics about setting up ssh:
http://h30499.www3.hp.com/t5/System-Administration/Cannot-passwordlessly-ssh-using-root/m-p/5381979/
http://h30499.www3.hp.com/t5/System-Administration/SSH-question/m-p/4299800/
http://h30499.www3.hp.com/t5/General/SSH-Host-based-Authentication-setup/m-p/4902860/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 05:42 AM
11-28-2011 05:42 AM
Re: script regarding ssh
Can i use local user to run the script. What is the procedure to run the script by local user..
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2011 05:48 AM
11-28-2011 05:48 AM
Re: script regarding ssh
>Can I use local user to run the script? What is the procedure to run the script by local user?
You don't need to be root, you just need to be able to list those directories.
You have to do the same ssh setup, except you don't need to give away the keys to the city by using root.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2011 03:21 AM
11-30-2011 03:21 AM
Re: script regarding ssh
Hello..
As per you given script above i have made a test script as below :
root@serverA:/home/rtns>cat testscript
#!/usr/bin/sh
ssh rets@serverB ls "/home/rtns/a/*.abc" | grep -q "MPF*.abc"
if [$? -eq 0]; then
rmdir abc (abc dirctory i have temporarily made in /home/rtns)
fi
Already i am able to do passwordless ssh and able to list the files. But when running this script i am gettin below error :
root@serverA:/home/rtns>./testscript
./testscript[3]: [1: not found.
following test files i have created in serverB in /home/rtns/a :
1_66.abc
2_66.abc
MPF1_1_66.abc
MPF1_2_66.abc
MPF1_3_66.abc
Please mention in the script where i am doing a mistake.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2011 07:02 AM - edited 11-30-2011 07:08 AM
11-30-2011 07:02 AM - edited 11-30-2011 07:08 AM
Re: script regarding ssh
if [$? -eq 0]; then
rmdir abc # (abc directory I have temporarily made in /home/rtns)
fi
>when running this script I am getting below error :
./testscript[3]: [1: not found.
Spacing is important when using [ ] or [[ ]]. You need to cut&paste from the post:
if [ $? -eq 0 ]; then
Note the "1" comes from $? which indicates the grep failed. There is a typo there in the regex, change to:
ssh rets@serverB ls "/home/rtns/a/*.abc" | grep -q "MPF.*\.abc"
Note: grep takes a regex but ls(1) takes a pattern.
>rmdir abc
This isn't likely to work if abc still contains files.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2011 11:00 PM
12-01-2011 11:00 PM
Re: script regarding ssh
Hi Dennis,
Thank u very much for ur support.
Your solution worked..
But i m having one more concern:
ssh rtns@serverB ls "/home/rtns/a/*.abc" | grep -q "MPF.*\.abc"
The above command checks all the files of MPF*.abc in serverB. The concern i m having is in serverA the files are generated with .abc extension and with serial nos. For e.g. 1_46446_652110140.abc, 1_46447_652110140.abc and so on. And in serverB they are shipped as MPF1_46446_652110140.abc, MPF1_46447_652110140.abc and so on. I want script that should check that a particular no. of file that has been generated on serverA has been shipped on serverB or not. Say in above case like if 46th file is generated on serverA and if it is shipped in serverB, then it should check the presence of 46th file on serverB and then only it should trigger the other script in serverA. And if the file is not present nothing should happen.
Thanks in advance,
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2011 08:56 AM
12-02-2011 08:56 AM
Re: script regarding ssh
>serverA: 1_46446_652110140.abc, ...
>serverB: MPF1_46446_652110140.abc, ...
>I want script that should check that a particular no. of file that has been generated on serverA has been shipped on serverB or not. Say in above case like if 46th file is generated on serverA and if it is shipped in serverB, then it should check the presence of 46th file on serverB and then only it should trigger the other script in serverA.
Do you want to check to see if there is a match on ALL files that are copied to serverB?
Or do you want to invoke that script if the matching files are on both machines?
And do you want to invoke the script for each match or only once if any matches?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-02-2011 07:02 PM
12-02-2011 07:02 PM
Re: script regarding ssh
>>Do you want to check to see if there is a match on ALL files that are copied to serverB?
Or do you want to invoke that script if the matching files are on both machines?
I will run the script through crontab. I want to invoke the script if the matching files are on both machines. And the script should not run if the file that is present on serverA has not shipped to serverB. That is if file is present on serverA and not present on serverB, then script should not run. That match should be regarding the particular no. of file.
Please revert if any more concern is there.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2011 03:02 PM
12-03-2011 03:02 PM
Re: script regarding ssh
>I want to invoke the script if the matching files are on both machines.
So you don't care if there are extra files on serverA or serverB.
# get basenames of files on each server
ls /serverA-path/*.abc | sed -e 's:.*/::g' > serverA_files
ssh rtns@serverB ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' > serverB_files
# find matches:
comm -12 serverA_files serverB_files > matched_files
if [ -s matched_files ]; then # some matches
run-script-here
fi
rm -f matched_files # cleanup
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2011 02:45 AM - edited 12-05-2011 03:25 AM
12-05-2011 02:45 AM - edited 12-05-2011 03:25 AM
Re: script regarding ssh
Hi dennis,
Thank u very much for the script.
I m having some queries about the script that i am mentioning below :
>># get basenames of files on each server
ls /serverA-path/*.abc | sed -e 's:.*/::g' > serverA_files
ssh rtns@serverB ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' > serverB_files
What are the serverA_files and serverB_files. Are they the folders that i have to make in respective directories. In the severB the .abc files are having MPF extension also. Whether that MPF will be removed while matching the files.
>>comm -12 serverA_files serverB_files > matched_files
if [ -s matched_files ]; then # some matches
run-script-here
fi
rm -f matched_files # cleanup
If some files are matched only, then also the script will run or the script will run only after all files are matched. My concern is to invoke the script only after all files on serverA and serverB are matched. If only some files are matched, then script should not run.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2011 09:04 AM - edited 12-05-2011 09:04 AM
12-05-2011 09:04 AM - edited 12-05-2011 09:04 AM
Re: script regarding ssh
>What are the serverA_files and serverB_files. In the serverB the .abc files are having MPF extension also. Whether that MPF will be removed while matching the files.
These are files that contain the list of files on each machine.
The sed command will remove the leading "MPF".
>My concern is to invoke the script only after all files on serverA and serverB are matched. If only some files are matched, then script should not run.
Ok, then:
# find matches:
cmp -s serverA_files serverB_files
if [ $? -eq 0 ]; then # all matched
run-script-here
else
echo "Some files not matched:"
comm -3 serverA_files serverB_files
fi
rm -f serverA_files serverB_files # cleanup
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2011 11:27 PM
12-06-2011 11:27 PM
Re: script regarding ssh
Hi dennis,
Thank u very much for the script. I am having a couple of questions for the above script:
>> if [ $? -eq 0 ]; then # all matched
/pathtoscript/backup.sh
whether the script will successfully run by typing the script as above. Or is there any other way to run the script.
>>else
echo "Some files not matched:"
comm -3 serverA_files serverB_files
I will run the script through cron entry per hour. So where the output of the above echo command will be printed.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2011 11:38 PM
12-06-2011 11:38 PM
Re: script regarding ssh
> /pathtoscript/backup.sh
>whether the script will successfully run by typing the script as above. Or is there any other way to run the script?
I don't know how to run your script or what parms it takes??
> echo "Some files not matched:"
> comm -3 serverA_files serverB_files
>I will run the script through cron entry per hour. So where the output of the above echo command will be printed.
It will be mailed to you. I left it there in case you wanted some debugging output.
You could comment it out like:
: echo "Some files not matched:"
: comm -3 serverA_files serverB_files
I used ":" vs "#" since there must be something in the else block.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2011 05:27 AM
12-07-2011 05:27 AM
Re: script regarding ssh
Hi dennis,
Thnx for the script. I have tested a test script based on above script and it is working successfully. Just one more concern i m having. I am providing the test script which i have tested:
#!/usr/bin/sh
ls /home/rtns/testdir/*.abc | sed -e 's:.*/::g' > serverA_files
ssh root@austsapdr ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' > serverB_files
cmp -s serverA_files serverB_files
if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh
else
: echo "Some files not matched:"
: comm -3 serverA_files serverB_files
fi
rm -f serverA_files serverB_files
>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh
If the exit status is not 0 then the script will not run. But if the exit status is zero and the script when runs, then after that an ssh should be done to serverB to run a script on serverB. Or any other way to do this??
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2011 01:09 PM
12-07-2011 01:09 PM
Re: script regarding ssh
>Thnx for the script. I have tested a test script based on above script and it is working successfully.
(If you are happy, please click on the Kudos! star of the posts that helped.)
>sh /home/rtns/x/a.sh
There is no need to use "sh" on this line. Just make sure your script is executable and has "#!..." on the first line.
>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh
>if the exit status is zero and the script runs, then after that an ssh should be done to serverB to run a script on serverB. Or any other way to do this?
You can either put that ssh into a.sh (but it would have to know about serverB) or add that ssh right after.
Do you also need to check the exit status of a.sh?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2011 10:16 PM
12-07-2011 10:16 PM
Re: script regarding ssh
>>if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh
>>Do you also need to check the exit status of a.sh?
No. I dont need the exit status of a.sh. I am concern of only that if a.sh runs (i.e. status of [ $? -eq 0 ] is zero) then only ssh is to do done to serverB to run a script there and if a.sh do not runs (i.e. status of [ $? -eq 0 ] is non-zero), then ssh should not be done to serverB.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2011 12:27 AM
12-08-2011 12:27 AM
Re: script regarding ssh
>I am concern of only that if a.sh runs then only ssh is to do done to serverB to run a script there ...
That "if" will do that. You just need to add that ssh in the "then" block after a.sh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2011 02:38 AM
12-08-2011 02:38 AM
Re: script regarding ssh
I tried adding another "then" as below :
#!/usr/bin/sh
ls /home/rtns/testdir/*.abc | sed -e 's:.*/::g' > serverA_files
ssh root@serverB ls "/home/rtns/a/*.abc" | grep "MPF.*\.abc" | sed -e 's:.*/MPF::g' > serverB_files
cmp -s serverA_files serverB_files
if [ $? -eq 0 ]; then
sh /home/rtns/x/a.sh; then
ssh root@serverB sh /home/rtns/testdir1/testscript
else
: echo "Some files not matched:"
: comm -3 serverA_files serverB_files
fi
rm -f serverA_files serverB_files
But it given the following error :
root@serverA:/home/rtns>./testscript2 (testscript2 is the above script)
Password:
Password:
./testscript2[5]: Syntax error at line 7 : `then' is not expected.
Regards,