Flush shell command to .bash_history

  The command in current shell will not save to .bash_history unless you exit the shell. But when you type "history -w", you will flush it in .bash_history.

  To earse the history, you should type"history -c", the command will clean all records in current shell.

──(root㉿kali)-[/home/hgy/sqlsucai] └─# cat /home/hgy/sqlsucai/hr.sql | sudo docker exec -i mysql5.7 mysql -uroot -p 123456 hr mysql Ver 14.14 Distrib 5.7.44, for Linux (x86_64) using EditLine wrapper Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Usage: mysql [OPTIONS] [database] -?, --help Display this help and exit. -I, --help Synonym for -? --auto-rehash Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash. (Defaults to on; use --skip-auto-rehash to disable.) -A, --no-auto-rehash No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of mysql and disables rehashing on reconnect. --auto-vertical-output Automatically switch to vertical output mode if the result is wider than the terminal width. -B, --batch Don't use history file. Disable interactive behavior. (Enables --silent.) --bind-address=name IP address to bind to. --binary-as-hex Print binary data as hex --character-sets-dir=name Directory for character set files. --column-type-info Display column type information. -c, --comments Preserve comments. Send comments to the server. The default is --skip-comments (discard comments), enable with --comments. -C, --compress Use compression in server/client protocol. -#, --debug[=#] This is a non-debug version. Catch this and exit. --debug-check This is a non-debug version. Catch this and exit. -T, --debug-info This is a non-debug version. Catch this and exit. -D, --database=name Database to use. --default-character-set=name Set the default character set. --delimiter=name Delimiter to be used. --enable-cleartext-plugin Enable/disable the clear text authentication plugin. -e, --execute=name Execute command and quit. (Disables --force and history file.) -E, --vertical Print the output of a query (rows) vertically. -f, --force Continue even if we get an SQL error. --histignore=name A colon-separated list of patterns to keep statements from getting logged into syslog and mysql history. -G, --named-commands Enable named commands. Named commands mean this program's internal commands; see mysql> help . When enabled, the named commands can be used from any line of the query, otherwise only from the first line, before an enter. Disable with --disable-named-commands. This option is disabled by default. -i, --ignore-spaces Ignore space after function names. --init-command=name SQL Command to execute when connecting to MySQL server. Will automatically be re-executed when reconnecting. --local-infile Enable/disable LOAD DATA LOCAL INFILE. -b, --no-beep Turn off beep on error. -h, --host=name Connect to host. -H, --html Produce HTML output. -X, --xml Produce XML output. --line-numbers Write line numbers for errors. (Defaults to on; use --skip-line-numbers to disable.) -L, --skip-line-numbers Don't write line number for errors. -n, --unbuffered Flush buffer after each query. --column-names Write column names in results. (Defaults to on; use --skip-column-names to disable.) -N, --skip-column-names Don't write column names in results. --sigint-ignore Ignore SIGINT (CTRL-C). -o, --one-database Ignore statements except those that occur while the default database is the one named at the command line. --pager[=name] Pager to use to display results. If you don't supply an option, the default pager is taken from your ENV variable PAGER. Valid pagers are less, more, cat [> filename], etc. See interactive help (\h) also. This option does not work in batch mode. Disable with --disable-pager. This option is disabled by default. -p, --password[=name] Password to use when connecting to server. If password is not given it's asked from the tty. -P, --port=# Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306). --prompt=name Set the mysql prompt to this value. --protocol=name The protocol to use for connection (tcp, socket, pipe, memory). -q, --quick Don't cache result, print it row by row. This may slow down the server if the output is suspended. Doesn't use history file. -r, --raw Write fields without conversion. Used with --batch. --reconnect Reconnect if the connection is lost. Disable with --disable-reconnect. This option is enabled by default. (Defaults to on; use --skip-reconnect to disable.) -s, --silent Be more silent. Print results with a tab as separator, each row on new line. -S, --socket=name The socket file to use for connection. --ssl-mode=name SSL connection mode. --ssl Deprecated. Use --ssl-mode instead. (Defaults to on; use --skip-ssl to disable.) --ssl-verify-server-cert Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead. --ssl-ca=name CA file in PEM format. --ssl-capath=name CA directory. --ssl-cert=name X509 cert in PEM format. --ssl-cipher=name SSL cipher to use. --ssl-key=name X509 key in PEM format. --ssl-crl=name Certificate revocation list. --ssl-crlpath=name Certificate revocation list path. --tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1, TLSv1.2 --server-public-key-path=name File path to the server public RSA key in PEM format. --get-server-public-key Get server public key 指令格式错误码?
最新发布
07-10
<think>嗯,用户想知道如何在Python中使用Paramiko库的client.exec_command方法。首先,我需要回忆Paramiko的基本用法。Paramiko是一个用于SSH连接的Python库,允许远程执行命令。client.exec_command应该是SSHClient类的一个方法,用来执行单条命令。 根据引用[4],用户可能需要处理标准输入、输出和错误流。记得exec_command返回三个文件对象:stdin、stdout和stderr。需要正确读取这些流,特别是当命令有输出时。比如,执行命令后,应该用stdout.read()来获取输出结果,而stderr.read()来获取错误信息。 另外,用户可能需要处理输入的情况。不过引用[4]提到,如果需要写入输入,可能需要使用stdin.channel.shutdown_write()来关闭写入通道,以便可以继续读取输出。这可能涉及到交互式命令的情况,但exec_command通常用于单次命令执行,而不是交互式会话。这时候可能需要使用invoke_shell方法,如引用[2]中提到的。 还有,用户可能会遇到主机密钥的问题。引用[2]中的代码示例展示了如何设置client.set_missing_host_key_policy(AllowAllKeys()),这样可以自动接受未知的主机密钥,不过在生产环境中可能不够安全,应该谨慎使用。 接下来,应该分步骤说明如何使用exec_command,包括建立连接、执行命令、处理输入输出和关闭连接。还要注意异常处理,比如网络错误或认证失败的情况。此外,引用[3]提到了其他SSH库,但用户的问题集中在Paramiko上,所以可以暂时忽略其他库的信息。 需要提醒用户注意安全,比如避免硬编码密码,使用密钥认证,正确处理主机密钥等。同时,给出代码示例,展示完整的流程,包括导入模块、创建SSHClient实例、执行命令、读取输出和关闭连接。如果用户需要执行需要输入的命令,可能需要结合其他方法,比如invoke_shell,但根据问题,专注于exec_command的用法即可。 最后,生成的相关问题应该围绕Paramiko的其他功能、错误处理、与Shell的区别以及替代库展开,帮助用户进一步学习。</think>### 如何使用Paramiko的`client.exec_command`方法? Paramiko是一个Python实现的SSHv2协议库,广泛用于远程服务器管理。`client.exec_command`方法用于在SSH会话中执行单条命令并获取其输入/输出流。以下是具体步骤和注意事项: --- #### 1. **建立SSH连接** ```python import paramiko # 创建SSHClient实例 client = paramiko.SSHClient() # 自动接受未知主机密钥(生产环境慎用)[^2] client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 client.connect( hostname='example.com', port=22, username='user', password='password' # 或使用密钥pkey=paramiko.RSAKey.from_private_key_file('key.pem') ) ``` --- #### 2. **执行命令并获取流** `exec_command`返回三个文件类对象:`stdin`(标准输入)、`stdout`(标准输出)、`stderr`(标准错误)。 ```python stdin, stdout, stderr = client.exec_command('ls -l /tmp') ``` - **读取输出**: ```python output = stdout.read().decode('utf-8') error = stderr.read().decode('utf-8') print("输出:", output) print("错误:", error) ``` - **写入输入**(如命令需要交互): 若命令需要输入(如`sudo`密码),需通过`stdin`写入并关闭写入通道: ```python stdin.write('mypassword\n') stdin.flush() stdin.channel.shutdown_write() # 关闭写入[^4] ``` --- #### 3. **关闭连接** ```python client.close() ``` --- #### 4. **完整示例** ```python try: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('example.com', username='user', password='password') stdin, stdout, stderr = client.exec_command('echo "Hello, Paramiko!"') print(stdout.read().decode()) except paramiko.AuthenticationException: print("认证失败") except paramiko.SSHException as e: print("SSH连接错误:", e) finally: client.close() ``` --- #### **关键注意事项** - **超时设置**:`connect()`和`exec_command`可通过`timeout`参数设置超时。 - **流读取顺序**:需先读取`stdout`或`stderr`,再执行其他操作。 - **性能优化**:频繁命令建议复用同一SSH连接,避免重复建立连接。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值