I have an ASP.NET Core 5 web app running on Linux (Apache) which is working fine in general. I want it to be able to change passwords of certain Linux users. Basically the user logged into the web app can change their corresponding Linux password, which they use for SFTP.
I can run ssh/bash commands from the web app but they are run using the user www-data
I'm not sure what the best way of doing this is:
Try to start a new SSH session all together as a user with sudo rights. (I'm not sure how to do that though.
Use sudo with the www-data user (seems less secure as it increases permissions for anything else using the www-data user).
Running a command like this does work to change a user's password as long as there are root permissions:
echo -e "changeme2\nchangeme2" | passwd testuser1
Thanks for any advice.
Thanks #feihoa, I did end up using SSH.NET to create a SSH session to localhost and do what I needed to.
public void SetLinuxPassword()
{
using (var client = new SshClient("localhost", "{username}", "{password}"))
{
client.Connect();
SshCommand cmd = client.RunCommand($"echo -e \"{password}\" | sudo -S bash changePassword.sh {LinuxUsername} {_newPlainTextPassword}");
}
}
I am trying to restart the Apache server through ssh command in perl - php.
Below is code I tried. It works through PuTTY. Though if I run through browser it does not run at line : $output = $ssh->exec("systemctl status apache2");
use Net::SSH::Expect;
my $ssh = Net::SSH::Expect->new (
host => "xx.xx.xx.xx",
password=> 'adsd#21',
user => 'root',
raw_pty => 1
);
print("\n");
my $output = $ssh->login();
print($output);
if ($output !~ /Welcome/) {
die "Login has failed. Login output was $login_output";
}
$output = $ssh->exec("systemctl status apache2");
The Error:
"WARNING: terminal is not fully functional"
Though Net::SSH is an elegant module, I would suggest using the system tools available to you by default.
You have ssh available and using ssh-keys are more secure, especially due to the fact that you currently display passwords in a script. If you are unsure of how to setup ssh-keys, let me know and I will add it to the answer.
Effectively your entire script can purely be:
use strict;
use warnings;
my $apache_status = `ssh username\#servername systemctl status apache2`;
print $apache_status;
I have several applications that change IP addresses every-time they are deployed.
I am wondering how I can create a bash shell script to:
Modify/update existing Firefox bookmarks
If the bookmarks don't exist, then create them.
After some research, I found that I need to modify places.sqlite, so I downloaded sqlite. I looked at the schema, and I think moz_places and moz_bookmarks are what I need to insert to, but I am not sure. If so, how would connect the ids if I need to 2 separate inserts. I already have a way to get the new ip address for every new deployment, so I would just stick that into a variable.
My use case looks something like this:
Deployment 1: URL: 192.168.1.**10**/app1
Deployment 2: URL: 192.168.1.**20**/app1
Brownie points if I can create multiple folders 1st and insert bookmarks inside them. Like {Folder#1: app1, app2}, {Folder#2: app3}, {Folder#3: app4, app5, app6}.
A shell script might not be the best tool for this problem; but you could use a script like this to redirect your browser to a new location each time your application redeploys, and bookmark localhost:<port>:
#!/bin/bash
# redirect localhost:<port> to another address with HTML
local_port="${1:?provide local port to listen on}"
redirect="${2:?provide application ip address}"
while :; do
(
echo "HTTP/1.1 200 OK"
echo ""
echo "<head>"
echo " <meta http-equiv=\"refresh\" content=\"0; url=$redirect\" />"
echo "</head>"
echo ""
) | ncat -l -p "$local_port" > /dev/null
done
This would let you bookmark localhost:8000 in Firefox, and call the script when you redeploy. You could have an instance of the script running for each app; you'd just need to change the listening port. If you have a script that redeploys your app, you could add these lines there.
$ bash redirect.sh 8000 192.168.1.10/app1
$ bash redirect.sh 8001 192.168.1.11/app1
I have a connection between my localhost and a remote server using putty SSH tunnel.
Thats fine.
Now I need a command to get the sql file on my local machine i.e. c:\folder\test.sql and import it into mysql on the remote server
I thought maybe...
mysql -u prefix_username -p testpass -h localhost -P 3307 prefix_testdb
then do a command like
mysql -p testpass -u prefix_username prefix_testdb < c:\folder\test.sql
this command did not work.
How can I acheive this?
You should run this command
mysql -h host -u user_name -pPassword database < file.sql > output.log
file.sql contains the sql queries to run and output.log makes sense only when you have a query that returns something (like a select)
The only thing different I can see in your code is the blank space between the -p option and the password. If you use the -p option, you must write the password without leaving any blank space. Or you just can user the option --password=Password
I hope you can solve the problem
You will need to ssh to the remote machine with the mysql command appended:
ssh remote_user#remote_server mysql -p testpass -u username testdb < c:\folder\test.sql
1. mysql -h xxx -uxxx -pxxx . //login to the remote mysql
2. use DATABASE. //assign which db to import
3. source path/to/file.sql //the path can be your local sql file path.
Reference: Import SQL file into mysql
Use 'scp' to copy and mysql to insert to you local machine.
Syntax:
scp remote_user#remove_server:/path/to/sql/file.sql ~/path/to/local/directory
after you transfered the file use:
mysql -uYouUserName -p name_of_database_to_import_to < ~/path/to/local/directory/file.sql
mysql {mydbname} --host {server}.mysql.database.azure.com --user {login} --password={password} < ./{localdbbackupfile}.sql
As managed services, DevOps, and CI/CD workflows have become more popular by this point, most providers of those managed services want to remove the human error part of getting the connection strings correct. If you happen to be using Azure, AWS, GCP, etc, There usually is a page or terminal command that shows you these strings to help you easily integrate. Don't forget to check their docs if you're using something like that. They are auto generated, so they are most likely 'best practice' with spot-on correct syntax for the db version you may be using.
The above command is from "connection strings" on the product details page of my Azure Managed Mysql DB Server instance.
Not necessarily asked, but an fyi, a lot of those services auto generate templates for use in a lot of common connection scenarios:
{
"connectionStrings": {
"ado.net": "Server={server}.mysql.database.azure.com; Port=3306; Database=mytestdb; Uid={login}; Pwd={password};",
"jdbc": "jdbc:mysql://{server}.mysql.database.azure.com:3306/mytestdb?user={login}&password={password}",
"jdbc Spring": "spring.datasource.url=jdbc:mysql://{server}.mysql.database.azure.com:3306/mytestdb spring.datasource.username={login} spring.datasource.password={password}",
"mysql_cmd": "mysql mytestdb --host {server}.mysql.database.azure.com --user {login} --password={password}",
"node.js": "var conn = mysql.createConnection({host: '{server}.mysql.database.azure.com', user: '{login}', password: {password}, database: mytestdb, port: 3306});",
"php": "$con=mysqli_init(); [mysqli_ssl_set($con, NULL, NULL, {ca-cert filename}, NULL, NULL);] mysqli_real_connect($con, '{server}.mysql.database.azure.com', '{login}', '{password}', 'mytestdb', 3306);",
"python": "cnx = mysql.connector.connect(user='{login}', password='{password}', host='{server}.mysql.database.azure.com', port=3306, database='mytestdb')",
"ruby": "client = Mysql2::Client.new(username: '{login}', password: '{password}', database: 'mytestdb', host: '{server}.mysql.database.azure.com', port: 3306)"
}
}
You can use pscp to upload file to the server. Go to your command line and type this
pscp.exe c:\folder\test.sql usernameoftheserver#websitename.com:/serverpath
I am trying to start Weblogic Admin server using start-up script (./startWeblogic.sh) from a remote host using a different user. The server starts fine but prompt is stuck, it does not return
Background: We have multiple admin servers in different environment and the requirement is all has to be started/stopped from a central automation server which has a password less sudo connectivity to all Weblogic hosts.
I am using command :
{ssh -l user remote-address '/spare/app/oracle/product/Middleware/user_projects/domains/example_domain/bin/./startWebLogenter code hereic.sh & > /dev/null < /dev/null'}
As admin server is spawning a child shell, the parent shell is not closing and it keeps holding the prompt.
Please Advise.
Thanks,
Bhaskar
A common approach is to use the nohup command. Try this:
ssh -l user remote-address '/usr/bin/nohup /spare/app/oracle/product/Middleware/user_projects/domains/example_domain/bin/./startWebLogic.sh & > /dev/null < /dev/null'