Visit Our UNIX and Linux User Community

Except script to run a local shell script on remote server using root access

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Except script to run a local shell script on remote server using root access
# 1  
Old 09-17-2017
Except script to run a local shell script on remote server using root access

local script:
cat >
cd /tmp
echo $PWD
echo `whoami`
cd /tmp/123
tar -cvf 789.tar 456
sleep 10

except script:

cat > first
set ip
set user "xyz123"
set password "123456"
set script

spawn sh -c "ssh $user@$ip bash < $script"

expect "Password:"
send "$password\r"

expect eof
[ctrl -d]

When I run the except script, I get the below error because the folder "/tmp/123" has root permission
bash: line 4: cd: /tmp/123: Permission denied

How can I run scipt as root user?

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-17-2017 at 08:31 AM.. Reason: Added CODE tags.
# 2  
Old 09-25-2017
It would be better, if you actually know the credentials of the root user, to connect and set up SSH keys to achieve this rather than trying to get round it with expect.

You have to decide on the target server if root is allowed to login, and this may be preventing you, and for a good reason too.

Clearly this is a dummy set up, so what are you actually trying to achieve? There are probably better ways rather than trying to crack in and shovel in direct commands. Think - If you can do it then so can someone else with different motives.

If you can describe what you need then we can probably help you find a much neater way to do this.

Kind regards,
# 3  
Old 09-25-2017
This looks a lot like the exploit I saw when my server was compromised years ago - login, extract code in a temp dir, sleep.

If you are not actually trying to crack and exploit the destination server, use ssh shared keys as intended! It will be so much easier.

Previous Thread | Next Thread
Test Your Knowledge in Computers #247
Difficulty: Easy
By 1973, the File Transfer Protocol (FTP) specification had been defined (RFC 354) and implemented, enabling file transfers over the ARPANET.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

2. Solaris

Script to get files from remote server to local server through sftp without prompting for password

Hi, I am trying to automate the process of fetching files from remote server to local server through sftp. I have the username and password for the remote solaris server. But I need to give password manually everytime i run the script. Can anyone help me in automating the script such that it... (3 Replies)
Discussion started by: ssk250
3 Replies

3. Shell Programming and Scripting

To run a shell script in remote server from windows batch file

Hi all, i need to run a shell script on remote server. I have created file .bat file in windows server with following code, c:\Users\Desktop\putty.exe -ssh -pw password user@server ./ i need to run the in my remote server Above command is not working, any... (4 Replies)
Discussion started by: rammm
4 Replies

4. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < However, when I try to pass arguments to it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

5. Shell Programming and Scripting

Execute a local script against a remote server

I am unable to run the below script against a remote server due to syntax error (then unexpected), but i am able to run it locally. Am i executing it correctly or is there any other way to execute it. ssh username@servernname ksh -s < scriptname #!/bin/ksh function record { ((end =... (5 Replies)
Discussion started by: NarayanaPrakash
5 Replies

6. Programming

Expect script to run a Shell script on remote server

Hi All, I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands. spawn ssh $uid@$host expect "password:" send "$password\r" expect "*\r" send... (2 Replies)
Discussion started by: yashwanthsn
2 Replies

7. Shell Programming and Scripting

rsh help - getting the output of remote script to local server.

Hi, I have a script that runs for an hour. Have to run it on remote server and need the output it produces on the remote server to decide for failure or success. I run it through a Autosys Job which logs the outputs, both 1 & 2. I use the commands 1) rsh <SERVER> 'nohup /tmp/xyz.ksh &' 2)... (5 Replies)
Discussion started by: aster007
5 Replies

8. Shell Programming and Scripting

How to stop a script running in remote server from local script

Hi, I have googled for quite some time and couldn't able to get what exactly I am looking for.. My query is "how to stop a shell script which is running inside a remote server, using a script"??? can any one give some suggestions to sort this out. (1 Reply)
Discussion started by: mannepalli
1 Replies

9. Shell Programming and Scripting

Can a script runned in local server access remote server?

Hi, Im creating a script that is supposed to run commands on remote server using sftp. My script is as below: #!/bin/ksh sftp remote_server mypassword cd /u08/mydir/allfiles mget * .. But this is what I got when I runned the script: Connecting to remote server...... (3 Replies)
Discussion started by: luna_soleil
3 Replies

10. Shell Programming and Scripting

executing a remote location script from local server

hi i am having two servers one is local and remote(FTP)server.from local server i have to connect to remote server and execute a shell script i want to run a shell script(remote location) from my local server i am having some knowledge on ftp but i am not getting the result .please give ... (2 Replies)
Discussion started by: srivsn
2 Replies

Featured Tech Videos