cat > first.sh
cd /tmp
echo $PWD
echo `whoami`
cd /tmp/123
tar -cvf 789.tar 456
sleep 10
[ctrl-d]
except script:
cat > first
#!/usr/bin/expect
set ip 10.5.15.20
set user "xyz123"
set password "123456"
set script first.sh
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
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.